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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
New Developer
Posts: 11
Registered: ‎05-29-2009
My Device: Not Specified

Not getting proper bytes from BlockDecryptor read()

[ Edited ]

I try to call read() repeately but I dont get the expected amount of bytes returned.

 

while(decryptor.available()>0){
int bytesRead = decryptor.read( temp, 0, 300 );

buffer.write( temp, 0, bytesRead );

}

or

while(( bytesRead = decryptor.read( temp, 0, 300 )) > 0){

buffer.write( temp, 0, bytesRead );

}

 

So if I have a 33bytes block.  bytesRead returns 32 BUT IT HAS ACTUALY placed all 33 bytes into temp and available() drops out after the first time through.

 

Or if I have a 32 byte block, bytesRead returns 16 BUT IT HAS ACTUALY placed all 32 bytes into temp and available() drops out after the first time though.

 

Here is the entire section of code ... that I have lifted from somewhere else on this site .. I have about 18 iteration of codes that all do similar stuff.

 

 

class tw {
private String decoded = "";

tw() {

byte[] keybytes = "1234567887654321".getBytes();
String cipher = "YXDRctpfSHDc/QxBzyq/NzDpqUe2vOkyMzBYOPU3peQ=";
byte[] iv = "ABCDEFGHIJKLMNOP".getBytes();
byte[] cipherbytes;


try{
cipherbytes = Base64InputStream.decode(cipher);
}catch (IOException ioe){ return; }

decoded = DC2(keybytes, cipherbytes, new InitializationVector(iv));

}


private String DC2(byte[] keyData, byte[] cipher, InitializationVector initVec) {
String plainText = null;
try {
AESKey key = new AESKey( keyData);
AESDecryptorEngine engine = new AESDecryptorEngine( key );
PKCS5UnformatterEngine unformatterEngine = new PKCS5UnformatterEngine(new CBCDecryptorEngine(engine,initVec));
ByteArrayInputStream inputStream = new ByteArrayInputStream(cipher);
BlockDecryptor decryptor = new BlockDecryptor(unformatterEngine, inputStream);

byte[] temp = new byte[ 300 ];
DataBuffer buffer = new DataBuffer();

while(decryptor.available()>0){
int bytesRead = decryptor.read( temp, 0, 300 );
buffer.write( temp, 0, bytesRead );
}
inputStream.close();
plainText = new String(buffer.getArray());

} catch (Exception e) {
System.out.println("Exception in CBC encryption :: "+e);
}

return plainText;
}
}

 any pointers would be much appreciated as I have been 'here' for a while.

 

JDE 4.7.0.39 / Simulator 9000 (2.10.0.120)

 

 

Highlighted
Developer
Developer
Posts: 319
Registered: ‎07-20-2008
My Device: Not Specified

Re: Not getting proper bytes from BlockDecryptor read()

How sure are you that the data was encrypted correctly in the first place?

 

Also, might I suggest using the factory decryption method rather than the individual components...

 

 

InitializationVector ivv = new InitializationVector(iv);

SymmetricKey sk = SymmetricKeyFactory.getInstance
	("AES_256", keybytes, 0, keybytes.length);

ByteArrayInputStream inputStream = 
	new ByteArrayInputStream(cipher);

cis = DecryptorFactory.getDecryptorInputStream
	(sk, inputStream, "AES/CBC/PKCS5", ivv);

 

 

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

Re: Not getting proper bytes from BlockDecryptor read()

Stop using InputStream.available() -- it's not guaranteed to return non-zero values when InputStream.read() could return some data. Simply read unil InputStream.read() returns -1.