我有一个客户端/服务器应用程序。服务器和客户端通信已加密。服务器发送加密的消息给客户端和客户端解密消息并获取它。与“客户”消息相同。如果我向客户端发送大数据或客户端向服务器发送大数据,“我得到要解密的数据的长度无效”错误。如果发送数据的长度很小,没有问题。数据长度的加密或解密有没有限制?我的“解密数据的长度无效”错误
这里是我的代码:
static byte[] Encrypt(byte[] plaintext, byte[] key, byte[] IV)
{
RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.Padding = PaddingMode.PKCS7;
ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
csEncrypt.Write(plaintext, 0, plaintext.Length);
csEncrypt.FlushFinalBlock();
return msEncrypt.ToArray();
}
public static string Encrypt(string plainText, string password)
{
byte[] byteDizi = Encoding.Unicode.GetBytes(plaintext);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
byte[] sifreliV = Encrypt(byteDizi,
pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(sifreliV);
}
// Dekriptolama bir parola ve IV kullanarak
static byte[] Decrypt(byte[] encryptedData,
byte[] Key, byte[] IV)
{
RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.Padding = PaddingMode.PKCS7;
ICryptoTransform decryptor = myRijndael.CreateDecryptor(Key, IV);
MemoryStream msDecrypt = new MemoryStream(encryptedData);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
byte[] fromEncrypt = new byte[encryptedData.Length];
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
return fromEncrypt;
}
public static string Decrypt(string encryptedData, string password)
{
byte[] encryptedByte = Convert.FromBase64String(encryptedData);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65,
0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
byte[] DecryptedData = Decrypt(encryptedByte,
pdb.GetBytes(32), pdb.GetBytes(16));
return Encoding.Unicode.GetString(DecryptedData);
}
}
在你的加密方法中,记录byte []'数组和Base64'String'的长度。在你的解密方法中,对传入的Base64'String'和生成的'byte []'数组的长度做一个类似的记录。这些匹配是否正确?特别是对于你说的大文件造成问题。 – rossum