Rsacryptoserviceprovider, Sslstream(Openssl) - Encrypt, Decrypt

Asked

Viewed 98 times

0

After server authentication from the certificate generated with openssl.

sslStream.AuthenticateAsClient(serverName);

Client data encryption is done as follows:

        string messsage = "teste123.<EOF>";

        byte[] messageRSA = ConvertByte.GetBytes(messsage);

        RSACryptoServiceProvider asr = new RSACryptoServiceProvider(2048);

        var publicKey = asr.ExportParameters(false);

        var csp = new RSACryptoServiceProvider();

        csp.ImportParameters(publicKey);

        messageRSA = csp.Encrypt(messageRSA, false);


And the information is sent via sslStream as follows to the server:

sslStream.Write(messageRSA);
sslStream.Flush();


Already on the server, receive the information as follows:

byte[] bytes = new byte[2048];
bytes = sslStream.Read(buffer, 0, buffer.Length);


With a specific method that I created myself, I clean this buffer so that it only has the value that is sent from the client side, and I use a private key to decrypt the information as follows:

RSACryptoServiceProvider asr = new RSACryptoServiceProvider(2048);
var privateKey = asr.ExportParameters(true);
var csp = new RSACryptoServiceProvider();
csp.ImportParameters(privateKey);
decryptedMessage = FixBuffer(buffer);//método que limpa meu buffer e retorna um array de byte válido
decryptedMessage= csp.Decrypt(decryptedMessage, false);


When trying to decrypt, it returns me an exception of type CryptographicException saying "Dados inválidos"

And the question is this, I have to have the public key that I generated on the client side for when it’s decrypting?
Because from what I understand, it generates that mistake from the moment that my privatekey is different from publickey so as not to decrypt the information.

1 answer

0


Necessary to make the key exchange, after authentication of both parts, fractionating the public key into 2 parts (Exponent and modulus), which are the only values within the public key.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.