非法块大小异常解密时使用填充密码解密时,输入长度必须为16的倍数

时间:2018-09-25 00:56:23

标签: java encryption

当我尝试解密已加密的消息时出现此错误。

为此,我首先加密了一条消息,在本例中为“测试消息”

然后再次运行该方法,但解密而不是加密。

我已经查看了与此相关的其他问题,但无法解决问题

import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

public class PBEusing {

    public static void main(String[] args) throws Exception {
        PBEusing pbe = new PBEusing();
        String encrypt = pbe.pbe("encrypt", "passwordTest", "Testing message");
        System.out.println(encrypt);

        String decrypt = pbe.pbe("decrypt", "passwordTest", encrypt);
        System.out.println(decrypt);
    }

    public static String pbe(String cipherMethod, String clientPassword, String clientMessage) throws Exception {

        String method = cipherMethod.toUpperCase();
        String output = "";

        SecureRandom rnd = new SecureRandom();
        byte[] iv = new byte[16];
        rnd.nextBytes(iv);

        byte[] plaintext = clientMessage.getBytes(StandardCharsets.UTF_8); // input message from user
        byte[] salt = "01234567".getBytes(StandardCharsets.UTF_8);
        IvParameterSpec ivParamSpec = new IvParameterSpec(iv);
        PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 10000, ivParamSpec);
        PBEKeySpec keySpec = new PBEKeySpec(clientPassword.toCharArray());
        try {
            SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_128");
            SecretKey secretKey = kf.generateSecret(keySpec);

            // On J2SE the SecretKeyfactory does not actually generate a key, it just wraps the password.
            // The real encryption key is generated later on-the-fly when initializing the cipher
            System.out.println(new String(secretKey.getEncoded()));

            // Encrypt
            if (method.equals("ENCRYPT")) {
                Cipher enc = Cipher.getInstance("PBEWithHmacSHA256AndAES_128");
                enc.init(Cipher.ENCRYPT_MODE, secretKey, pbeParamSpec);
                byte[] encrypted = enc.doFinal(plaintext);
                output = new BASE64Encoder().encode(encrypted);
                System.out.println("Encrypted text: " + output);
            } else {

                // Decrypt
                Cipher dec = Cipher.getInstance("PBEWithHmacSHA256AndAES_128");
                dec.init(Cipher.DECRYPT_MODE, secretKey, pbeParamSpec);
                byte[] decrypted = dec.doFinal(plaintext);
                String test = new BASE64Encoder().encode(decrypted);

                //String message = new String(test, StandardCharsets.UTF_8);
                output = test;
            }

        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        }
        return output;
    }
}

1 个答案:

答案 0 :(得分:0)

您正在对加密输出进行Base64编码,但是在尝试解密之前不会再次对Base64进行解码。