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
New Developer
digitalhuman
Posts: 13
Registered: ‎10-28-2008
My Device: Bol
My Carrier: Telia

Solution: RSA Public Private key encryption / decryption example

[ Edited ]

Since I was stuck with this for weeks I will help you all out with my working example code. Hope you enjoy it. I really don't want to put you in the same tunnel where I was. Cause you simply can't find anything about this on the internet. Well, not with using your own existing public and private keys.

 

Now, lets start.

 

Convert your Private PEM key to DER using openssl

1) openssl pkcs8 -topk8 -inform PEM -outform DER -in my.private.key -nocrypt > pkcs8_private_key.der

2) we will get the public key of your PEM .crt (Certificate file)

 

//Here what I did and use

 

openssl pkcs8 -topk8 -inform PEM -outform DER -in my.private.key -nocrypt > pkcs8_private_key.der

//First get the private key
RSAPrivateKey privKey = privKey = (RSAPrivateKey)PKCS8_PrivateKeyDecoder.decode(getClass().getResourceAsStream("pkcs8_private_key.der"), "PKCS8");

//Now get the public key from our PEM RSA certificate
InputStream stream = getClass().getResourceAsStream("sams.crt");
byte[] base64bytes = new byte[stream.available()];
stream.read(base64bytes, 0, base64bytes.length);
stream.close();

X509Certificate cert = (X509Certificate) CertificateUtilities.readCertificateFile("X509", base64bytes);
pubKey = (RSAPublicKey) cert.getPublicKey();

//Initiate the Crypto system
RSACryptoSystem crypt = crypt = new RSACryptoSystem(2048);  //I used 2048 bits keys


//Example method to encrypt data with the above objects

public String encrypt(String data){
	try{	
		if(data.length() > 0){
			NoCopyByteArrayOutputStream out = new NoCopyByteArrayOutputStream();
			data = data.trim();
			byte[] plain = data.getBytes();		
			RSAEncryptorEngine eng = new RSAEncryptorEngine(this.pubKey);
			PKCS1FormatterEngine fengine = new PKCS1FormatterEngine(eng);
			
			BlockEncryptor cryptoStream = new BlockEncryptor(fengine, out);
			cryptoStream.write( plain, 0, plain.length );
			cryptoStream.flush();
			cryptoStream.close();
			out.close();
			
			byte[] cipherText = out.getByteArray();
			
			String encMessage = Base64OutputStream.encodeAsString(cipherText, 0, cipherText.length, false, false);
			return encMessage;
		}else{
			return "";
		}
	}
	catch(Exception err){
		System.err.println(err.toString());
		return "";
	}
}

 

Also, i you need help with anything. Contact me on twitter: @digital_human or visit my blog: http://victorangelier.blogspot.com

Meet me at #SAMS: http://www.onsams.com, Twitter: @digital_human, or visit my blog: http://victorangelier.blogspot.com
Please use plain text.
Developer
simon_hain
Posts: 15,806
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Solution: RSA Public Private key encryption / decryption example

that looks very helpful. maybe you could create a knowledge base article from it?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Developer
digitalhuman
Posts: 13
Registered: ‎10-28-2008
My Device: Bol
My Carrier: Telia

Re: Solution: RSA Public Private key encryption / decryption example

Yes I can, where and howto do it?

Meet me at #SAMS: http://www.onsams.com, Twitter: @digital_human, or visit my blog: http://victorangelier.blogspot.com
Please use plain text.
Developer
simon_hain
Posts: 15,806
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Solution: RSA Public Private key encryption / decryption example

http://supportforums.blackberry.com/t5/BlackBerry-World-Development/How-to-create-your-own-article-a...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Developer
digitalhuman
Posts: 13
Registered: ‎10-28-2008
My Device: Bol
My Carrier: Telia

Re: Solution: RSA Public Private key encryption / decryption example

You are way to fast :smileywink: Thx

Meet me at #SAMS: http://www.onsams.com, Twitter: @digital_human, or visit my blog: http://victorangelier.blogspot.com
Please use plain text.
New Developer
digitalhuman
Posts: 13
Registered: ‎10-28-2008
My Device: Bol
My Carrier: Telia

Re: Solution: RSA Public Private key encryption / decryption example

Consider it done.

Meet me at #SAMS: http://www.onsams.com, Twitter: @digital_human, or visit my blog: http://victorangelier.blogspot.com
Please use plain text.