1. Download our Official Android App: Forums for Android!

Apps Encrytion on Android

Discussion in 'Android Development' started by Vlhad, Jun 30, 2011.

  1. Vlhad

    Vlhad Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Jun 30, 2011

    Jun 30, 2011
    1
    0
    5
    I have been trying to encrypt a string with AES, CBC and PKCS#7 on Android for about 2 days now, I cannot get it to work! I have a shared key that I want to encrypt the string with. This is my code:
    Code (Text):
    1.  
    2.             String plainText = "24124124123";
    3.             String pwd = "BobsPublicPassword";
    4.             byte[] key = pwd.getBytes("UTF-16");
    5.             key = cutArray(key, 16);
    6.             byte[] input = plainText.getBytes("UTF-16");
    7.             byte[] output = null;
    8.             SecretKeySpec keySpec = null;
    9.             keySpec = new SecretKeySpec(key, "AES");
    10.             Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    11.             cipher.init(Cipher.ENCRYPT_MODE, keySpec);
    12.             output = cipher.doFinal(input);
    13.  
    My method cutArray at line 4 looks like this:
    Code (Text):
    1.     private static byte[] cutArray(byte[] arr, int length){
    2.         byte[] resultArr = new byte[length];
    3.         for(int i = 0; i < length; i++ ){
    4.             resultArr[i] = arr[i];
    5.         }
    6.         return resultArr;
    7.     }
    The reason for this method is that if I dont use it, I get an exception because my key isnt 128, 196 or 256 bits. And now that I do cut my array in size, my output is messed up. I
     

    Advertisement

Share This Page

Loading...