Problems with RSA Encryption
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, I'm using the hex values which should be identical because the byte in Java is a signed value, -127 to 127, unlike within C#. The code doesn't throw any errors, but it doesn't perform the encryption. Below is my code. I can't seem to find any information that demonstrates the usage of RSA encryption with the exception of the partial example in the API doc. Thanks.
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 "";
}
|