private to public decrypt now working
- From: "Bob Bins" <deveng@xxxxxxxxx>
- Date: Thu, 17 May 2007 12:37:28 -0400
I am using the below sample for public private key encryption. And my problem is I can encrypt with the public key and decrypt with the private key but I can't encrypt with the private and decrypt with the public. I always get a BadKey error when I pass in the public key to decrypt.
Copy the below code in to a form and you will see. If you switch it to use the public key to encrypt and private to decrypt it works. I through it was supposed to work both ways?
//Orignal source from
http://pages.infinit.net/ctech/20031101-0151.html
private void Form1_Load(object sender, EventArgs e)
{
KeyGen();
FileStream fs = new FileStream("privkey.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
String rsaPrivKey = sr.ReadToEnd();
fs.Close();
// Now, create a new RSA key and import the parameters
RSACryptoServiceProvider rsaPrivate = new RSACryptoServiceProvider();
rsaPrivate.FromXmlString(rsaPrivKey);
byte[] encrypted = Encrypt(rsaPrivate,System.Text.Encoding.UTF8.GetBytes("This is an encrypted string."));
fs = new FileStream("pubkey.txt", FileMode.Open);
sr = new StreamReader(fs);
String rsaPubKey = sr.ReadToEnd();
fs.Close();
// Now, create a new RSA key and import the parameters
RSACryptoServiceProvider rsaPublic = new RSACryptoServiceProvider();
rsaPublic.FromXmlString(rsaPubKey);
byte[] Decrypted = Decrypt(rsaPublic, encrypted);
MessageBox.Show(System.Text.Encoding.UTF8.GetString(Decrypted));
}
public static void KeyGen()
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
// Save the public key info out to pubkey.txt
FileStream fs = new FileStream("pubkey.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(rsa.ToXmlString(false));
sw.Close();
// Save the priate key info out to privkey.txt
fs = new FileStream("privkey.txt", FileMode.Create);
sw = new StreamWriter(fs);
sw.Write(rsa.ToXmlString(true));
sw.Close();
}
//Pass in key that contains public only.
static byte[] Encrypt(RSA rsa, byte[] input)
{
// by default this will create a 128 bits AES (Rijndael) object
SymmetricAlgorithm sa = SymmetricAlgorithm.Create();
ICryptoTransform ct = sa.CreateEncryptor();
byte[] encrypt = ct.TransformFinalBlock(input, 0, input.Length);
RSAPKCS1KeyExchangeFormatter fmt = new RSAPKCS1KeyExchangeFormatter(rsa);
byte[] keyex = fmt.CreateKeyExchange(sa.Key);
// return the key exchange, the IV (public) and encrypted data
byte[] result = new byte[keyex.Length + sa.IV.Length + encrypt.Length];
Buffer.BlockCopy(keyex, 0, result, 0, keyex.Length);
Buffer.BlockCopy(sa.IV, 0, result, keyex.Length, sa.IV.Length);
Buffer.BlockCopy(encrypt, 0, result, keyex.Length + sa.IV.Length, encrypt.Length);
return result;
}
//Pass in key that contains private
static byte[] Decrypt(RSA rsa, byte[] input)
{
// by default this will create a 128 bits AES (Rijndael) object
SymmetricAlgorithm sa = SymmetricAlgorithm.Create();
byte[] keyex = new byte[rsa.KeySize >> 3];
Buffer.BlockCopy(input, 0, keyex, 0, keyex.Length);
RSAPKCS1KeyExchangeDeformatter def = new RSAPKCS1KeyExchangeDeformatter(rsa);
byte[] key = def.DecryptKeyExchange(keyex);
byte[] iv = new byte[sa.IV.Length];
Buffer.BlockCopy(input, keyex.Length, iv, 0, iv.Length);
ICryptoTransform ct = sa.CreateDecryptor(key, iv);
byte[] decrypt = ct.TransformFinalBlock(input, keyex.Length + iv.Length, input.Length - (keyex.Length + iv.Length));
return decrypt;
}
- Follow-Ups:
- Re: private to public decrypt now working
- From: Valery Pryamikov
- Re: private to public decrypt now working
- Prev by Date: Re: SignedXml.Signature.SignatureValue hash
- Next by Date: Code Signing and CAS for ASP.NET Application
- Previous by thread: Re: SSL
- Next by thread: Re: private to public decrypt now working
- Index(es):
Relevant Pages
|