【发布时间】:2015-04-17 10:27:01
【问题描述】:
我正在寻求一些帮助,以进行 AES 256 位加密,模式为 CBC,IV 为(16 字节)00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00。
基本上我想复制在this link 上所做的事情。所有的键都是十六进制的。以下是我从其他链接复制的内容。
以下解决方案中的几个问题:
- 如何使 IV 键全为零?
- 如何判断我的所有密钥都是十六进制的,而输出也必须是十六进制的?
public static String Encrypt(String plainText, String key)
{
var plainBytes = Encoding.UTF8.GetBytes(plainText);
return Convert.ToBase64String(Encrypt(plainBytes, GetRijndaelManaged(key)));
}
static RijndaelManaged GetRijndaelManaged(String secretKey)
{
var keyBytes = new byte[16];
var secretKeyBytes = Encoding.ASCII.GetBytes(secretKey);
Array.Copy(secretKeyBytes, keyBytes, Math.Min(keyBytes.Length, secretKeyBytes.Length));
return new RijndaelManaged
{
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7,
KeySize = 128,
BlockSize = 128,
Key = keyBytes,
IV = keyBytes
};
}
static byte[] Encrypt(byte[] plainBytes, RijndaelManaged rijndaelManaged)
{
return rijndaelManaged.CreateEncryptor()
.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
}
【问题讨论】:
-
你的意思是
IV = new byte[16]和KeySize = 256? -
是的,先生,它的值必须全为零
-
是的,字节数组的默认值全为零:stackoverflow.com/questions/22506274/…
-
是的,密钥大小是 256 位
-
这是我的钥匙 d651b30f-9c4c-4be0-83b8-2d7816256f64
标签: c# encryption cryptography aes