0

我正在尝试在 C# 中重新创建此 openssl 命令:

openssl enc –e –aes-256-cbc –k SecretPhrase1234 –in profile.xml –out profile.cfg

然后,该加密文件将由设备加载,该过程描述如下:

小写 -k 在密钥之前,可以是任何纯文本短语,用于生成随机 64 位盐。然后,结合使用 –k 参数指定的秘密,它得出一个随机的 128 位初始向量和实际的 256 位加密密钥。

因此,在我的 C# 应用程序中,我需要使用“SecretPhrase1234”创建一个随机的 64 位盐。然后我需要导出一个 128 位的 IV 和一个 256 位的密钥。该设备已经加载了密码短语。

这是我的代码:

AesManaged aes = new AesManaged();

// Encrypt the string to an array of bytes.
aes.KeySize = 256;
aes.BlockSize = 128;
aes.Mode = CipherMode.CBC;

Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes("SecretPhrase1234", 8);
byte[] SALT = rfc.Salt;
PasswordDeriveBytes pdb = new PasswordDeriveBytes("SecretPhrase1234", SALT);               
byte[] IV = rfc.GetBytes(aes.BlockSize/8);
//The next line doesn't work
byte[] KEY = pdb.CryptDeriveKey("AES", "SHA1", aes.KeySize, IV); 
aes.Key = KEY;
aes.IV = IV;

byte[] encrypted = AESEncryption.EncryptStringToBytes(plainConfig, 
                                                    aes.Key, aes.IV);             
tw.WriteLine(Encoding.ASCII.GetString(encrypted));
tw.Close();
4

1 回答 1

0

我找到了一个非常适合我需要的 OPENSSL 的 .NET 实现。在这里: openssl 仅使用 .NET 类

于 2012-07-20T20:31:21.477 回答