Encryption in C#


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
 
Ad

Advertisements

G

Gregory A. Beamer

Rafael Delpiano wrote in
What may be wrong? Is it something with the way of importing the
certificate?

Without knowing how you created keys, I can only guess what is wrong

In Windows, it is very common to have a PFX to send the public key and a
CER to install the private key. If you follow this methodology, you will
not have a private key in the PFX.

Does the PFX have the private key? One way to check is to decouple the
creation of the object from the method where you are decrypting. You can
then debug through by separating out the concerns and stepping through the
code.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
R

Rafael Delpiano

I exported the certificate from Windows repository taking care of exporting both private and public keys, thus having to password-protect it.

The private key seems to be ok (at least it has a length of 1024 when I invoke c.PrivateKey.KeySize)



Gregory A. Beamer wrote:

Without knowing how you created keys, I can only guess what is wrongIn
24-Nov-09

Without knowing how you created keys, I can only guess what is wron

In Windows, it is very common to have a PFX to send the public key and
CER to install the private key. If you follow this methodology, you wil
not have a private key in the PFX

Does the PFX have the private key? One way to check is to decouple th
creation of the object from the method where you are decrypting. You ca
then debug through by separating out the concerns and stepping through th
code

Peace and Grace

-
Gregory A. Beamer (MVP

Twitter: @gbworl
Blog: http://gregorybeamer.spaces.live.co

******************************************
| Think outside the box!
*******************************************

Previous Posts In This Thread:

EggHeadCafe - Software Developer Portal of Choice
Native DataSet Compression in VS.NET 2005
http://www.eggheadcafe.com/tutorial...6-e859f31baea0/native-dataset-compressio.aspx
 
R

Rafael Delpiano

It seems that the problem was Visual C# 2008 Express Edition's limitations. I copied the code into Visual Studio 2005 and it worked (!).

Pity that It didn't give any error messages, I spent several hours on that.



Rafael Delpiano wrote:

private key seems to be ok...
24-Nov-09

I exported the certificate from Windows repository taking care of exporting both private and public keys, thus having to password-protect it.

The private key seems to be ok (at least it has a length of 1024 when I invoke c.PrivateKey.KeySize)

Previous Posts In This Thread:

EggHeadCafe - Software Developer Portal of Choice
Visual Basic.NET and the .NET Platform [aPress]
http://www.eggheadcafe.com/tutorial...912-35144a12f26e/visual-basicnet-and-the.aspx
 
A

Arne Vajhøj

Rafael said:
It seems that the problem was Visual C# 2008 Express Edition's
limitations. I copied the code into Visual Studio 2005 and it worked (!).

That does not sound likely.

I have never seen in any language that the tool used to type
in the code effected how the code worked.

Something else changed.

Arne
 
G

Gregory A. Beamer

Rafael Delpiano wrote in
It seems that the problem was Visual C# 2008 Express Edition's
limitations. I copied the code into Visual Studio 2005 and it worked
(!).

Another one to add to the mix for why one should buy Visual Studio when
Express is free. Thanks for posting back.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Ad

Advertisements

G

Gregory A. Beamer

That does not sound likely.

I have never seen in any language that the tool used to type
in the code effected how the code worked.

Something else changed.

I am not sure I agree completely, as there are some limitations in Express
that can affect coding decisions. If he had tried a command line compile on
the bits and it worked on a solution file in VS, but not in Express,
however, I would agree completely.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top