Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone

How to do Encryption and decryption using bouncycastle in blackberry?

We need to do Encryption and decryption using bouncycastle in blackberry? Please advise.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to do Encryption and decryption using bouncycastle in blackberry?

I suggest you get the Book:

 

Advanced BlackBerry Development

by Chris King.

 

There is a chapter in the book where is goes though some cryptography samples using Bouncy Castle.

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: How to do Encryption and decryption using bouncycastle in blackberry?

Slightly off-topic, but you can now access all Crypto APIs (even those protected with the Certicom key, RCC) on BlackBerry. May be this can remove the need for Bouncy Castle in your case...

Developer
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone

Re: How to do Encryption and decryption using bouncycastle in blackberry?

Hi Could you please let me know which one is better to user bouncycastle or RIM crypto API? Thanks
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: How to do Encryption and decryption using bouncycastle in blackberry?

The built-in Crypto API is built-in, so you don't have to add yet another crypto library to your project. Moreover, most of the crypto is implemented in native code (making it fast) and with electronic countermeasures enabled. The downside is that you have to trust that the crypto is implemented properly (no source code released to public).

 

Bouncy Castle may be advantageous if you are missing some APIs in the built-in Crypto API. Moreover, Bouncy Castle may be advantageous if you want to have platform-independent Java-only crypto code. The downside is that you'll only be able to use the J2ME version of it, and the library's binary might still be quite big.

Developer
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone

Re: How to do Encryption and decryption using bouncycastle in blackberry?

Hi Guys

 

Thanks for all the replies.

 

I have somehow managed to read the public and private keys from PEM files.

 

Below is my code to read both public and private keys.

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import org.bouncycastle.asn1.x509.RSAPublicKeyStructure;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMReader;
import org.bouncycastle.openssl.PasswordFinder;

public class PEMUtil {
private static class Password implements PasswordFinder {
char[] password;

Password(char[] word) {
this.password = word;
}

public char[] getPassword() {
return password;
}
}

private PEMReader openPEMResource(String fileName, String password) {
InputStream res = this.getClass().getResourceAsStream(fileName);
Reader fRd = new BufferedReader(new InputStreamReader(res));
if (password != null) {
return new PEMReader(fRd, new Password(password.toCharArray()));
} else {
return new PEMReader(fRd);
}
}

public PrivateKey getPrivateKey(String fileName, String password)
throws IOException {
PEMReader pr = openPEMResource(fileName, password);
Object o = pr.readObject();

if (o == null || !(o instanceof KeyPair)) {
System.out.println("Didn't find OpenSSL key");
}

KeyPair kp = (KeyPair) o;
PrivateKey privateKey = kp.getPrivate();
System.out.println("Private Key " + privateKey );

RSAPrivateKey privateKey2 = (RSAPrivateKey) privateKey;
System.out.println("Private Key " + privateKey2 );



return privateKey;
}

public PrivateKey getPrivateKey() throws IOException {
return getPrivateKey("private_key.pem", null);
}

public PublicKey getPublicKey(String fileName, String password)
throws IOException {
PEMReader pr = openPEMResource(fileName, password);
Object o = pr.readObject();

if (!(o instanceof RSAPublicKey)) {
System.out.println("Didn't find OpenSSL key");
}

RSAPublicKey publicKey = (RSAPublicKey) o;
System.out.println("Public Key " + publicKey);


RSAKeyParameters rsaPublic = new RSAKeyParameters(false, publicKey.getModulus(),
publicKey.getPublicExponent());

System.out.println("rsaPublic "+ rsaPublic);

return publicKey;

}

public PublicKey getPublicKey() throws IOException {
return getPublicKey("onboard_public_key.pem", null);
}

public static void main(String args[]) throws IOException {
PEMUtil util = new PEMUtil();
Security.addProvider(new BouncyCastleProvider());
util.getPrivateKey();
util.getPublicKey();
// util.getPublKey("public_key.pem");
}
}

 

The problem I have now is that I could read the keys in objects PrivateKey and PublicKey respectively, while in my encryption and decryption code I need objects of RSAKeyParameters and RSAPrivateCrtKeyParameters.

 

Though I could convert my PublicKey into RSAKeyParameters, I could not find any way to convert my PrivateKy to RSAPrivateCrtKeyParameters.

 

Below is my Encryption and decryption code.

 

 

	public static byte[] RSAEncrypt(String plainText) throws Exception {
byte[] rv = null;
AsymmetricBlockCipher eng = new RSAEngine();
RSAKeyParameters rsaPublic = GenerateKeys.getPublicKey();
eng.init(true, rsaPublic);
byte[] ptBytes = plainText.getBytes();
rv = eng.processBlock(ptBytes, 0, ptBytes.length);
return rv;
}

public static String RSADecrypt(byte[] cipherText)throws Exception {
byte[] rv = null;
AsymmetricBlockCipher eng = new RSAEngine();
RSAPrivateCrtKeyParameters rsaPrivate = GenerateKeys.getPrivateKey();
eng.init(false, rsaPrivate);
rv = eng.processBlock(cipherText, 0, cipherText.length);
return new String(rv);
}

 

 

 

Please help.

 

Thanks

 

Developer
Posts: 157
Registered: ‎02-18-2009
My Device: Not Specified

Re: How to do Encryption and decryption using bouncycastle in blackberry?

[ Edited ]

I think your questions would get better answers if you ask them on the Bouncycastle mailing list because it's not related to BlackBerry. However, it seems that you do not require a RSAPrivateCrtKeyParameters instance because RSAEngine accepts a RSAKeyParameters instance as well. You already have a PrivateKey which should be a RSAPrivateKey (you should be able to cast it to RSAPrivateKey). You can now create an instance of RSAKeyParameters.

 

Untested code:

 

 

CipherParameters params = new RSAKeyParameters(true, 
privateKey.getModulus(), privateKey.getPrivateExponent());

 

 

The params can now be used in the call to init:

 

eng.init(false, params);