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 Contributor
Posts: 3
Registered: ‎02-16-2012
My Device: 9810
My Carrier: XL

DES encryption result to string

hi all i wanna encrypt the message with key and give the result is hexadecimal... ho i can do that??

 

thx

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

Re: DES encryption result to string

[ Edited ]

Welcome to the forums.

 

It is unusual to wish to have the encrypted data in hexadecimal.  As I understand it, most people will, if they have to send Binary data in text form, use Base64, which I think is more efficient.

 

If you wish to convert a byte array to hex String, then something like this will do it:

http://supportforums.blackberry.com/t5/Java-Development/Showing-garbage-instead-of-Unicode-character...

 

So then the question is how do you use DES.  This unfortunately is not a trivial exercise, because there are actually quite a number of flavors of DES, depending on single or triple, the Block chaining that you use, the initial vector and the padding. 

 

If you look at your JavaDoc for DESEncryptorEngine, you will find a link it it that points you at a code sample. 

 

Can I recommend that you review AES.  It is a more efficient encryption, and more secure.  I would not use DES for anything unless I had no choice. 

New Contributor
Posts: 3
Registered: ‎02-16-2012
My Device: 9810
My Carrier: XL

Re: DES encryption result to string

hi peter thx for your explaination..

 

here is my code

 

package mypackage;

import net.rim.device.api.crypto.

CryptoTokenException;
import net.rim.device.api.crypto.CryptoUnsupportedOperationException;
import net.rim.device.api.crypto.DESDecryptorEngine;
import net.rim.device.api.crypto.DESEncryptorEngine;
import net.rim.device.api.crypto.DESKey;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen implements FieldChangeListener
{
       EditField input;
       LabelField encrypt;
       LabelField decrypt;
       ButtonField execute;
       String _encrypt = "encrypt: ";
       String _decrypt = "decrypt: ";
   /**
    * Creates a new MyScreen object
    */
   public MyScreen()
   {
       // Set the displayed title of the screen
       setTitle("crypto test");
       input = new EditField("message: ", "");
       add(input);
       add(new SeparatorField());
       encrypt = new LabelField(_encrypt);
       decrypt = new LabelField(_decrypt);
       add(encrypt);
       add(decrypt);
       add(new SeparatorField());
       execute = new ButtonField("execute", ButtonField.CONSUME_CLICK);
       execute.setChangeListener(this);
       add(execute);
   }
       public void fieldChanged(Field field, int context) {
               // TODO Auto-generated method stub
               if(field == execute) {
                       String _message = input.getText();
                       String _e = encrypt(_message);
                       if(_e != null) {
                               encrypt.setText(_encrypt + _e);
                       }

                       String _d = decrypt(_e);
                       if(_d != null) {
                               decrypt.setText(_decrypt + _d);
                       }
               }
       }

       public String decrypt(String ciphered) {
               byte[] ba = "3b3898371520f75e".getBytes();
               DESKey key = new DESKey(ba);
                try {
                       DESDecryptorEngine engine = new DESDecryptorEngine( key );
                       byte[] data = new byte[engine.getBlockLength()];
                       engine.decrypt(hexStringToByteArray(ciphered), 0, data, 0);
                       return new String(data);
               } catch (CryptoTokenException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
               } catch (CryptoUnsupportedOperationException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
               }
               return null;
       }

       public String encrypt(String text) {
               byte[] ba = "3b3898371520f75e".getBytes();
               DESKey key = new DESKey(ba);
               try {
                       DESEncryptorEngine engine = new DESEncryptorEngine( key );
                       byte[] data = new byte[engine.getBlockLength()];
                       engine.encrypt(text.getBytes(), 0, data, 0);
                       return byteArrayToHexString(data);
               } catch (CryptoTokenException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
               } catch (CryptoUnsupportedOperationException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
               }
               return null;
       }

       public static byte[] hexStringToByteArray(String s) {
       int len = s.length();
       byte[] data = new byte[len / 2];
       for (int i = 0; i < len; i += 2) {
           data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                   + Character.digit(s.charAt(i+1), 16));
       }
       return data;
   }

       public static String byteArrayToHexString(byte[] b) {
           StringBuffer sb = new StringBuffer(b.length * 2);
           for (int i = 0; i < b.length; i++) {
             int v = b[i] & 0xff;
             if (v < 16) {
               sb.append('0');
             }
             sb.append(Integer.toHexString(v));
           }
           return sb.toString();
         }
}
i just wanna match the result with this DES calculator --- > HERE DES CAlculator javascriot
but why the result is soo different.. why?? thx
Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: DES encryption result to string

Interesting. code looks fine.

 

I would appreciate you actually dumping off the hex for the bytes you put into the encryption and what you get out.  For example, can you change:

 

engine.encrypt(text.getBytes(), 0, data, 0);

To

 

byte [] plainText = text.getBytes(),

System.out.println("Plain text(" + plainText.length + "): " +  byteArrayToHexString(plainText));

engine.encrypt(plainText, 0, data, 0);

System.out.println("Cipher text(" + data.length + "): " +  byteArrayToHexString(data));

System.out.println("Key(" + ba.length + "): " +  byteArrayToHexString(ba));

 

I'd be interested to see what happens if you put the same values in as are in the sample on the page you pointed at.  You won't be able to do that with your current code, so you could change your current code so that you type in hex values rather than characters.    I appreciate that is probably not what you want to do long term but it might help us with this problem.