线程“主”中的异常java.security.UnrecoverableKeyException:给定最终块未正确填充 [英] Exception in thread "main" java.security.UnrecoverableKeyException: Given final block not properly padded

查看:128
本文介绍了线程“主”中的异常java.security.UnrecoverableKeyException:给定最终块未正确填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所有我是usig 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());

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

FileInputStream fis = new FileInputStream(C:\\\mykey.keystore); //这里
//我是
//上传
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();
}

}

错误

 线程main中的异常java.security.UnrecoverableKeyException:给定最终块未正确填充
在com.sun.crypto.provider.KeyProtector .com(.com)($) $ b at darm.code.com.Decrypto.main(Decrypto.java:35)


解决方案

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

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

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

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

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

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


Hi all i am usig AES for encryption, what i have done is i encrypted a data in a text file and and stored the a given location, decryption works fine if given in the same class file, i have created a different java class to decrypt the file, I am using the Javakeystore with username and password to store the keys and retrieve it and use the stored key to decrypt but i am getting the above error. Help me out guys. Here is the code for decryption.

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();
    }

}

error

  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)

解决方案

I found the answer for the issue in line 35

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

For encryption I had also set a password for the password i.e padding for the password is pw-password

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

after running I get the saved key to decrypt a simple logic I missed.

Found Key: ***********6fd******5**********

这篇关于线程“主”中的异常java.security.UnrecoverableKeyException:给定最终块未正确填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆