Sunday, 3 March 2013

Blackberry AES256 Encryption and Decryption.

Today i am going to share very useful and interesting topic i.e AES 256 Encryption and decryption.
For achieving this i created a Library for doing Encryption operation.

You can get Library file from this Link

Step1)
Create a Blackberry Project named as "AESTest"

Step2) In your Main Screen Write down the following piece of code as given below.

package co.aes.test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import net.rim.device.api.crypto.CryptoException;
import net.rim.device.api.system.Display;
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.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import com.aes.encryption.AES256Encryption;
import com.aes.encryption.AESUtility;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class AESEncryption extends MainScreen implements
        FieldChangeListener {
    /** This is the Secret Key required for Encryption and Decryption */
    private String secretKey="ABCtfiHT1S1biFnoiFFWZcPwWBnhxqhkQ1Ipyh2yG7U=";
    private String cipher, plainText;
    private ButtonField but_encrypt, but_decrypt;
    private LabelField encrypted, decrypted;
    private VerticalFieldManager vfm;
    private EditField enterText;

    /**
     * Creates a new AESMainScreen object
     */
    public AESEncryption() {
        setTitle("AES");
        but_encrypt = new ButtonField("Encrypt");
        but_decrypt = new ButtonField("Decrypt");
        encrypted = new LabelField();
        decrypted = new LabelField();
        enterText = new EditField();
        enterText.setPadding(15, 15, 15, 15);
        encrypted.setPadding(5, 5, 5, 5);
        decrypted.setPadding(5, 5, 5, 5);
        but_encrypt.setChangeListener(this);
        but_decrypt.setChangeListener(this);

        vfm = new VerticalFieldManager(Field.FIELD_HCENTER
                | Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT) {
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(maxWidth, maxHeight);
                int topSpace = (Display.getHeight() / 8);
                setPadding(topSpace, 0, 0, 0);
            }
        };
        HorizontalFieldManager hfm1 = new HorizontalFieldManager(
                Field.FIELD_HCENTER);
        hfm1.add(but_encrypt);
        hfm1.add(but_decrypt);
        vfm.add(hfm1);
        vfm.add(encrypted);
        vfm.add(decrypted);
        add(enterText);
        add(vfm);
    }

    public void fieldChanged(Field field, int context) {
        /** Encryption Button OnClick Event */
        if (field == but_encrypt) {
            try {
                cipher = AES256Encryption.encryptbyCBC(
                        AESUtility.Base64ConvertedArray(secretKey),
                        AESUtility.getByteInLittleIndian(enterText.getText()));
                System.out.println("Cipher Text:-" + " " + cipher);
                /** Setting the Cipher Text to the Label Field*/
                encrypted.setText("Cipher Text:----" +"\n"+cipher);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (CryptoException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (field == but_decrypt) {
            try {
                plainText = AES256Encryption.decryptCBC(
                        AESUtility.Base64ConvertedArray(secretKey),
                        AESUtility.Base64ConvertedArray(cipher));
            } catch (CryptoException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Plain text:-" + "  " + plainText);
            /** Setting the Plain Text to the Label Field*/
            decrypted.setText("Plain Text:-" + plainText);
        }
    }
}

ScreenShots:-1) Enter your text to Encrypt with the key. You can give your own key.




































ScreenShots:2) After Clicking Encrypt Button, Cipher text will be shown in LabelField.



































ScreenShots:3) After Decrypted.



































Download Full source Code Click AESEncryptionDecryption

No comments:

Post a Comment