0
votes

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?

1
i have tested your code and i have an exception. you have tested your code ? no exception ?EL missaoui habib
the problem, i just cant get the right result like the example that given to me. The code already cut for the reason make it short sir, you can not just copy paste to running it.lendir

1 Answers

0
votes

Guessing from the given input, you should run into Javas restriction of the key length: Since the US doesn't allow usage of hard security keys, Java is restricted to a key length of 128bit per default.

To enable keys >128bit you have to change the policy of your Java version with the offical "unlimited" policies (here for SE8)

Overwriting the current policies in lib/security with the download should be enough.