Bouncycastle RSAEngine not decrypting ciphered key correctly

Hi,

I'm trying to implement a system where I'm using BC's RSAEngine() to encode a symmtric key of 128bits. Now when I create a new RSAEngine() object to simulate my receiver, who will decrypt the ciphered key, the output key is 128bytes, instead of 16nytes. I'm quite confused as to why. my code is below:

/* generate key pair */

RSAKeyPairGenerator kg = new RSAKeyPairGenerator();

kg.init(new RSAKeyGenerationParameters(BigInteger.valueOf(0x11),sr,1024,25));

AsymmetricCipherKeyPair pair = kg.generateKeyPair();

/* generate symmetric key */

KeyGenerator keygen = KeyGenerator.getInstance("AES","BC");

keygen.init(128,sr);

Key sKey = keygen.generateKey();

RSAEngine eng = new RSAEngine();

eng.init(true,pair.getPublic());

byte[] ckey = eng.processBlock(sKey.getEncoded(),0,sKey.getEncoded().length);

String keyFile = "res//ckey.txt";

BufferedWriter fr = new BufferedWriter(new FileWriter(keyFile));

String s = Utils.toHex(ckey);

fr.write(s);

fr.close();

keyFile = "res//skey.txt";

fr = new BufferedWriter(new FileWriter(keyFile));

s = Utils.toHex(sKey.getEncoded());

fr.write(s);

fr.close();

keyFile = "res//pkey.txt";

fr = new BufferedWriter(new FileWriter(keyFile));

fr.write(param.getModulus().toString(16) + "\n");

fr.write(param.getPublicExponent().toString(16) + "\n");

fr.write(param.getExponent().toString(16) + "\n");

fr.write(param.getP().toString(16) + "\n");

fr.write(param.getQ().toString(16) + "\n");

fr.write(param.getDP().toString(16) + "\n");

fr.write(param.getDQ().toString(16) + "\n");

fr.write(param.getQInv().toString(16) + "\n");

fr.close();

BufferedReader f = new BufferedReader(new FileReader("res//pkey.txt"));

BigInteger mod = new BigInteger(f.readLine(),16);

BigInteger pubExp = new BigInteger(f.readLine(),16);

BigInteger exp = new BigInteger(f.readLine(),16);

BigInteger p = new BigInteger(f.readLine(),16);

BigInteger q = new BigInteger(f.readLine(),16);

BigInteger dp = new BigInteger(f.readLine(),16);

BigInteger dq = new BigInteger(f.readLine(),16);

BigInteger qInv = new BigInteger(f.readLine(),16);

s = f.readLine();

f.close();

RSAPrivateCrtKeyParameters par = new RSAPrivateCrtKeyParameters(mod,pubExp,exp,p,q,dp,dq,qInv);

f = new BufferedReader(new FileReader("res//ckey.txt"));

byte[] tmp = Utils.hexToBytes(f.readLine());

f.close();

System.out.println(tmp.length);

RSAEngine e = new RSAEngine();

e.init(false,par);

byte[] t = eng.processBlock(tmp,0,tmp.length);

System.out.println("KEY: " + t.length);

[2770 byte] By [yipsheea] at [2007-11-27 9:04:54]
# 1

It would seem that no explicit padding is applied to your 128 bit data by the RSAEngine. This means you will probably get an implicit padding of all zeros to make the length up to your RSA modulus. If this is the case then all you need to do to is to ignore all but the last 128 bits (16 bytes) of the decrypted bytes.

You might do better to apply an explicit PKCS1 padding to your key before encrypting it because, if the exponent is small, then it will be fairly easy break the encryption.

sabre150a at 2007-7-12 21:38:27 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks sabre. Miraculously, the code I posted up works now, although what you mentioned about the padding and exponent interests me. Which exponent were you referring to?
yipsheea at 2007-7-12 21:38:28 > top of Java-index,Java Essentials,Java Programming...
# 3

> Thanks sabre. Miraculously, the code I posted up

> works now, although what you mentioned about the

> padding and exponent interests me. Which exponent

> were you referring to?

The public key exponent. It is usually a small prime - typically 3,17 or 65537 so that it is easy and quick to compute data^exponent .

sabre150a at 2007-7-12 21:38:28 > top of Java-index,Java Essentials,Java Programming...