학교용 프로젝트를 진행하고 있습니다. TcpClient 및 TcpListener와 서버 클라이언트 통신을하고 있습니다. 처음에는 서버에서 AES 키를 만든 다음 RSA를 사용하여 클라이언트에 보냅니다. 나는 여기에 아무런 문제가 없다. 그러나 클라이언트에서 문자열을 암호화 한 다음 서버에서 encrpyt하려고하면 예외가 발생합니다.AesCryptoServiceProvider가 암호화 예외를 throw합니다.
- 패딩이 잘못 제거 할 수 없습니다
서버 코드 :
Byte[] bytes = new Byte[1024];
String data = null;
clientStream.Read(bytes, 0, bytes.Length); // preberemo iz streama
//Array.Clear(bytes, 0, bytes.Length);
// string temp = Encoding.UTF8.GetString(bytes11);
//temp = temp.Replace("\0", "");
//yte[] bytes = Encoding.UTF8.GetBytes(temp);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, aes.CreateDecryptor(aes.Key,aes.IV), CryptoStreamMode.Write))
{
//StreamWriter swEncrypt = new StreamWriter(csEncrypt);
csEncrypt.Write(bytes, 0, bytes.Length);
}
byte[] encrypted = msEncrypt.ToArray();
string enc = Encoding.UTF8.GetString(encrypted, 0, encrypted.Length);
}
클라이언트 코드 :
byte[] messg = Encoding.UTF8.GetBytes(messig);
// dobimo client stream
string enc = null;
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, aes.CreateEncryptor(aes.Key, aes.IV), CryptoStreamMode.Write))
{
csEncrypt.Write(messg,0,messg.Length);
}
byte[] encrypted = msEncrypt.ToArray();
enc = Encoding.UTF8.GetString(encrypted, 0, encrypted.Length);
}
byte[] data = new byte[1024];
data = Encoding.UTF8.GetBytes(enc);
//CryptoStream CryptStream = new CryptoStream(stream,aes.CreateEncryptor(aes.Key, aes.IV),CryptoStreamMode.Write);
stream.Write(data, 0, data.Length); // pošljem sporočilo
msEncrypt을 사용하려고 할 때 서버에서 예외가 .ToArray();
그러나 내가 dinamically Byte []를 할당하거나 모든 null 값을 제거하면 "입력 데이터가 완전한 블록이 아닙니다"라는 예외가 발생합니다.
나는 downvote하지 않을 것이다 (아직), 나의 대답을 읽고 당신이이 대답으로 무엇을 결정하십시오. –