DES加密问题 [英] Issue with DES encryption

查看:126
本文介绍了DES加密问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DES加密简单的文本文件。它在文件中附加了额外的一些特殊字符,当我解密同一个文件时也会发生同样的事情。





I am encrypting simple text files using DES.Eery time it appends extra few special charactes in file and same happens when i decrypt the same file.


static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey)
        {
            {
                SqlConnection conn = null;
                SqlDataReader rdr = null;
                try
                {
                    conn = new
                    SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
                    SqlCommand cmd = new SqlCommand("my_proc", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    conn.Open();
                    SqlDataReader sqlReader = cmd.ExecuteReader();
                    string suffix = null;
                    string pKey = null;
                    Object xsuffix = new Object();
                    Object ypKey = new Object();
                    while (sqlReader.Read())
                    {
                        ypKey = sqlReader.IsDBNull(sqlReader.GetOrdinal("PrimaryNum"))
                            ? String.Empty
                            : sqlReader.GetValue(sqlReader.GetOrdinal("PrimaryNum"));
                        xsuffix = sqlReader.IsDBNull(sqlReader.GetOrdinal("Suffix"))
                            ? String.Empty
                            : sqlReader.GetValue(sqlReader.GetOrdinal("Suffix"));
                        suffix = xsuffix.ToString();
                        pKey = ypKey.ToString();
                    }
                    sqlReader.Close();
                    byte[] salt = Encoding.ASCII.GetBytes(suffix);
                    FileStream fsInput = new FileStream(sInputFilename,
                          FileMode.Open,
                          FileAccess.Read);
                    byte[] bytearrayinput = new byte[fsInput.Length];
                    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
                    fsInput.Close();
                    FileStream fsEncrypted = new FileStream(sOutputFilename,
                       FileMode.Create,
                       FileAccess.Write);
                    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                    Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(pKey, salt);
                    DES.Key = deriveBytes.GetBytes(8);
                    DES.Mode = CipherMode.ECB;
                    DES.Padding = PaddingMode.PKCS7;
                    ICryptoTransform desencrypt = DES.CreateEncryptor();
                    CryptoStream cryptostream = new CryptoStream(fsEncrypted,
                       desencrypt,
                       CryptoStreamMode.Write);

                    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
                    cryptostream.FlushFinalBlock();
                    cryptostream.Close();
                    fsEncrypted.Close();
                }
                finally
                {
                    if (conn != null)
                    {
                        conn.Close();
                    }
                    if (rdr != null)
                    {
                        rdr.Close();
                    }
                }
            }
        }
What could be wrong? i tried different padding mode already. No luck :(

推荐答案

Padding是Block cipher实现的一部分。



参考

http://msdn.microsoft.com/en-us/library/system.security.cryptography.paddingmode(v = vs.110).aspx [ ^ ]



如果您使用8分组密码,

如果要加密9字节数据

FF FF FF FF FF FF Ff Ff FF



使用PaddingMode.ANSIX923

将添加7个字节,以便我们有整数个块,即2个8字节的块



这些街区将是

FF FF FF FF FF Ff Ff Ff FF 00 00 00 00 00 00 07



填充块中附加字节的正确字节表示块中有多少字节未使用。这将允许在解密过程中删除这些未使用的填充字节。



如果您检查样本

http://msdn.microsoft.com/en-us/library/0dh224hh(v=vs。 110).aspx [ ^ ]



数据是这是一些要加密的数据。 ,29个字节



它被加密到长度为32个字节的文件



但是,当你解密文件,你得到29字节数据。



您可能还想参考最近的CP文章

C#中的时髦加密/解密 [ ^ ]



您将注意到,implmentation将加密数据保存到文件,但随后读出加密数据以进行缓冲,解密到缓冲区,然后将解密数据保存回文件。请注意,保存的解密文件与原始文件的长度相同。但加密文件总是稍大(文件长度是块大小的倍数)。
Padding is part of Block cipher implementation.

Refer to
http://msdn.microsoft.com/en-us/library/system.security.cryptography.paddingmode(v=vs.110).aspx[^]

If you are using 8 block cipher,
If you want to encrypt 9 bytes data
FF FF FF FF FF FF Ff Ff FF

Using PaddingMode.ANSIX923
7 bytes will be added so that we have whole number of blocks, ie 2 blocks of 8 bytes

The blocks would be
FF FF FF FF FF Ff Ff Ff FF 00 00 00 00 00 00 07

The right bytes of the appended bytes in the padded block indicates how many byte in the blocks are unused. This will allow such unused padding bytes to be remove in the decryption process.

If you examine the sample in
http://msdn.microsoft.com/en-us/library/0dh224hh(v=vs.110).aspx[^]

the data is "Here is some data to encrypt." , 29 bytes

It gets encrypted to file that has a length of 32 bytes

But, when you decrypts the file, you get back the 29 bytes data.

You may also want to refer to a recent CP article
Swanky encryption/decryption in C#[^]

You will note that the implmentation save encrypted data to file, but then read out the encrypted data to buffer, decrypt to buffer and then save the decrypted data back to file. Note that the saved decrypted file is the same length as the original file. But the encrypted file is always slightly larger ( file length is a multiple of block size ).


这篇关于DES加密问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆