256bit AES/CBC/PKCS5Padding with Bouncy Castle

前端 未结 1 626
孤城傲影
孤城傲影 2020-12-13 11:03

I am having trouble mapping the following JDK JCE encryption code to Bouncy Castles Light-weight API:

public String dec(String password, String salt, String          


        
相关标签:
1条回答
  • 2020-12-13 11:51

    This should work for you:

    public String dec(String password, String salt, String encString)
            throws Exception {
    
        byte[] ivData = toByte(encString.substring(0, 32));
        byte[] encData = toByte(encString.substring(32));
    
        // get raw key from password and salt
        PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(),
                toByte(salt), 50, 256);
        SecretKeyFactory keyFactory = SecretKeyFactory
                .getInstance("PBEWithSHA256And256BitAES-CBC-BC");
        SecretKeySpec secretKey = new SecretKeySpec(keyFactory.generateSecret(
                pbeKeySpec).getEncoded(), "AES");
        byte[] key = secretKey.getEncoded();
    
        // setup cipher parameters with key and IV
        KeyParameter keyParam = new KeyParameter(key);
        CipherParameters params = new ParametersWithIV(keyParam, ivData);
    
        // setup AES cipher in CBC mode with PKCS7 padding
        BlockCipherPadding padding = new PKCS7Padding();
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                new CBCBlockCipher(new AESEngine()), padding);
        cipher.reset();
        cipher.init(false, params);
    
        // create a temporary buffer to decode into (it'll include padding)
        byte[] buf = new byte[cipher.getOutputSize(encData.length)];
        int len = cipher.processBytes(encData, 0, encData.length, buf, 0);
        len += cipher.doFinal(buf, len);
    
        // remove padding
        byte[] out = new byte[len];
        System.arraycopy(buf, 0, out, 0, len);
    
        // return string representation of decoded bytes
        return new String(out, "UTF-8");
    }
    

    I assume that you're actually doing hex encoding for toByte() since your code uses 32 characters for the IV (which provides the necessary 16 bytes). While I don't have the code you used to do the encryption, I did verify that this code will give the same decrypted output as your code.

    0 讨论(0)
提交回复
热议问题