【问题标题】:Decryption Exception解密异常
【发布时间】:2018-04-13 01:57:31
【问题描述】:

我正在尝试从 Alice 和 Bob 生成一个共享密钥。我使用密钥生成器来获取 Bob 和 Alice 的公钥和私钥。然后,我使用 AES 生成了一个密钥。我应该做的是加密密钥然后解密它,它应该与原始密钥相同。但是,我的代码给了我一个异常,上面写着“解密错误”。我不知道为什么。谁能帮我?谢谢!

此外,decodeBytes 和 generateSharedKey 应该是相等的,我也遇到了麻烦。

import java.security.*;
import java.util.UUID;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class rsa {
    static SecretKey sharedKey = null;
    public static void main(String[] args) throws NoSuchAlgorithmException, 
    NoSuchProviderException, InvalidKeyException, NoSuchPaddingException, 
    IllegalBlockSizeException, BadPaddingException {


        //Generates Key Pair -> a private and public key for both Alice and 
         Bob
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048);

        //Alice's key pair
        KeyPair aliceKey = keyGen.genKeyPair();
        PublicKey pubAlice = aliceKey.getPublic(); //alice's public key
        PrivateKey privAlice = aliceKey.getPrivate();//alice's private key

        //Bob's key pair
        KeyPair bobKey = keyGen.genKeyPair();
        PublicKey pubBob = bobKey.getPublic(); //bob's public key
        PrivateKey privBob = bobKey.getPrivate();// bob's private key

        //Generates a random key and encrypt with Bob's public Key
        System.out.println(generateSharedKey());

        byte[] keyEncrypted = encrypt(sharedKey, pubBob);

        byte[] decodeBytes = decrypt(sharedKey, privBob);
        System.out.println(decodeBytes);
        }

    public static SecretKey generateSharedKey() throws 
    NoSuchAlgorithmException{ 
        KeyGenerator sharedKeyGen = KeyGenerator.getInstance("AES"); 
        sharedKey = sharedKeyGen.generateKey();
        return sharedKey;
    }

    public static byte[] encrypt(SecretKey sharedKey, PublicKey pubKey) 
    throws NoSuchAlgorithmException, NoSuchPaddingException, 
    InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        //System.out.println(inputBytes);

        return cipher.doFinal(generateSharedKey().getEncoded());   
    }

    public static byte[] decrypt(SecretKey sharedKey, PrivateKey privKey) 
  throws NoSuchAlgorithmException, NoSuchPaddingException, 
    InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privKey);
        return cipher.doFinal(generateSharedKey().getEncoded());

    }
}

【问题讨论】:

  • 你能发布异常的整个回溯吗?
  • 这是整个异常:线程“main”中的异常 javax.crypto.BadPaddingException:sun.security.rsa.RSAPadding.unpadV15 (Unknown Source) at sun.security.rsa.RSAPadding 的解密错误.unpad(未知来源)在 com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363) 在 com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389) 在 javax.crypto.Cipher .doFinal(Cipher.java:2165) 在 rsa.rsa.decrypt(rsa.java:64) 在 rsa.rsa.main(rsa.java:38)

标签: java security encryption rsa


【解决方案1】:

请在发布之前仔细阅读您的代码...您正在生成一个新共享密钥,然后尝试在您的 decrypt 方法中对其进行解密...

您应该传入byte[] 来解密并返回SecretKey,而不是相反。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 2020-12-19
    • 1970-01-01
    • 2013-04-22
    相关资源
    最近更新 更多