News  [SoftwareSite

Latest News
Older News
RSS Feed
 
Complete Projects
Useful Classes
Top Downloads
Message Board
AllAPI.net
 
Send Comments
Software License
Mentalis.org Buttons
Donate
 
Forums -> Security Library Forum
 
Asymmetric Data Encryption  
by Jim Hunt [jimh at netwasp dot com]
posted on 2005/03/28

Hi

I want to encrypt a string of data on my server using a private key and then subsequently decrypt the data on a end user's client machine using the corresponding public key. I want to use the following code:

Server Code:

Certificate cert = Certificate.CreateFromPfxFile("MyPrivateCertFile.pfx", "password");
RSA public_key = cert.PublicKey;
RSAPKCS1KeyExchangeFormatter kef = new RSAPKCS1KeyExchangeFormatter(public_key);
byte[] encrypted = kef.CreateKeyExchange(data);

client Code:

Certificate cert = Certificate.CreateFromCerFile("MyPublicCertFile.cer");
RSA private_key = cert.PrivateKey;
RSAPKCS1KeyExchangeDeformatter ked = new RSAPKCS1KeyExchangeDeformatter(private_key);
byte[] decrypted = ked.DecryptKeyExchange(encrypted);

The server code appears to encrypt correctly but the client code throws an exception of "Bad Key". The only was this works is is I encrypt with the public key and decrypt with the provate key. Obviously I don't want to have the provate key sttored on the end user's machine so how can I encrypt on the server using the private key and then subsequently decrypt on the client's machine using the public key?

Thanks

Jim

by Jim Hunt [jimh at netwasp dot com]
posted on 2005/03/28

Sorry, ignore the last post I put in the wrong sample code - this one demonstrates what I'm trying to do:

Hi

I want to encrypt a string of data on my server using a private key and then subsequently decrypt the data on a end user's client machine using the corresponding public key. I want to use the following code:

Server Code:

Certificate cert = Certificate.CreateFromPfxFile("MyPrivateCertFile.pfx", "password");
RSA private_key = cert.PrivateKey;
RSAPKCS1KeyExchangeFormatter kef = new RSAPKCS1KeyExchangeFormatter(private_key);
byte[] encrypted = kef.CreateKeyExchange(data);

client Code:

Certificate cert = Certificate.CreateFromCerFile("MyPublicCertFile.cer");
RSA public_key = cert.PublicKey;
RSAPKCS1KeyExchangeDeformatter ked = new RSAPKCS1KeyExchangeDeformatter(public_key);
byte[] decrypted = ked.DecryptKeyExchange(encrypted);

The server code appears to encrypt correctly but the client code throws an exception of "Bad Key". The only was this works is is I encrypt with the public key and decrypt with the provate key. Obviously I don't want to have the provate key sttored on the end user's machine so how can I encrypt on the server using the private key and then subsequently decrypt on the client's machine using the public key?

Thanks

Jim

by Pieter Philippaerts [Pieter at mentalis dot org]
posted on 2005/03/28

You can't encrypt with the private key and decrypt with the public key -- this is a limitation of asymmetric cryptography.

 

Copyright © 2002-2007, The Mentalis.org Team. All rights reserved.
This site is located at http://www.mentalis.org/
Send comments to the webmaster.