Home All Groups Group Topic Archive Search About

Bad Data CryptographicException when RSA decrypting

Author
2 Jun 2005 10:57 PM
Jacek
Hello.
I'm developing a client server program using RSA to encrypt/decrypt data
sent and received via NetworkStream. On win2k when I try to decrypt a
message, encrypted with 1024 bit key I got exception: "bad data", but when I
encrypt the same message with 512 bit key, decryption works fine. Also on
WinXP encryption and decryption with 512 or 1024 bit keys works OK.
Do you know why I encounter this exception?
I attach some of my code:

....
byte [] tosend = encr.DoRSAEncrypt(ud.GetBytes());
//test decryption. the following line fails
UserData uda = new UserData(decr.DoRSADecrypt(tosend));

--------------
public byte[] DoRSAEncrypt(byte[] keydata)
{
byte [] modulus = (byte[])certkeymodulus.Clone();
byte [] exponent = (byte[])certkeyexponent.Clone();
if(keydata==null || modulus==null || exponent==null)
   return null;
byte[] protectedkey = null;
try
{
//Initialize RSAKeyInfo with public parameters
RSAParameters RSAKeyInfo = new RSAParameters();
RSAKeyInfo.Modulus    = modulus;
RSAKeyInfo.Exponent    = exponent;
//Initialize RSACryptoServiceProvider
RSACryptoServiceProvider oRSA    = new RSACryptoServiceProvider();
oRSA.ImportParameters(RSAKeyInfo);
protectedkey = oRSA.Encrypt(keydata, false);
}
catch(CryptographicException cex)
{
Console.WriteLine(cex.Message);
return null ;
}
return protectedkey;
}

-------------

public byte[] DoRSADecrypt(byte[] encdata)
{
string container = this.keycontainer;
int keyspec = this.RSAkeytype;
if(encdata==null ||container==null || (keyspec !=1 && keyspec !=2) )
return null;

byte[] clearkey = null;
try
{
//Construct RSA with keycontainer associated with certificate found
CspParameters cp = new CspParameters();
cp.KeyContainerName = container;
cp.KeyNumber = keyspec;
cp.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider(cp);
clearkey = oRSA.Decrypt(encdata, false);
}
catch(CryptographicException cexc)
{
Console.WriteLine("Error in DoRSAKeyDecrypt\n{0}", cexc.Message);
return null ;
}
return clearkey;
}

--------------
public class UserData
{
public string username;
public string password;
public int usernamesize;
public int passwordsize;
public int size;

public UserData() {}

public UserData(string user, string pass)
{
username = user;
usernamesize = user.Length;
password = pass;
passwordsize = pass.Length;
size = (2*4) + usernamesize + passwordsize;
}

public UserData(byte [] data)
{
int offset = 0;
usernamesize = BitConverter.ToInt32(data, offset);
offset += 4;
username = Encoding.ASCII.GetString(data, offset, usernamesize);
offset = offset + usernamesize;
passwordsize = BitConverter.ToInt32(data, offset);
offset +=4;
password = Encoding.ASCII.GetString(data, offset, passwordsize);
offset = offset + passwordsize;
size = offset;
}

public byte [] GetBytes()
{
byte [] data = new byte[40];
int offset = 0;
Buffer.BlockCopy(BitConverter.GetBytes(username.Length), 0, data, offset, 4);
offset += 4;
Buffer.BlockCopy(Encoding.ASCII.GetBytes(username), 0, data, offset,
username.Length);
offset += username.Length;
Buffer.BlockCopy(BitConverter.GetBytes(password.Length), 0, data, offset, 4);
offset +=4;
Buffer.BlockCopy(Encoding.ASCII.GetBytes(password), 0, data, offset,
password.Length);
offset += password.Length;
size = offset;
return data;
}
    }

Author
3 Jun 2005 9:38 AM
Valery Pryamikov
Hi,
send "Microsoft Strong Cryptographic Provider" or "Microsoft Enhanced
Cryptographic Provider" CspParameter to the constructor of
RSACryptoServiceprovicer.
ex:
CspParameters providerParam = new CspParameters(1, "Microsoft Strong
Cryptographic Provider");
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(providerParam);

-Valery.
http://www.harper.no/valery

Show quoteHide quote
"Jacek" <Ja***@discussions.microsoft.com> wrote in message
news:74C70C6F-8FFD-4BCF-9FD3-732119DC5A5D@microsoft.com...
> Hello.
> I'm developing a client server program using RSA to encrypt/decrypt data
> sent and received via NetworkStream. On win2k when I try to decrypt a
> message, encrypted with 1024 bit key I got exception: "bad data", but when
> I
> encrypt the same message with 512 bit key, decryption works fine. Also on
> WinXP encryption and decryption with 512 or 1024 bit keys works OK.
> Do you know why I encounter this exception?
> I attach some of my code:
>
> ...
> byte [] tosend = encr.DoRSAEncrypt(ud.GetBytes());
> //test decryption. the following line fails
> UserData uda = new UserData(decr.DoRSADecrypt(tosend));
>
> --------------
> public byte[] DoRSAEncrypt(byte[] keydata)
> {
> byte [] modulus = (byte[])certkeymodulus.Clone();
> byte [] exponent = (byte[])certkeyexponent.Clone();
> if(keydata==null || modulus==null || exponent==null)
>   return null;
> byte[] protectedkey = null;
> try
> {
> //Initialize RSAKeyInfo with public parameters
> RSAParameters RSAKeyInfo = new RSAParameters();
> RSAKeyInfo.Modulus = modulus;
> RSAKeyInfo.Exponent = exponent;
> //Initialize RSACryptoServiceProvider
> RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider();
> oRSA.ImportParameters(RSAKeyInfo);
> protectedkey = oRSA.Encrypt(keydata, false);
> }
> catch(CryptographicException cex)
> {
> Console.WriteLine(cex.Message);
> return null ;
> }
> return protectedkey;
> }
>
> -------------
>
> public byte[] DoRSADecrypt(byte[] encdata)
> {
> string container = this.keycontainer;
> int keyspec = this.RSAkeytype;
> if(encdata==null ||container==null || (keyspec !=1 && keyspec !=2) )
> return null;
>
> byte[] clearkey = null;
> try
> {
> //Construct RSA with keycontainer associated with certificate found
> CspParameters cp = new CspParameters();
> cp.KeyContainerName = container;
> cp.KeyNumber = keyspec;
> cp.Flags = CspProviderFlags.UseMachineKeyStore;
> RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider(cp);
> clearkey = oRSA.Decrypt(encdata, false);
> }
> catch(CryptographicException cexc)
> {
> Console.WriteLine("Error in DoRSAKeyDecrypt\n{0}", cexc.Message);
> return null ;
> }
> return clearkey;
> }
>
> --------------
> public class UserData
> {
> public string username;
> public string password;
> public int usernamesize;
> public int passwordsize;
> public int size;
>
> public UserData() {}
>
> public UserData(string user, string pass)
> {
> username = user;
> usernamesize = user.Length;
> password = pass;
> passwordsize = pass.Length;
> size = (2*4) + usernamesize + passwordsize;
> }
>
> public UserData(byte [] data)
> {
> int offset = 0;
> usernamesize = BitConverter.ToInt32(data, offset);
> offset += 4;
> username = Encoding.ASCII.GetString(data, offset, usernamesize);
> offset = offset + usernamesize;
> passwordsize = BitConverter.ToInt32(data, offset);
> offset +=4;
> password = Encoding.ASCII.GetString(data, offset, passwordsize);
> offset = offset + passwordsize;
> size = offset;
> }
>
> public byte [] GetBytes()
> {
> byte [] data = new byte[40];
> int offset = 0;
> Buffer.BlockCopy(BitConverter.GetBytes(username.Length), 0, data, offset,
> 4);
> offset += 4;
> Buffer.BlockCopy(Encoding.ASCII.GetBytes(username), 0, data, offset,
> username.Length);
> offset += username.Length;
> Buffer.BlockCopy(BitConverter.GetBytes(password.Length), 0, data, offset,
> 4);
> offset +=4;
> Buffer.BlockCopy(Encoding.ASCII.GetBytes(password), 0, data, offset,
> password.Length);
> offset += password.Length;
> size = offset;
> return data;
> }
> }

Bookmark and Share