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
Visitor
Hizkia
Posts: 1
Registered: ‎07-08-2013
My Device: Bold
Accepted Solution

DES Encryption on Blackberry?

I have an application developed on BlackBerry JDE 5.0.0 that encrypts a String using DES algorithm with ECB mode. After the encryption, the result is encoded by base64 encoding. But whenever I compare the result that i get from my encryption method with the result that i get on the online encryptor engine, it always give different result on the several last character. I tried to decrypt the result that i get form my encryption method with the online encriptor engine and it looks like the result is not the valid one. So how can I fix that different result on the several last character?

Here my encryption method code :

 

public String encryptDESECB(String text) throws MessageTooLongException
{


byte[] input = text.getBytes();
byte[] output = new byte[8];
byte[] uid = null;
uid = "431654625bd37673e3b00359676154074a04666a".getBytes();
DESKey key = new DESKey(uid);
try

{

DESEncryptorEngine engine = new DESEncryptorEngine(key);
engine.encrypt(input, 0, output, 0);
String x= BasicAuth.encode(new String(output));
System.out.println("AFTER ENCODE"+x);
return new String(x);

}

catch (CryptoTokenException e)

{

return "NULL";

}

catch (CryptoUnsupportedOperationException e)

{

return "NULL";

}

}

 

The String that i want to encrypt is "00123456" The Result that i get from my encryption method is:YnF2BWFV/8w= The Result that i get from online encryptor engine (http://www.tools4noobs.com/online_tools/encrypt/) : YnF2BWFV9sw= The Result that i get from android (With the same encryption algorithm & Method) : YnF2BWFV9sw=

Here's the code on Android:

 

public static String encryptDesECB(String data)

{

try

{

DESKeySpec keySpec = newDESKeySpec("431654625bd37673e3b00359676154074a04666a".getBytes("UTF8"));

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);

// ENCODE plainTextPassword String
byte[] cleartext = data.getBytes("UTF8");

Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);

Logger.log(Log.INFO, new String(cipher.doFinal(cleartext)));

String encrypedPwd = Base64.encodeToString(cipher.doFinal(cleartext), Base64.DEFAULT);

Logger.log(Log.INFO, encrypedPwd);

return encrypedPwd;

}

catch (Exception e)

{

Logger.log(e);
return null;

}

}

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: DES Encryption on Blackberry?

welcome to the support forums.

the first step when working with encryption is to ensure that your output decrypts properly. i would suggest to keep the base64 out for now and decrypt the byte array after encrypting it.
You could also compare the contents of the byte array before converting it to string on both platforms.

maybe you could try to use utf-8 as the encoding when creating the string: http://www.blackberry.com/developers/docs/7.1.0api/java/lang/String.html#String(byte[],%20java.lang....
----------------------------------------------------------
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
New Contributor
HizkiaSetiadi
Posts: 2
Registered: ‎07-08-2013
My Device: Blackberry Bold 9700

Re: DES Encryption on Blackberry?

Thanks for your answer Simon.

I've been able to decrypt the content correctly on blackberry JDE 5.0.0 but the problem is I need pass the String that it produce (encoded with base64 encoding first) to the server as a PIN. I think the server couldn't decrypt the encrypted String correctly because it's always refuse the PIN that I give. So I think it might have something to do with those different result that I get from blackberry JDE after comparing it with Android and the online encryptor engine. So, any suggestion for this?

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: DES Encryption on Blackberry?

Sorry don't have time to look hard at this, but I am not sure that the following line is giving you what you want.

 

uid = "431654625bd37673e3b00359676154074a04666a".getBytes();

After executing this instruction, uid will contain bytes representing the characters.  So

uid[0] will contain 0x34

uid[1] will contain 0x33

uid[2] will contain 0x31

and so on.

 

Normally people supply keys in hexadecimal form, since retricting the key to printable characters significantly reduces the randomness associated with it. 

 

Can you confirm that you are initilaizing your key array correctly.

New Contributor
HizkiaSetiadi
Posts: 2
Registered: ‎07-08-2013
My Device: Blackberry Bold 9700

Re: DES Encryption on Blackberry?

Thanks for looking Peter. I already found my mistake. It turn out my BasicAuth method isn't the correct one for encoding the encrypted string. Now I'm using the correct one Base64 Class for the encoding, and it turn out fine. It produce the same String as android and the online encryptor engine.

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: DES Encryption on Blackberry?

Great, can you mark this as solved then?  Instructions in Help link near top of screen if needed.