线程“main”中的异常java.security.UnrecoverableKeyException:给定最终块未正确填充

时间:2014-12-24 10:55:16

标签: java encryption

大家好我使用AES加密,我所做的是我在文本文件中加密数据并存储给定位置,解密工作正常,如果在同一个类文件中给出,我创建了一个不同的java用于解密文件的类,我使用带有用户名和密码的Javakeystore存储密钥并检索它并使用存储的密钥进行解密,但是我收到了上述错误。帮帮我们。这是解密的代码。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.security.KeyStore;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import de.flexiprovider.core.FlexiCoreProvider;

public class Decrypto {

    public static void main(String[] args) throws Exception {
        Security.addProvider(new FlexiCoreProvider());

        /*
         * Cipher cipher1 = Cipher.getInstance("AES128_CBC", "FlexiCore");
         * KeyGenerator keyGen = KeyGenerator.getInstance("AES", "FlexiCore");
         * SecretKey secKey = keyGen.generateKey();
         * System.out.println(secKey);
         */
        Cipher cipher1 = Cipher.getInstance("AES128_CBC", "FlexiCore");
        KeyStore keyStore = KeyStore.getInstance("JCEKS");

        FileInputStream fis = new FileInputStream("C:\\mykey.keystore"); // here
                                                                         // i am
                                                                         // uploading
        keyStore.load(fis, "javaci123".toCharArray());
        fis.close();
        Key secKey = (Key) keyStore.getKey("mySecretKey",
                "javaci123".toCharArray()); // line 35

        System.out.println("Found Key: " + (secKey));

        String cleartextFile = "C:\\cleartext.txt";
        String ciphertextFile = "C:\\ciphertextSymm.txt";

        // FileInputStream fis = new FileInputStream(cleartextFile);
        FileOutputStream fos = new FileOutputStream(ciphertextFile);

        String cleartextAgainFile = "C:\\cleartextAgainSymm.txt";

        cipher1.init(Cipher.DECRYPT_MODE, secKey);
        fis = new FileInputStream(ciphertextFile);

        // fis = new FileInputStream(ciphertextFile);
        CipherInputStream cis = new CipherInputStream(fis, cipher1);
        fos = new FileOutputStream(cleartextAgainFile);
        byte[] block = new byte[8];
        int i;
        while ((i = fis.read(block)) != -1) {
            cis.read(block, 0, i);
        }
        cis.close();
    }

}

错误

  Exception in thread "main" java.security.UnrecoverableKeyException: Given final block not     properly padded
   at com.sun.crypto.provider.KeyProtector.unseal(KeyProtector.java:360)
  at com.sun.crypto.provider.JceKeyStore.engineGetKey(JceKeyStore.java:133)
  at java.security.KeyStore.getKey(Unknown Source)
  at darm.code.com.Decrypto.main(Decrypto.java:35)

2 个答案:

答案 0 :(得分:4)

UnrecoverableKeyException表示问题,特别是如果根本原因是"Given final block not properly padded"。这基本上意味着您的密码不正确。 KeyStore将首先从给定密码生成密钥,并使用该密钥解密存储的密钥。如果解密失败,您希望MAC身份验证错误,但在这种情况下,您会收到填充错误(这基本上意味着某人忘记向包含包装私钥的容器添加完整性保护)。

答案 1 :(得分:0)

我在第35行找到了问题的答案

Key secKey = (Key) keyStore.getKey("mySecretKey",
        "javaci123".toCharArray()); // line 35

对于加密我还设置了密码密码,即密码的填充是pw-password

Key secKey = (Key) keyStore.getKey("mySecretKey",
        "pw-password".toCharArray()); // line 35

运行后我得到保存的密钥来解密我错过的简单逻辑。

找到钥匙:*********** 6fd ****** 5 **********