长度较长的字符串的AES解密期间出现BadPaddingException

时间:2019-11-20 11:58:14

标签: java aes

public class AESEncryptionDecryption {

    public static void main(String[] args) throws Exception {
        String message = "FUN dfdf fgfgf dfffgf";
        String randomNo = generateRandom(16); //16digit random number is generated.
        //ENCRYPTION
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        //byte[] ivBytes = Arrays.copyOfRange(randomNo.getBytes("UTF-8"), 0, 16);
        IvParameterSpec iv = new IvParameterSpec(randomNo.getBytes("UTF-8"));
        SecretKeySpec keyspec = new SecretKeySpec(randomNo.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keyspec, iv);
        cipher.update(message.getBytes("UTF-8"));
        byte[] cipherText = cipher.doFinal();

        //DECRYPTION
        byte[] data = Base64.decodeBase64(base64);
        cipher.init(Cipher.DECRYPT_MODE, keyspec, iv);
        cipher.update(cipherText);
        byte[] decryptedMessage = cipher.doFinal();
        System.out.println("Decrypted Message :: " + new String(decryptedMessage,"UTF-8"));
    }


}

我只收到长度较长的字符串的例外情况,较小长度的字符串可以正常工作。

1 个答案:

答案 0 :(得分:0)

import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class PasswordEncrypt {
    public static String ALGORITHM = "AES";
    private static String AES_CBS_PADDING = "AES/CBC/PKCS5Padding";
    private static int AES_128 = 128;


    private static byte[] encryptDecrypt(final int mode, final byte[] key, final byte[] IV, final byte[] message)
        throws Exception {
    final Cipher cipher = Cipher.getInstance(AES_CBS_PADDING);
    final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
    final IvParameterSpec ivSpec = new IvParameterSpec(IV);
    cipher.init(mode, keySpec, ivSpec);
    return cipher.doFinal(message);
}

public static Map<String, SecretKey> keyGenerator() throws NoSuchAlgorithmException{
    Map<String, SecretKey> map = new HashMap<String, SecretKey>();
     KeyGenerator keyGenerator = KeyGenerator.getInstance(PasswordEncrypt.ALGORITHM);
     keyGenerator.init(AES_128);
     SecretKey key = keyGenerator.generateKey();
     map.put("key", key);
     SecretKey IV = keyGenerator.generateKey();
     map.put("iv", IV);
     return map;

}


public static String encrypt(String message) throws Exception{
    Map<String , SecretKey> map = keyGenerator();
    SecretKey key = map.get("key");
    SecretKey IV = map.get("iv");
    byte[] cipherText = encryptDecrypt(Cipher.ENCRYPT_MODE, key.getEncoded(), IV.getEncoded(), message.getBytes());
    String encrypted_message =  Base64.getEncoder().encodeToString(cipherText);
    String encodedKey = Base64.getEncoder().encodeToString(map.get("key").getEncoded());
    String encodedIV = Base64.getEncoder().encodeToString(map.get("iv").getEncoded());

    return encrypted_message+"javax"+encodedIV+"javax"+encodedKey;



}

public static String decrypt(String encryptedMessage) throws Exception{
    String[] result = encryptedMessage.split("javax");
    byte[] decodedIV = Base64.getDecoder().decode(result[1]);
    byte[] decodedKey = Base64.getDecoder().decode(result[2]);
    byte[] cipher_text = Base64.getDecoder().decode(result[0]);
    SecretKey IV = new SecretKeySpec(decodedIV, 0, decodedIV.length, "AES");
    SecretKey key = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");    
    byte[] decryptedString = encryptDecrypt(Cipher.DECRYPT_MODE, key.getEncoded(), IV.getEncoded(), cipher_text);
    String decryptedMessage = new String(decryptedString);
    return decryptedMessage;

}


public static void main(String[] args) throws Exception {
    PasswordEncrypt cu = new PasswordEncrypt();
    String encryptedmessage =  cu.encrypt("fds fasdf asdf asdf adsf asdrtwe trstsr sdffg sdf sgdfg fsd gfsd gfds gsdf gsdf");
    System.out.println(encryptedmessage);
    String decryptedMessage = cu.decrypt(encryptedmessage);
    System.out.println(decryptedMessage);
}

输入:fds fasdf asdf asdf adsf asdrtwe trstsr sdffg sdf sgdfg fsd gfsd gfds gsdf gsdf    op:h8Auwmfp98BPkIfjrMFt0myxwi / PSy + / g9Js3EOv31WC12mxg3KaimoTfpFO8Sc9e73kOhYpP0eoxigXf4dAXgqCVwqzT6tqUqm + 6aDJK == xxP2K2PxH2P2KP2KP2K2P2K2P2C2P2C4K2P2C2D2C4D0D0