R
Rafael Delpiano
I'm trying to self-learn RSA Encryption/Decryption in C#, and I arrived to the following code (just as a test of how this stuff works):
<code>
X509Certificate2 c = new X509Certificate2("certificado.pfx", "prueba");
//Create values to store encrypted data.
byte[] Encrypted;
//Encrypt the symmetric text.
Encrypted = ((RSACryptoServiceProvider)c.PublicKey.Key).Encrypt(s2b(entrada.Text), false);
byte[] aux = ((RSACryptoServiceProvider)c.PrivateKey).Decrypt(Encrypted, false);
salida.Text = b2s(aux);
</code>
Where:
- certificado.pfx is a standard certificate file with both the private and public keys
- entrada and salida are textboxes
- s2b converts a string into a byte array and b2s does the opposite
The point is that the Decrypt method in the second to last line returns an empty byte[0] array. I've checked that Encrypt returns what seems to be valid byte[128] results.
What may be wrong? Is it something with the way of importing the certificate?
EggHeadCafe - Software Developer Portal of Choice
New .NET Technology Preference Poll
http://www.eggheadcafe.com/tutorial...52-244e7462fc1e/new-net-technology-prefe.aspx
<code>
X509Certificate2 c = new X509Certificate2("certificado.pfx", "prueba");
//Create values to store encrypted data.
byte[] Encrypted;
//Encrypt the symmetric text.
Encrypted = ((RSACryptoServiceProvider)c.PublicKey.Key).Encrypt(s2b(entrada.Text), false);
byte[] aux = ((RSACryptoServiceProvider)c.PrivateKey).Decrypt(Encrypted, false);
salida.Text = b2s(aux);
</code>
Where:
- certificado.pfx is a standard certificate file with both the private and public keys
- entrada and salida are textboxes
- s2b converts a string into a byte array and b2s does the opposite
The point is that the Decrypt method in the second to last line returns an empty byte[0] array. I've checked that Encrypt returns what seems to be valid byte[128] results.
What may be wrong? Is it something with the way of importing the certificate?
EggHeadCafe - Software Developer Portal of Choice
New .NET Technology Preference Poll
http://www.eggheadcafe.com/tutorial...52-244e7462fc1e/new-net-technology-prefe.aspx