c# - RSACryptoServiceProvider, SSLStream(OpenSSL) - Encrypt, Decrypt -



c# - RSACryptoServiceProvider, SSLStream(OpenSSL) - Encrypt, Decrypt -

after server authentication, using openssl certificate.:

sslstream.authenticateasclient(servername);

the info encryption, on client side made code:

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);

the info goes through sslstream, this:

sslstream.write(messagersa); sslstream.flush();

, server going receive data.:

byte[] bytes = new byte[2048]; bytes = sslstream.read(buffer, 0, buffer.length);

i've created method clean buffer, because "2048" size, i'm going have lot of "0" values don't need, só method clean these zeros don't need.

rsacryptoserviceprovider asr = new rsacryptoserviceprovider(2048); var privatekey = asr.exportparameters(true); var csp = new rsacryptoserviceprovider(); csp.importparameters(privatekey); decryptedmessage = fixbuffer(buffer);//method cleans buffer, , homecoming valid array, info want. decryptedmessage= csp.decrypt(decryptedmessage, false);

when tries decrypt, cryptographicexception, , message invalid data. , question is,do need same public private key utilize on client side decrypt data? if yes, how can pass key server side, , decrypt info correctly?

to decrypt data, need utilize private key corresponds public key used encrypt it. stands, you're generating new (different) key on server, not usable decrypt data. how convey right key server, reply don't - server should generate key, , send public part (only) client, can utilize encrypt message.

this beingness said, seems rather unnecessary encrypting info @ all, given communication occurring on encrypted ssl connection.

c# cryptography rsa

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -