RSACryptoServiceProvider decrypt with public key
From: Martin M?ller (mav.northwind_at_web.de)
Date: 05/19/04
- Next message: anonymous: "Re: Application security"
- Previous message: Nicole Calinoiu: "Re: Application security"
- Next in thread: Stephen McCloskey [MSFT]: "Re: RSACryptoServiceProvider decrypt with public key"
- Reply: Stephen McCloskey [MSFT]: "Re: RSACryptoServiceProvider decrypt with public key"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: 19 May 2004 05:03:22 -0700
Dear community, please help:
For several days now I've been trying to implement something that
should be possible according to all the sources you find on asymmetric
encryption but just can't get it to work.
The main idea behind asymmetric encryption is that there's a public
and a private key and that the public key can be derived from the
private key, but not the other way round.
Data encrypted with the public key can be decrypted using the private
key.
Data encrypted with the private key can be decrypted using the public
key.
The first way (encrypt with public, decrypt with private) is shown in
several examples you can find, but the other one (encrypt with
private, decrypt with public) doesn't seem to work and I can't find a
working example for it either.
I'm using .NET 1.0 on a WinXP machine and tried the following:
Create a new RSACryptoServiceProvider, save each key to a separate
file using ToXmlString().
For encryption I read the private key file, use a new
RSACryptoServiceProvider's FromXmlString() method to set the
parameters just read and encode the data to a Base64 string.
The data encrpyted is very short (about 20 bytes) and key length is
sufficient (1024, but you get the same result with other key lengths).
Encryption works fine.
Now I create a new CSP like before, read the public key file, convert
the Base64 string to a byte array again and try to decrypt it and I
always get a CryptographicException stating "invalid key", if I don't
use OAEP padding and "Error occurred while decoding OAEP padding."
when using padding.
The other way (encrypt with public and decrypt with private) does
work, as does encryption and decryption both with the private key, but
if I always need the private key what's the point of asymetric
encryption ?
Can anyone enlighten me? Is it a bug in .NET 1.0? Does 1.1 behave
differently? What other options do I have?
Here are the code fragments I described above:
---------------- Create a key pair -------------------------------
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(1024);
string s = csp.ToXmlString(true);
StreamWriter sw = new StreamWriter("KeyPriv.xml");
sw.WriteLine(s);
sw.Close();
s = csp.ToXmlString(false);
sw = new StreamWriter("KeyPub.xml");
sw.WriteLine(s);
sw.Close();
csp.Clear();
------------------------------------------------------------------
---------------- Encrypt with private key ------------------------
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(1024);
StreamReader sr = new StreamReader("KeyPriv.xml");
string s = sr.ReadToEnd();
sr.Close();
csp.FromXmlString(s);
byte[] inp = System.Text.Encoding.Unicode.GetBytes(clearText);
byte[] outp = csp.Encrypt(inp, false);
cypherText = Convert.ToBase64String(outp);
csp.Clear();
------------------------------------------------------------------
---------------- Decrypt with public key -------------------------
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(1024);
StreamReader sr = new StreamReader("KeyPub.xml");
string s = sr.ReadToEnd();
sr.Close();
csp.FromXmlString(s);
byte[] inp = Convert.FromBase64String(cypherText);
// This always throws an exception "invalid key" :(((
byte[] outp = csp.Decrypt(inp, false);
clearText = System.Text.Encoding.Unicode.GetString(outp);
csp.Clear();
------------------------------------------------------------------
- Next message: anonymous: "Re: Application security"
- Previous message: Nicole Calinoiu: "Re: Application security"
- Next in thread: Stephen McCloskey [MSFT]: "Re: RSACryptoServiceProvider decrypt with public key"
- Reply: Stephen McCloskey [MSFT]: "Re: RSACryptoServiceProvider decrypt with public key"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|