private to public decrypt now working



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

Relevant Pages

  • Re: DECRYPT with PUBLIC key (how to?)
    ... This is a very stupid thing to think that you can encrypt with private ... Schneier wrote a book where he mistakenly used "Encryption with private ... decrypt it with MY Public Key. ... http://msdn2.microsoft.com/en-us/library/aa387460.aspx (Public/Private Key ...
    (microsoft.public.dotnet.security)
  • Re: [opensuse] Implementation of Private & Secure Mail Server & Mailing Lists Manager
    ... I'm not talking about public mailing lists. ... The task is to setup closed, private, secure mailing list for limited ... Decrypt using an Encrypt key? ...
    (SuSE)
  • RE: CrpytAPI, Decryption using the Public Key
    ... using the generated key pair's private part. ... You should NOT encrypt / decrypt your data with the RSA key pair. ... wrap this session key using the RSA public key: ...
    (microsoft.public.platformsdk.security)
  • Re: private to public decrypt now working
    ... If you switch it to use the public key to encrypt and private to decrypt it works. ...
    (microsoft.public.dotnet.security)
  • Re: Protecting commercial ruby code with public/private key encryption
    ... such codes can encrypt with either key and decrypt with either key. ... symmetric cipher, encrypt that key using your public key, and send both ciphertexts. ... That way I can send a private message to multiple recipients without having to encrypt the entire message multiple times. ...
    (comp.lang.ruby)