3

I am using the code in below mentioned post to encrypt and decrypt values between Java and Java script module of my application.

Compatible AES algorithm for Java and Javascript

In a above post they are using 128 bit key value. I want to use my own key instead of hard coding the 128 bit key value.

My question is that can I convert any random string into 128 bit key value.

Please post some examples if it is possible to convert any string into 128 bit value.

Community
  • 1
  • 1
brig
  • 3,721
  • 12
  • 43
  • 61
  • possible duplicate of [128 bit key issue in AES algorithm Java/ Java Script](http://stackoverflow.com/questions/23371389/128-bit-key-issue-in-aes-algorithm-java-java-script) – Sunny Patel Apr 30 '14 at 19:11

2 Answers2

7

Characters are represented with 8 bits. hence to form 128 bit key, create a string having 16 chars (16*8=128), e.g. "abcdefgh12345678".

to mask this key as base64, you may use Apache commons-codec Base64.encodeBase64... @see http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html

Anirban Basak
  • 193
  • 1
  • 4
2

Something I fount by google, and use in my project:

private final static String algorithm = "PBKDF2WithHmacSHA1";

private final static String HEX = "0123456789ABCDEF";

private static final String CP_ALGORITH = "AES";
private static final String CP_KEY = "PUTsomeKEYinHere";

public static String cipher(String cipherKey, String data) throws NoSuchAlgorithmException, 
                    InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, 
                    IllegalBlockSizeException, BadPaddingException {
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
    KeySpec spec = new PBEKeySpec(cipherKey.toCharArray(), cipherKey.getBytes(), 128, 256);
    SecretKey tmp = skf.generateSecret(spec);
    SecretKey key = new SecretKeySpec(tmp.getEncoded(), CP_ALGORITH);
    Cipher cipher = Cipher.getInstance(CP_ALGORITH);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return toHex(cipher.doFinal(data.getBytes()));
}

public static String decipher(String cipherKey, String data) throws NoSuchAlgorithmException, 
                        InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, 
                        IllegalBlockSizeException, BadPaddingException {
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
    KeySpec spec = new PBEKeySpec(cipherKey.toCharArray(), cipherKey.getBytes(), 128, 256);
    SecretKey tmp = skf.generateSecret(spec);
    SecretKey key = new SecretKeySpec(tmp.getEncoded(), CP_ALGORITH);
    Cipher cipher = Cipher.getInstance(CP_ALGORITH);
    cipher.init(Cipher.DECRYPT_MODE, key);
    return new String(cipher.doFinal(toByte(data)));
}

private static byte[] toByte(String data) throws NullPointerException{
    int len = data.length()/2;
    byte[] result = new byte[len];
    for (int i = 0; i < len; i++)
        result[i] = Integer.valueOf(data.substring(2*i, 2*i+2), 16).byteValue();
    return result;
}

private static String toHex(byte[] doFinal) {
    StringBuffer result = new StringBuffer(2*doFinal.length);
    for (int i = 0; i < doFinal.length; i++) {
        result.append(HEX.charAt((doFinal[i]>>4)&0x0f)).append(HEX.charAt(doFinal[i]&0x0f));
    }
    return result.toString();
}

usage:

cipher(CP_KEY, STRINGtoCIPHER);

decipher(CP_KEY, YOURcipheredSTRING)

I put that all in a shared class with static fields and methods, so i can use it everywhere in my app. I use it to store SessionID i shared preferences, and it works nice.

bovquier
  • 78
  • 1
  • 14