I got problem with encryption using java AES/CBC/PKCS7Padding. I have already search and follow throuh till using the BouncyCastle
provider. But I still cant get the right encryption
Let say the requirements are:
Encryption Type: Symmetric
Algorithm: AES
Blocksize = 128Bit (16 Bytes)
Cipher mode: CBC
Padding mode: PKCS7
Encryption key length: 256 Bit (32 Bytes)
Vector Initialization Length (IV): 128 Bit (16 Bytes)
sample:
Plain data = ABC123
Encrypted data (base64 encoded) = CtGtW4hJfXxilSfNR1xmrg==
and my code is...
public final class StringFunc {
final static String key = "jb2a19ou79rws6zknjlr803fvfgiyp1k";
final static String algorithm = "AES/CBC/PKCS7Padding";
final static String iv = "hod74ty97wr97g83";
private static Cipher cipher = null;
private static SecretKeySpec skeySpec = null;
private static IvParameterSpec ivSpec = null;
private static void setUp(){
try{
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
skeySpec = new SecretKeySpec(key.getBytes(), "AES");
ivSpec = new IvParameterSpec(iv.getBytes());
cipher = Cipher.getInstance(algorithm);
}catch(NoSuchAlgorithmException | NoSuchPaddingException ex){
}
}
public static String encrypt(String str){
try{
Integer strL = (int) Math.ceil(str.length() / 8.0);
Integer strB = strL*8;
str = padRight(str, '', strB);
setUp();
try {
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
} catch (InvalidAlgorithmParameterException ex) {
return "";
}
byte[] enc = cipher.doFinal(str.getBytes());
return new String(Base64.encodeBase64(enc));
}catch(InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex){
return "";
}
}
public static String padRight(String msg, char x, int l) {
String result = "";
if (!msg.isEmpty()) {
for (int i=0; i<(l-msg.length()); i++) {
result = result + x;
}
result = msg + result;
}
return result;
}
}
I still cant get the right encryption. Anyone can help or give suggestion?