RSACryptoServiceProvider, SSLStream (OpenSSL) - Encrypt, Decrypt

0

After server authentication from the certificate generated with openssl.

sslStream.AuthenticateAsClient(serverName); Home The encryption of the data by the client 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 through sslStream as follows to the server:

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


On the server, you receive the information as follows:

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


With a specific method that I have created myself, I clean this buffer so that it only has the value that is sent from the client side, and it generates 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 an exception of type CryptographicException saying "Dados inválidos"

And the question is, do I have to have the public key that I generated on the client side for when it's decrypted? Because of what I noticed, it generates this error from the moment my privatekey is different from the publickey thus not decrypting the information.

    
asked by anonymous 23.06.2014 / 15:00

1 answer

0

Need to make the exchange of keys, after the authentication of both parts, fractionating the public key in 2 parts (exponent and modulus), which are the only values inside the public key.

    
30.06.2014 / 15:25