0

これに基づく: http://www.superstarcoders.com/blogs/posts/symmetric-encryption-in-c-sharp.aspx

バイト配列の暗号化/復号化を書きました:

public static byte[] EncryptFile(string password, byte[] bytes, string salt)
    {
        using (RijndaelManaged aesEncryption = new RijndaelManaged())
        {
            DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
            byte[] rgbKey = rgb.GetBytes(aesEncryption.KeySize >> 3);
            byte[] rgbIV = rgb.GetBytes(aesEncryption.BlockSize >> 3);
            aesEncryption.KeySize = 256;
            aesEncryption.Mode = CipherMode.CBC;
            aesEncryption.Padding = PaddingMode.PKCS7;
            aesEncryption.IV = rgbIV;
            aesEncryption.Key = rgbKey;
            using (ICryptoTransform crypto = aesEncryption.CreateEncryptor())
            {
                return crypto.TransformFinalBlock(bytes, 0, bytes.Length);
            }
        }
    }

    public static byte[] DecryptFile(string password, byte[] bytes, string salt)
    {
        using (RijndaelManaged aesEncryption = new RijndaelManaged())
        {
            DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
            byte[] rgbKey = rgb.GetBytes(aesEncryption.KeySize >> 3);
            byte[] rgbIV = rgb.GetBytes(aesEncryption.BlockSize >> 3);
            aesEncryption.KeySize = 256;
            aesEncryption.Mode = CipherMode.CBC;
            aesEncryption.Padding = PaddingMode.PKCS7;
            aesEncryption.IV = rgbIV;
            aesEncryption.Key = rgbKey;
            using (ICryptoTransform crypto = aesEncryption.CreateDecryptor())
            {
                return crypto.TransformFinalBlock(bytes, 0, bytes.Length);
            }
        }
    }

しかし、IV とキーを計算するときは、代わりに SHA256 を使用する必要がありRfc2898DeriveBytesますか?

4

1 に答える 1

2

いいえ、SHA256は使用しないでください。SHA256は、Rfc2898DeriveBytesパスワードベースの鍵導出機能を実装するために使用されるハッシュ関数です。

ハッシュ関数を使用してデータを検証できます。ここで、Rfc2898DeriveBytesは特にキーを生成するために使用されます。

msdnRfc2898DeriveBytesおよびSHA256経由

于 2012-09-28T12:51:28.600 に答える