클라이언트/서버 응용 프로그램이 있습니다. 서버 및 클라이언트 통신은 암호화됩니다. 서버는 암호화 된 메시지를 클라이언트와 클라이언트에게 보내고 메시지를 해독합니다. 클라이언트 메시지와 동일합니다. 큰 데이터를 클라이언트에 보내거나 클라이언트가 서버로 큰 데이터를 보내는 경우 "해독 할 데이터의 길이가 잘못되었습니다."라는 오류가 발생합니다. 데이터 전송이 작은 경우 문제가 없습니다. 데이터 길이를 암호화하거나 해독하는 데 제한이 있습니까? 여기 "해독 할 데이터의 길이가 유효하지 않습니다"오류
내 코드입니다 :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