static byte[] keyBytes = new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1
};
static byte[] iv = new byte[] { 1, 1, 1, 1 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
static SymmetricAlgorithm getKey()
{
RijndaelManaged key = new RijndaelManaged();
key.Key = keyBytes;
key.IV = iv;
return key;
}
static string Encrypt(string PlainText)
{
SymmetricAlgorithm key = getKey();
MemoryStream ms = new MemoryStream();
CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(encStream);
sw.WriteLine(PlainText);
sw.Close();
encStream.Close();
byte[] buffer = ms.ToArray();
ms.Close();
return Convert.ToBase64String(buffer);
}
static string Decrypt(string encrypted)
{
SymmetricAlgorithm key = getKey();
byte[] CypherText = Convert.FromBase64String(encrypted);
MemoryStream ms = new MemoryStream(CypherText);
CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(encStream);
string val = sr.ReadLine();
sr.Close();
encStream.Close();
ms.Close();
return val;
}
显然 key 和 iv 已被更改为全“一”以保护有罪者。
我在这里尝试了其他几篇 SO 文章,但无济于事。
我想我会继续展示我正在尝试使用的 ruby openssl 代码:
def Crypt.decrypt(encrypted_data, key, iv, cipher_type)
aes = OpenSSL::Cipher::Cipher.new(cipher_type)
aes.decrypt
#aes.padding = 1
aes.key = key
aes.iv = iv if iv != nil
aes.update(encrypted_data) + aes.final
end