gpt4 book ai didi

java - 混合加密方法失败

转载 作者:行者123 更新时间:2023-12-01 11:27:43 24 4
gpt4 key购买 nike

我在尝试使用 AES 和 RSA 混合加密创建一个小程序时遇到了麻烦。它仅使用我尝试过的对称加密 AES 就可以正常工作,但是当我尝试实现 RSA 来包装 AES 加密消息和 key 时,我无法让它工作。

我知道这样的程序可以使用输出流或密码流来完成,但如果可能的话,我想以这种方式解决它。换句话说,就像用户在 JOptionInputDialog 中输入任何字符串并使用 AES key 对其进行加密,然后使用 RSA 公钥对 AES key 进行加密,然后使用 RSA 私钥进行解密。

我希望答案显示在同一个 JOptionPane 窗口中。例如:

密文:jfjfjh解密后的文本:你好

我现在无法理解如何使用 RSA 私钥解密该字符串。我不知道我错过了什么或做错了什么。从过去一周半以来我一直在谷歌搜索的任何例子来看,我认为它看起来不错。我一定错过了我眼前的一些东西,但我看不到它,因为我坐了太多小时试图找到不同的方法(我想我改变了它一百万次,但我无法向你展示我所有的方法,所以这个是我与您分享的。我非常感谢任何形式的帮助。所以这是我的代码:(希望您能理解,因为有些单词是我的语言)

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.JOptionPane;

/**
*
* @author Patricia
*/
public class EncryptionDecryption {

/**
* @param args the command line arguments
* @throws java.lang.Exception
*/
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException{

//Creating assymmetric keys RSA ( A public Key and Private Key )
KeyPairGenerator gen2 = KeyPairGenerator.getInstance("RSA");
gen2.initialize(1024);

KeyPair keyPair = gen2.genKeyPair();
PrivateKey privatnyckel = keyPair.getPrivate();
PublicKey publiknyckel = keyPair.getPublic();



//Create assymmetric key AES //Create key generator
KeyGenerator gen = KeyGenerator.getInstance("AES"); //Returns keygenerator object that generates key for specified algorithm in this case AES

SecureRandom random = new SecureRandom();
gen.init(random);
// create a key
SecretKey AES = gen.generateKey();
//get the raw key bytes
byte[] symmetriskNyckel =AES.getEncoded(); //Returns copy of the key bytes

//Create cipher based upon AES, encrypt the message with AES key
Cipher cipher = Cipher.getInstance("AES");
//Initialize cipher with secret key AES
cipher.init(Cipher.ENCRYPT_MODE, AES);

// get the text to encrypt with AES key
String inputText1 = JOptionPane.showInputDialog(" Enter the secret message");
//Encrypt the plaintext message you wanna send with the symmetric key AES;
byte[] kryptera = cipher.doFinal(inputText1.getBytes());

//encrypt AES key with RSA
Cipher pipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
pipher.init(Cipher.WRAP_MODE, publiknyckel);
byte[] krypteradAESNyckel = cipher.wrap(AES);

JOptionPane.showMessageDialog(null, "AES key encrypted with RSA public key " + krypteradAESNyckel);

// re-initialise the cipher to be in decrypt mode
Cipher flipher = Cipher.getInstance("RSA");
flipher.init(Cipher.UNWRAP_MODE, privatnyckel );
// decrypt message
byte [] dekryptera = flipher.unwrap(kryptera);



JOptionPane.showMessageDialog
(null, "AES symmetrisk nyckel " +symmetriskNyckel );





// and display the results //
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
"Texten krypterad " + new String(kryptera) + "\n"
+ "Text dekrypterat: " + new String(dekryptera));




JOptionPane.showMessageDialog(null, "\n RSA assymmetrisk privat nyckel " + privatnyckel
+ "RSA assymmetrisk publik nyckel" + publiknyckel);




// end example
System.exit(0);






}

}

最佳答案

我认为你的变量名称和注释很好开始。当处理新事物时,在代码中给自己写注释。一旦它起作用了,你可以稍后再回来,用丰富、有意义的名称进行重构,并修剪注释以使其清晰,以便它们在六个月或一年后对你说话,提醒你当时你在想什么。写了这个。 (我认为瑞典名字和英语名字一样好。)

正如 JB Nizet 指出的那样,当您应该解包 krypteradAESNyckel 中包含的对称 key 时,您正在解包 kryptera。接下来,您将使用恢复的对称 key 解密 kryptera。 (您的代码只是输出早期的 symmetriskNyckel,而没有实际从 krypteradAESNyckel 中将其解开。)

我还注意到在一种情况下你

Cipher.getInstance("RSA/ECB/PKCS1Padding"); 

但后来你

Cipher.getInstance("RSA"); 

我会让它们保持一致,都是“RSA/ECB/PKCS1Padding”。对细节的关注与清晰的变量名称和有意义的注释一样重要。

关于java - 混合加密方法失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30687666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com