BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 05-02-2009, 11:36 PM   #1 (permalink)
New Member
 
Join Date: May 2009
Model: 9530
PIN: N/A
Carrier: Verizon
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default RSA Encryption Key Problems

Please Login to Remove!

First let me start by saying I'm primarily a .NET developer, I've been learning some Java and the RIM APIs for this only in the last few days.
I'm using the BlackBerry JDE, trying to encrypt a string using a RSA key that I generated with Visual Studio/C#. After a while, I realized that my modulus value when I converted it to a byte array didn't match the modulus byte array when looking at it through Visual Studio. This caused me a lot of headaches trying to figure it out.

Now, the issue I face is with inconsistencies with my byte array. I took all the values out of Visual Studio, put them into a CSV, and used that to create my array on the JDE. However, when looking at the arrays side-by-side, the values still don't match up. I had to cast all my values as a byte, e.g. 175 became (byte)175 while I was creating the array. But that value that shows as 175 on Visual Studio might show up as -66 within the JDE.

Does anyone have any insight on this? I've been at this for hours and can't seem to find anyone else who's taking a .NET RSA key and used it in JDE.

Thanks!

Last edited by ShannyMan : 05-02-2009 at 11:37 PM.
Offline  
Old 05-03-2009, 10:18 AM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

In Java, a byte is a signed value with a range -127 to +127.

Anything higher than 127 will show up as a negative number if you try to cast from a byte to (for example) an int. The sign bit is propagated to the int.

I think you should compare the byte arrays using hexidecimal notation, so that your 175 value is AF. I think that if you compare the arrays this way, they are probably the same.
Offline  
Old 05-03-2009, 11:52 AM   #3 (permalink)
New Member
 
Join Date: May 2009
Model: 9530
PIN: N/A
Carrier: Verizon
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, I just tried that but it still doesn't seem to work. It doesn't throw any errors, but it doesn't perform the encryption. The out.size() always returns 0 and the string remains empty when copying it from the array. Here's my code:

public byte[] get_Modulus()
{
//TODO: Add support for storing it on file system
byte[] mod = new byte[] {(byte)0xB2,(byte)0x3F,(byte)0x70,(byte)0x16,(byte )0xAE,(byte)0x73,(byte)0x3E,(byte)0xD6,(byte)0x18, (byte)0x02,(byte)0x95,(byte)0x81,(byte)0xED,(byte) 0xA7,(byte)0xCD,(byte)0xD8,(byte)0x76,(byte)0xA2,( byte)0x00,(byte)0xA5,(byte)0x34,(byte)0x45,(byte)0 x0D,(byte)0xAD,(byte)0xD9,(byte)0xFA,(byte)0x20,(b yte)0xD7,(byte)0x1B,(byte)0xEE,(byte)0xA2,(byte)0x 4C,(byte)0x2B,(byte)0x33,(byte)0xD9,(byte)0xF9,(by te)0xA3,(byte)0xC5,(byte)0x10,(byte)0x62,(byte)0x6 6,(byte)0x3E,(byte)0x0D,(byte)0x7F,(byte)0x1D,(byt e)0x15,(byte)0x1E,(byte)0xD3,(byte)0xE2,(byte)0xFD ,(byte)0x84,(byte)0x46,(byte)0x1F,(byte)0x74,(byte )0x76,(byte)0x73,(byte)0x15,(byte)0x5C,(byte)0xA3, (byte)0x5E,(byte)0x9F,(byte)0x6E,(byte)0x61,(byte) 0x03,(byte)0xB4,(byte)0x31,(byte)0x5C,(byte)0xD2,( byte)0xDD,(byte)0xD1,(byte)0xB9,(byte)0x33,(byte)0 x76,(byte)0x92,(byte)0xE7,(byte)0xF0,(byte)0xCE,(b yte)0xB2,(byte)0xF3,(byte)0x8A,(byte)0xF2,(byte)0x 5F,(byte)0xFF,(byte)0xDD,(byte)0x1C,(byte)0x32,(by te)0x43,(byte)0x97,(byte)0x39,(byte)0x23,(byte)0x3 5,(byte)0xFC,(byte)0x78,(byte)0x5D,(byte)0xBE,(byt e)0x34,(byte)0x8C,(byte)0xD0,(byte)0xD8,(byte)0x99 ,(byte)0x09,(byte)0x1F,(byte)0x1A,(byte)0x4F,(byte )0xFF,(byte)0x2F,(byte)0x5A,(byte)0x8D,(byte)0x74, (byte)0x48,(byte)0x6B,(byte)0x13,(byte)0xF9,(byte) 0x59,(byte)0x1B,(byte)0x45,(byte)0x33,(byte)0xFF,( byte)0xF3,(byte)0x42,(byte)0x5C,(byte)0x41,(byte)0 x8D,(byte)0x5A,(byte)0xD4,(byte)0x2B,(byte)0xF5,(b yte)0x11};
return mod;
}

public byte[] get_Exponent()
{
byte[] exponent = new byte[] {(byte)1, (byte)0, (byte)1};
return exponent;
}

public String EncryptString(String pDecryptedData)
{
try
{
byte[] exponent = get_Exponent();
byte[] modulus = get_Modulus();

RSACryptoSystem crypto = new RSACryptoSystem(1024);

RSAPublicKey key = new RSAPublicKey(crypto, exponent, modulus);

NoCopyByteArrayOutputStream out = new NoCopyByteArrayOutputStream();

BlockEncryptor cryptoStream = new BlockEncryptor(new RSAEncryptorEngine(key), out);

byte[] plainText = pDecryptedData.getBytes();

cryptoStream.write(plainText, 0, plainText.length);

int finalLength = out.size();

byte[] cryptoText = new byte[finalLength];

System.arraycopy(cryptoText, 0, out.getByteArray(), 0, finalLength);

return new String(cryptoText);
}
catch (net.rim.device.api.crypto.InvalidKeyException e)
{
return "";
}
catch (java.lang.Exception e)
{
return "";
}
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.