3

I'm trying to make decryption logic and knnow that encrypted string has been made using: Key: 8d6ea4d3e6f8c4f8641516baa5e42b85 transformation: AES/CBC/ISO10126PADDING salt: 1c4dd21d7ba43bdd iterations: 0 Encrypted string: JO0blEp+nEl5nNhgUqoZRJNecogM1XHIXUCatPOJycs=

Key and salt are given samples here..The main point is to show the format in which I have this data. encryption methods is based on the default JCE provider of the JDK (SunJCE).

Now based on this infromation I have above, I'm trying to build Decryption logic. Few doubts: 1. As the AES-265 is used, can it have 128 bits Key and 64 bit salt values? or I'm interpreting the information wrongly. 2. seeing the encrypted string, it looks like it is Base64 encoded value and we need to decode it while decrypting. Is my understanding correct? 3. Below is the decryption logic that I'm writing that is giving error: "javax.crypto.BadPaddingException: Given final block not properly padded" when I call doFinal() function. and I'm struck here from last three days:( . Can you please point out or give me the exact code that to be used here for decryption with having infromation:

    public static void main(String[] args) throws Exception
 {
        String encstring = "JO0blEp+nEl5nNhgUqoZRJNecogM1XHIXUCatPOJycs=";           
        String salt1 = "1c4dd21d7ba43bdd";
        String keyStr = "8d6ea4d3e6f8c4f8641516baa5e42b85";


        byte[] keyBytes = Hex.decodeHex(keyStr.toCharArray());

        SecretKey secret2 = new SecretKeySpec(keyBytes, "AES");

        byte[] iv = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        AlgorithmParameterSpec params = new IvParameterSpec(iv);


        Cipher cipher2 = Cipher.getInstance("AES/CBC/ISO10126PADDING", "SunJCE");


        cipher2.init(Cipher.DECRYPT_MODE, secret2, params);  
        byte[] encryptedString = Base64.decodeBase64(encstring.getBytes());
        byte[] plaintext1 = cipher2.doFinal(encryptedString);

        System.out.println(new String(plaintext));   
        }
    }
4

1 回答 1

-1

先说几个观察:

  • 您说它是AES256(使用256 位密钥),但您的密钥看起来可能是 32 位十六进制数字,提供128 位密钥数据。

  • 你说你有,但 AES 不使用盐。而且您实际上并没有在代码中使用盐。

  • 您谈论0 迭代,但迭代不是您指定给 AES 的东西,它不会是 0。

我的猜测是您的密钥实际上是用于生成密钥的密码。 像这样的东西:

   SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
   KeySpec spec = new PBEKeySpec(password, salt, iterations, keyLength);
   SecretKey theKey = factory.generateSecret(spec);

看看这个问题的答案:Java 256-bit AES Password-Based Encryption

于 2013-12-14T12:55:27.643 回答