“线程中的异常”;“主要”;java.security.UnrecoverableKeyException:给定的最后一个块未正确填充

“线程中的异常”;“主要”;java.security.UnrecoverableKeyException:给定的最后一个块未正确填充,java,encryption,Java,Encryption,大家好,我使用AES进行加密,我所做的是在一个文本文件中加密一个数据,并将数据存储在一个给定的位置,如果在同一个类文件中给出,解密工作正常,我创建了一个不同的java类来解密该文件,我使用带有用户名和密码的Javakeystore来存储密钥和检索密钥,并使用存储的密钥进行解密,但是我得到了上面的错误。帮帮我,伙计们。这是解密的代码 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStrea

大家好,我使用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();
    }

}
错误


UnrecoverableKeyException
可以识别问题,特别是当根本原因是“给定的最后一个块没有正确填充”时。这基本上意味着您的密码不正确。
KeyStore
将首先从给定密码生成一个密钥,并使用该密钥对存储的密钥进行解密。如果解密失败,您希望出现MAC身份验证错误,但在本例中,您会得到一个填充错误(这基本上意味着有人忘记向包含包装私钥的容器添加完整性保护)。

我在第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**************************

我不是有意侮辱您,而是激发您对使用IDE的重新格式化代码功能的兴趣。
Key secKey = (Key) keyStore.getKey("mySecretKey",
        "pw-password".toCharArray()); // line 35