3
我收到'解密數據的長度無效'。嘗試解密字符串時出錯。我在本網站上查看了一些其他對此錯誤的引用,並嘗試了一些在那裏找到的建議,但迄今爲止沒有任何工作。另一個'要解密的數據長度無效'。錯誤
我確定我錯過了一些基本的東西,但我看不到它是什麼。
我在加密和解密時使用了相同的密鑰和IV。我在加密和解密時添加了FlushFinalBlock()
調用。我甚至試圖設置encryptStream.Position = 0
,但是會拋出ObjectDisposedException
。
我創建了一個控制檯應用程序來說明問題。該代碼的全部內容如下:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Crypto
{
class Program
{
static void Main(string[] args)
{
_CryptoString = AESStringEncryption(CRYPTO_STRING);
_CryptoString = AESStringDecryption(_CryptoString);
}
private const string CRYPTO_STRING = "The quick brown fox jumped over the lazy dog.";
private static byte[] _KY = { 47, 53, 94, 65, 243, 197, 42, 80, 125, 255, 144, 41, 130, 76, 2, 142, 43, 1, 120, 124, 255, 248, 232, 139, 170, 42, 243, 52, 4, 17, 60, 174 };
private static byte[] _VI = { 68, 42, 157, 47, 99, 8, 174, 169, 119, 255, 144, 218, 8, 30, 60, 42 };
private static string _CryptoString;
/// <summary>
/// Applies .NET Framework AES (Advanced Encryption Standard) Level encryption to the provided string parameter.
/// Utilizies the UTF8 encoding stardard for the conversion from string to byte[].
/// </summary>
public static string AESStringEncryption(string unencryptedString)
{
byte[] unencryptedBytes = Encoding.UTF8.GetBytes(unencryptedString);
byte[] encryptedBytes = AESByteArrayEncryption(unencryptedBytes);
string encryptedString = Encoding.UTF8.GetString(encryptedBytes);
return encryptedString;
}
/// <summary>
/// Applies .NET Framework AES (Advanced Encryption Standard) Level decryption to the provided string parameter.
/// Utilizies the UTF8 encoding stardard for the conversion from byte[] to string.
/// </summary>
public static string AESStringDecryption(string encryptedString)
{
byte[] encryptedBytes = Encoding.UTF8.GetBytes(encryptedString);
byte[] decryptedBytes = AESByteArrayDecryption(encryptedBytes);
string decryptedString = Encoding.UTF8.GetString(decryptedBytes);
return decryptedString;
}
/// <summary>
/// Applies .NET Framework AES (Advanced Encryption Standard) Level encryption to the provided byte array parameter.
/// </summary>
public static byte[] AESByteArrayEncryption(byte[] unencryptedBytes)
{
using (var rm = new RijndaelManaged())
{
var encryptor = rm.CreateEncryptor(_KY, _VI);
using (var encryptStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(encryptStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(unencryptedBytes, 0, unencryptedBytes.Length);
cryptoStream.FlushFinalBlock();
}
//encryptStream.Position = 0;
byte[] encryptedBytes = encryptStream.ToArray();
return encryptedBytes;
}
}
}
/// <summary>
/// Applies .NET Framework AES (Advanced Encryption Standard) Level decryption to the provided byte array parameter.
/// </summary>
public static byte[] AESByteArrayDecryption(byte[] encryptedBytes)
{
using (var rm = new RijndaelManaged())
{
var decryptor = rm.CreateDecryptor(_KY, _VI);
using (var decryptStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(decryptStream, decryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(encryptedBytes, 0, encryptedBytes.Length);
cryptoStream.FlushFinalBlock();
}
//decryptStream.Position = 0;
byte[] decryptedBytes = decryptStream.ToArray();
return decryptedBytes;
}
}
}
}
}
工作太棒了!謝謝。我知道這一定很簡單。 – 2011-04-22 18:57:52
感謝它的工作,非常簡單 – 2016-12-30 08:40:22
工作很好..謝謝@ Weltonv3.56 – 2017-05-03 10:44:23