Encrypt string with public key only

Posted by vlahovic on Stack Overflow See other posts from Stack Overflow or by vlahovic
Published on 2011-06-30T07:09:25Z Indexed on 2011/06/30 8:22 UTC
Read the original article Hit count: 225

i'm currently working on a android project where i need to encrypt a string using 128 bit AES, padding PKCS7 and CBC. I don't want to use any salt for this.

I've tried loads of different variations including PBEKey but i can't come up with working code. This is what i currently have:

String plainText = "24124124123";
String pwd = "BobsPublicPassword";
byte[] key = pwd.getBytes(); 
key = cutArray(key, 16);
byte[] input = plainText.getBytes();
byte[] output = null;
SecretKeySpec keySpec = null;
keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
output = cipher.doFinal(input);

private static byte[] cutArray(byte[] arr, int length){
byte[] resultArr = new byte[length];
for(int i = 0; i < length; i++ ){
    resultArr[i] = arr[i];
}
return resultArr;
}

Any help appreciated

//Vlahovic

© Stack Overflow or respective owner

Related posts about java

Related posts about android