当前位置: 首页 > 知识库问答 >
问题:

BadPaddingException:尝试用私钥解密RSA字符串时出现解密错误

贝钧
2023-03-14

我想在我的项目上使用RSA/AES文件加密。我用rsa做了一对私钥/公钥和一个AES密钥,然后用公钥加密AES密钥。最后用私有RSA密钥对AES加密密钥进行解密。

    public class EncriptionRSAKey {
    public static final int RSA_Key_Size = 512; // dimensione chiave RSA
    private PrivateKey privKey;// chiave privata
    private PublicKey pubKey;// chiave pubblica

    /**
     * Costruttore che genera una chiave privata e una pubblica in RSA
     * @throws NoSuchAlgorithmException
     */
    public EncriptionRSAKey() throws NoSuchAlgorithmException{
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(RSA_Key_Size);
        privKey = keyGen.genKeyPair().getPrivate();
        pubKey = keyGen.genKeyPair().getPublic();
    }


    /**
     * Decripta una chiave RSA cifrata con una chiave pubblica
     * @param data chiave AES
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public SecretKey decryptAESKey(byte[] data ) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
    {
        SecretKey key = null;
        Cipher cipher = Cipher.getInstance("RSA");;
        cipher.init(Cipher.DECRYPT_MODE, privKey ); // inizializza il cipher
        key = new SecretKeySpec ( cipher.doFinal(data), "AES" ); // genera la chiave AES
        return key;
    }

    /**
     * Restituisce la chiave pubblica
     * @return pubKey chiave pubblica
     */
    public PublicKey getPubKey() {
        return pubKey;
    }
For AES key:

    public class EncriptionAESKey {
    public static final int AES_Key_Size = 256; // dimensione chiave AES

    /**
     * Genera una chiave AES
     * @return aesKey Chiave AES
     * @throws NoSuchAlgorithmException
     */
    public static SecretKey makeAESKey() throws NoSuchAlgorithmException {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(AES_Key_Size); 
        return keyGen.generateKey();
    }

    /**
     * Cifra una chiave AES data una chiave pubblica RSA
     * @param skey Chiave AES da cifrare
     * @param publicKey Chiave RSA pubblica
     * @return key AES cifrato con RSA
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static byte[] EncryptSecretKey (SecretKey skey, PublicKey publicKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
    {
        Cipher cipher = null;
        byte[] key = null;
        // initialize the cipher with the user's public key
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey );
        key = cipher.doFinal(skey.getEncoded());
        return key;
    }
    public class EncriptionTest {

    @Test
    public void testAESEncription() {
        try {
            EncriptionRSAKey rsa = new EncriptionRSAKey();
            SecretKey aes = EncriptionAESKey.makeAESKey();
            byte[] aesEnc = EncriptionAESKey.EncryptSecretKey(aes, rsa.getPubKey());
            assertEquals(rsa.decryptAESKey(aesEnc),aes);


        } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
            fail();
        }
    }
Gives me this Exception:`javax.crypto.BadPaddingException: Decryption error
    at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
    at sun.security.rsa.RSAPadding.unpad(Unknown Source)
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
    at javax.crypto.Cipher.doFinal(Cipher.java:2165)
    at model.EncriptionRSAKey.decryptAESKey(EncriptionRSAKey.java:54)
    at test.EncriptionTest.testAESEncription(EncriptionTest.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)`

共有1个答案

罗业
2023-03-14

这是你的bug:

privKey = keyGen.genKeyPair().getPrivate();
pubKey = keyGen.genKeyPair().getPublic();

每次调用keygen.genKeyPair()时,都会生成一个新的keypair。因此,privkey最终作为一个小键盘的私钥,pubkey最终作为一个完全不相关的小键盘的公钥。相反,首先将小键盘保存在一个变量中,例如。

KeyPair keyPair = keyGen.genKeyPair();
privKey = keyPair.getPrivate();
pubKey = keyPair.getPublic();
 类似资料:
  • 我有一个公钥和一个私钥,还有一个字符串,我想要解密。 公钥的格式如下: 私钥的格式如下: 我要解密的字符串已经使用公钥加密,然后我需要使用私钥解密它。

  • 问题内容: 因此,我一直在尝试使用带有node-rsa的node和带有jsencrypt的 javascript 创建一个网站(用于分配),其中javascript客户端获取服务器生成的公共密钥(node- rsa),对消息进行加密(jsencrypt)用户输入的密码,将其发送到服务器,并让服务器对其进行解密(node- rsa)。密钥的生成有效,而加密有效,但是解密无效。启动节点脚本时,请执行以

  • 今天,我写了一些代码,用AES加密字符串,用RSA加密密钥。当我试图解密所有内容时,Java给了我一个BadPaddingException。这是我的代码: 测验爪哇: 钥匙匠。爪哇: 加密机。爪哇: 解密程序。爪哇:

  • 在一个做其他事情的大型应用程序中——我需要加密和解密一个文件。所以我一直在四处寻找,并实现了这两个核心功能,基本上使用RSA密钥包装一个随机的AES密钥来加密一个文件。对称键和iv被写入文件的开头。 我在下面的解密函数部分得到一个异常(“javax.crypto.BadPaddingException:Decryption error”)。在肯安迪夫线路上——doFinal。具体来说,这一行是异常

  • 尝试将数据解密为用AES-128加密的字节数组,使用字符串密钥"keykeykeykey1" 代码: 给我BadPaddingExc0019。我错过了什么?