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
Developer
Posts: 112
Registered: ‎09-23-2009
My Device: Not Specified
Accepted Solution

Problem encrypting with RSA

I am having problem when trying to encrypt an array of bytes using RSA. I use the example given at http://supportforums.blackberry.com/t5/Java-Development/Use-Advanced-Encryption/ta-p/445085.

 

Unfortunately, this code does not work.

 

                        byte[] content = secureNotes.toString().getBytes();
			
			System.out.println("Content: "+new String(content));
			
			System.out.println("1");
//			NoCopyByteArrayOutputStream out = new NoCopyByteArrayOutputStream();
			
			RSAEncryptorEngine engine = new RSAEncryptorEngine(rsaKeyPair.getRSAPublicKey());
			OAEPFormatterEngine fengine = new OAEPFormatterEngine( engine );
			
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			System.out.println("2");
			BlockEncryptor cryptoStream = new BlockEncryptor(
					fengine, out);
			System.out.println("3");
			cryptoStream.write(content, 0, content.length);
			System.out.println("Algorithm: "+cryptoStream.getAlgorithm());
			System.out.println("4");
			
			System.out.println("5");
//			out.flush();
			System.out.println("Size: "+out.size());
			content = out.toByteArray();
			System.out.println("6");
//			cryptoStream.close();
			System.out.println("7");
//			out.close();
			
			System.out.println("8");
			FileUtilities.saveToFile(directoryName + "secureNotes.json", content);
			
			System.out.println("9");
			
			

 

When the software execute line System.out.println("Size: "+out.size());. The result is "Size: 0"

 

So encryption failed. I checked the file I make: secureNotes.json, and it's empty.

 

Have anyone checked that this tutorial http://supportforums.blackberry.com/t5/Java-Development/Use-Advanced-Encryption/ta-p/445085 is valid anyway? Because I tried to implement my code based on this tutorial.

BlackBerry Development Advisor
Posts: 15,753
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Problem encrypting with RSA

You should call out.close before reading its size or bytes.  Call that after your 5th println.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Highlighted
Developer
Posts: 112
Registered: ‎09-23-2009
My Device: Not Specified

Re: Problem encrypting with RSA

Thanks Mark. But I finally find out that everytime I use 2048 as key size, it's not running well. But It's OK when I use 1024.