我正在尝试编写一个 C# 方法,该方法将吐出与 openssl 二进制文件相同的加密字符串(Base64),但我花了很多时间来匹配。
大量的终端输出和 C# 要遵循...:P
我们将使用非常令人兴奋的例子,用密码“123”加密字符串“a”。
当我提供静态盐和密码时,首先是 openssl(这是命令的理想运行方式,也是我希望 C# 输出匹配的内容):
dev@magoo ~# echo -n a | openssl enc -aes-128-cbc -S cc77e2a591358a1c -pass pass:123 -a -p
salt=CC77E2A591358A1C
key=7B2AD689138A44AD32297BBAAA5B0EEE
iv =EC4F0416B2E9A9B2FEEF2E66FF982159
U2FsdGVkX1/Md+KlkTWKHOtnt1ftHSKyWiz6GxkBVck=
第二个是openssl,当我提供我的静态盐时,以及从该盐派生的密钥和iv(来自第一个命令的输出的C + P'd)但没有密码(因为即使是文档也说这不是-好主意):
dev@magoo ~# echo -n a | openssl enc -aes-128-cbc -S cc77e2a591358a1c -K 7b2ad689138a44ad32297bbaaa5b0eee -iv ec4f0416b2e9a9b2feef2e66ff982159 -a -p
salt=E85778B7FFFFFFFF
key=7B2AD689138A44AD32297BBAAA5B0EEE
iv =EC4F0416B2E9A9B2FEEF2E66FF982159
62e3V+0dIrJaLPobGQFVyQ==
这让我觉得很奇怪。将第一个命令中“调试”输出(-p 参数)中的键和 iv 值添加到相同的盐,我不知何故得到了不同的盐!(CC77E2A591358A1C 与 E85778B7FFFFFFFF [这里的 0xff 的 4 个字节似乎很有趣])。
第三是我的应用程序的输出:
dev@magoo ~# mono aestest.exe "a" "123"
==> INPUT : a
==> SECRET : 123
==> SALT : cc77e2a591358a1c
==> KEY : 7b2ad689138a44ad32297bbaaa5b0eee
==> IV : ec4f0416b2e9a9b2feef2e66ff982159
==> ENCRYPTED : 62e3V+0dIrJaLPobGQFVyQ==
因此,当我自己手动指定密钥和 IV(然后以某种方式生成不同的盐)时,C# 匹配 openssl 命令的输出,但这似乎是错误的。在我看来,C# 应用程序的输出应该与 OpenSSL 的第一组输出匹配,不是吗?
C#代码:
public static string EncryptString(string plainText, string password)
{
byte[] salt = Encryption.GetStaticSalt();
byte[] key, iv;
Encryption.DeriveKeyAndIV(password, salt, out key, out iv);
var amAes = new AesManaged();
amAes.Mode = CipherMode.CBC;
amAes.KeySize = 128;
amAes.BlockSize = 128;
amAes.Key = key;
amAes.IV = iv;
var icTransformer = amAes.CreateEncryptor();
var msTemp = new MemoryStream();
var csEncrypt = new CryptoStream(msTemp, icTransformer, CryptoStreamMode.Write);
var sw = new StreamWriter(csEncrypt);
sw.Write(plainText);
sw.Close();
sw.Dispose();
csEncrypt.Clear();
csEncrypt.Dispose();
byte[] bResult = msTemp.ToArray();
string sResult = Convert.ToBase64String(bResult);
if (System.Diagnostics.Debugger.IsAttached)
{
string debugDetails = "";
debugDetails += "==> INPUT : " + plainText + Environment.NewLine;
debugDetails += "==> SECRET : " + password + Environment.NewLine;
debugDetails += "==> SALT : " + Encryption.ByteArrayToHexString(salt) + Environment.NewLine;
debugDetails += "==> KEY : " + Encryption.ByteArrayToHexString(amAes.Key) + " (" + amAes.KeySize.ToString() + ")" + Environment.NewLine;
debugDetails += "==> IV : " + Encryption.ByteArrayToHexString(amAes.IV) + Environment.NewLine;
debugDetails += "==> ENCRYPTED : " + sResult;
Console.WriteLine(debugDetails);
}
return sResult;
}
private static string ByteArrayToHexString(byte[] bytes)
{
StringBuilder sbHex = new StringBuilder();
foreach (byte b in bytes)
sbHex.AppendFormat("{0:x2}", b);
return sbHex.ToString();
}
public static byte[] GetStaticSalt()
{
// Just random bytes.
return new byte[]
{
0xcc,
0x77,
0xe2,
0xa5,
0x91,
0x35,
0x8a,
0x1c
};
}
// largely hijacked from http://stackoverflow.com/a/8011654/97423
public static void DeriveKeyAndIV(string password, byte[] bSalt, out byte[] bKey, out byte[] bIV)
{
int keyLen = 16;
int ivLen = 16;
byte[] bPassword = Encoding.UTF8.GetBytes(password);
using (var md5Gen = MD5.Create())
{
List<byte> lstHashes = new List<byte>(keyLen + ivLen);
byte[] currHash = new byte[0];
int preHashLength = bPassword.Length + bSalt.Length;
byte[] preHash = new byte[preHashLength];
Buffer.BlockCopy(bPassword, 0, preHash, 0, bPassword.Length);
Buffer.BlockCopy(bSalt, 0, preHash, bPassword.Length, bSalt.Length);
currHash = md5Gen.ComputeHash(preHash);
lstHashes.AddRange(currHash);
while (lstHashes.Count < (keyLen + ivLen))
{
preHashLength = currHash.Length + password.Length + bSalt.Length;
preHash = new byte[preHashLength];
Buffer.BlockCopy(currHash, 0, preHash, 0, currHash.Length);
Buffer.BlockCopy(bPassword, 0, preHash, currHash.Length, password.Length);
Buffer.BlockCopy(bSalt, 0, preHash, currHash.Length + password.Length, bSalt.Length);
currHash = md5Gen.ComputeHash(preHash);
lstHashes.AddRange(currHash);
}
bKey = new byte[keyLen];
bIV = new byte[ivLen];
lstHashes.CopyTo(0, bKey, 0, keyLen);
lstHashes.CopyTo(keyLen, bIV, 0, ivLen);
}
}
我在这里错过了一些非常明显的东西,还是更微妙的东西?我环顾四周,看到了很多关于 C#、OpenSSL 和 AES 的信息,但对这个特定问题一无所知……所以,停顿了吗?;)