.NET中提供了名为AesManaged的类来实现AES加密,该类位于System.Security.Cryptography命名空间下。以下是在.NET中不使用IV进行AES加密的步骤:
using System.Security.Cryptography;
AesManaged aesManaged = new AesManaged();
aesManaged.KeySize = 128;
aesManaged.BlockSize = 128;
aesManaged.Mode = CipherMode.CBC;
aesManaged.Padding = PaddingMode.None;
aesManaged.GenerateKey();
string plainText = "Hello, World!";
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
byte[] cipherTextBytes = aesManaged.CreateEncryptor().TransformFinalBlock(plainTextBytes, 0, plainTextBytes.Length);
string cipherText = Convert.ToBase64String(cipherTextBytes);
string cipherText = "SGVsbG8sIHdvcmxkIQ==";
byte[] cipherTextBytes = Convert.FromBase64String(cipherText).Reverse().ToArray();
byte[] decryptedTextBytes = aesManaged.CreateDecryptor().TransformFinalBlock(cipherTextBytes, 0, cipherTextBytes.Length);
string decryptedText = Encoding.UTF8.GetString(decryptedTextBytes);
string decryptedText = "Hello, World!";
byte[] decryptedTextBytes = Encoding.UTF8.GetBytes(decryptedText);
byte[] cipherTextBytes = aesManaged.CreateDecryptor().TransformFinalBlock(decryptedTextBytes, 0, decryptedTextBytes.Length);
string cipherText = Convert.ToBase64String(cipherTextBytes);
byte[] iv = aesManaged.IV;
byte[] cipherTextBytesWithoutIV = cipherTextBytes.Skip(iv.Length).ToArray();
string cipherTextWithoutIV = Convert.ToBase64String(cipherTextBytesWithoutIV);
string decryptedText = "Hello, World!";
byte[] decryptedTextBytes = Encoding.UTF8.GetBytes(decryptedText);
byte[] cipherTextBytesWithoutIV = aesManaged.CreateDecryptor().TransformFinalBlock(decryptedTextBytes, 0, decryptedTextBytes.Length);
byte[] iv = cipherTextBytesWithoutIV.Take(aesManaged.IV.Length).ToArray();
byte[] cipherTextBytesWithIV = cipherTextBytesWithoutIV.Skip(iv.Length).ToArray();
string cipherTextWithIV = Convert.ToBase64String(cipherTextBytesWithIV);
string decryptedText = "Hello, World!";
byte[] decryptedTextBytes = Encoding.UTF
领取专属 10元无门槛券
手把手带您无忧上云