这是加密方法:
public static byte[] Encrypt(byte[] plaintext, byte[] key)
{
using (var aes = Aes.Create())
{
aes.BlockSize = 128;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.None;
var iv = new byte[16];
for (int i = 0; i < iv.Length; i++)
iv[i] = 0;
aes.IV = iv;
var encryptor = aes.CreateEncryptor(key, aes.IV);
using(var target = new MemoryStream())
using (var cs = new CryptoStream(target, encryptor, CryptoStreamMode.Write))
{
using (var source = new StreamWriter(cs))
source.Write(plaintext);
return target.ToArray();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我是怎么称呼的:
var key = new byte[16] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
var plaintext = new byte[16] { 128, 0, 112, 0, 96, 0, 80, 0, 64, 0, 48, 0, 32, 0, 16, 0 };
Run Code Online (Sandbox Code Playgroud)
但它一直在source.Write(明文)上抛出异常,说它不是一个完整的块?我正在使用一个16字节/ 128位阵列,块大小设置为128.我不明白什么是错的?
另外,只是为了阻止任何有关ECB不好的建议,这不是为了生产,我只是在玩耍.
StreamWriter将UTF8文本字符写入流.
你正在写plaintext.ToString()为密文的文本.
返回"System.Byte[]",不会转换为16字节的UTF8.
| 归档时间: |
|
| 查看次数: |
23428 次 |
| 最近记录: |