[英]AES to encrypt a single block shows error
当在程序下面运行它显示错误。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("Hello, world!");
byte[] plainText = UTF8Encoding.UTF8.GetBytes("textt");
byte[] key = UTF8Encoding.UTF8.GetBytes("1234567890123456");
AES_encrypt_block(plainText,key);
}
private static byte[] AES_encrypt_block(byte[] plainText, byte[] Key)
{
byte[] output_buffer = new byte[plainText.Length];
using (AesManaged aesAlg = new AesManaged())
{
//If CBC, must initialize IV = O_{128}
//aesAlg.Mode = CipherMode.CBC;
//aesAlg.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
aesAlg.Mode = CipherMode.ECB;
aesAlg.BlockSize = 128;
aesAlg.KeySize = 128;
aesAlg.Padding = PaddingMode.None;
aesAlg.Key = Key;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
encryptor.TransformBlock(plainText, 0, plainText.Length, output_buffer, 0);
}
return output_buffer;
}
}
}
错误
用户代码中的异常:
System.ArgumentException:值无效。 at> System.Security.Cryptography.RijndaelManagedTransform.TransformBlock(Byte []> inputBuffer,Int32 inputOffset,Int32 inputCount,Byte [] outputBuffer,Int32> outputOffset)at Rextester.Program.AES_encrypt_block(Byte [] plainText,Byte [] Key)在Rextester.Program.Main(String [] args)“
你可以这样做(例子)。 但我建议使用CBC而不是ECB。 并且,由于填充,解密的块将始终具有块的大小,即使原始内容(5字节字符串)更短。 因此,在创建字符串时,请在实际内容之前插入字符串长度(第2个示例)。
加密/解密ECB:
private static AesManaged Create(byte[] key)
{
var aes = new AesManaged();
aes.Mode = CipherMode.ECB;
aes.BlockSize = 128;
aes.KeySize = 128;
aes.Padding = PaddingMode.Zeros;
aes.Key = key;
return aes;
}
private static byte[] AES_encrypt_block(byte[] plainText, byte[] key)
{
using (AesManaged aes = Create(key))
{
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
return encryptor.TransformFinalBlock(plainText, 0, plainText.Length);
}
}
private static byte[] AES_decrypt_block(byte[] plainText, byte[] key)
{
using (AesManaged aesAlg = Create(key))
{
var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
return decryptor.TransformFinalBlock(plainText, 0, plainText.Length);
}
}
串到字节:
private static byte[] StringToBytes(string s)
{
var length = BitConverter.GetBytes(s.Length);
var raw = Encoding.UTF8.GetBytes(s);
var full = new byte[sizeof(int) + raw.Length];
Array.Copy(length, full, sizeof(int));
Array.Copy(raw, 0, full, sizeof(int), raw.Length);
return full;
}
private static string BytesToString(byte[] input)
{
var length = BitConverter.ToInt32(input, 0);
return Encoding.UTF8.GetString(input, sizeof(int), length);
}
验证:
var key = UTF8Encoding.UTF8.GetBytes("1234567890123456");
var plainBytes = StringToBytes("textt");
var encryptedBytes = AES_encrypt_block(plainBytes, key);
var decryptedBytes = AES_decrypt_block(encryptedBytes, key);
var decryptedString = BytesToString(decryptedBytes);
你应该改变它;
encryptor.TransformBlock(plainText, 0, plainText.Length, output_buffer, 0);
至
var output = encryptor.TransformFinalBlock(plainText, 0, plainText.Length);
你应该指定PaddingMode
;
aesAlg.Padding = PaddingMode.PKCS7;
整个方法看起来像;
private static byte[] AES_encrypt_block(byte[] plainText, byte[] Key)
{
using (AesManaged aesAlg = new AesManaged())
{
aesAlg.Mode = CipherMode.ECB;
aesAlg.BlockSize = 128;
aesAlg.KeySize = 128;
aesAlg.Padding = PaddingMode.PKCS7;
aesAlg.Key = Key;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
return encryptor.TransformFinalBlock(plainText, 0, plainText.Length);
}
}
如果使用TransformBlock
,则输入文本长度应为16
。 因为它改变了每个块的加密。 因此,您应该使用TransformFinalBlock
而不是它。 此外,应为较短的消息数据块设置PaddingMode
。
编辑
执行解密 ;
private static byte[] AES_Decrypt_block(byte[] cipherText, byte[] Key)
{
using (AesManaged aesAlg = new AesManaged())
{
aesAlg.Mode = CipherMode.ECB;
aesAlg.BlockSize = 128;
aesAlg.KeySize = 128;
aesAlg.Padding = PaddingMode.PKCS7;
aesAlg.Key = Key;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
return decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length);
}
}
用法
byte[] plainText = UTF8Encoding.UTF8.GetBytes("textt");
byte[] key = UTF8Encoding.UTF8.GetBytes("1234567890123456");
var encrypted = AES_encrypt_block(plainText, key);
var decrypted = AES_Decrypt_block(encrypted, key);
Console.WriteLine(UTF8Encoding.UTF8.GetString(decrypted));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.