java - 解密异常

标签 java security encryption rsa

我正在尝试从 Alice 和 Bob 生成共享 key 。我使用 key 生成器来获取鲍勃和爱丽丝的公钥和私钥。然后,我使用 AES 生成了一个 key 。我应该做的是加密 key 然后解密它,它应该与原始 key 相同。但是,我的代码给了我一个异常,提示“解密错误”。我不明白为什么。谁能帮我?谢谢你!

此外,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());

    }
}

最佳答案

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

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

关于java - 解密异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49808193/

相关文章:

java - 单击按钮的上半部分 - 不起作用?

java - 如何修复 Apache Tomcat 6 服务器中的 java spring mvc web 应用程序运行时间?

java - 带提前返回语句的算法逐行分析

iphone - 在 iPhone 上生成 key 对并打印以记录为 NSString

java - 将我的java应用程序与fosuserbundle的表用户同步

node.js - Node Google Cloud KMS 加密似乎有效,但解密失败

java - 在 Linux 上运行时如何为 Chrome 驱动程序设置自定义 TMPDIR?

php - 如何防止 PHP 中的 SQL 注入(inject)?

matlab - MATLAB 编译器的安全性

c# - Java BouncyCaSTLe AES 解密的 C# 等价物是什么?