嗨,我正在看维基百科关于公钥密码学公钥密码学的文章
我也看到了这张图片,它展示了如何使用您的私钥和其他人的公钥生成对称密钥密码。生成对称密钥/共享秘密
我已经知道如何在各方之间交换公钥,但是我想知道是否可以使用Java编程语言在图片中实现这个过程。
使用的私钥和公钥将使用RSA生成,生成的密钥/共享秘密将是对称密码的对称密钥(我想使用AES-128)
我理解这背后的理论,但我不知道如何在Java中正确地实现它,任何想法或帮助都会很感激:)
发布于 2014-04-20 21:12:08
尝试KeyAgreement和算法"DiffieHellman"。请注意,您需要Java 8的2048位密钥大小,Java 7及更低版本被卡在1024上(除非安装了弹跳城堡提供程序)。
或者您可以使用"ECDH"算法,但请注意,这需要一些学习曲线。
发布于 2014-04-21 03:35:35
谢谢奥斯特德,我能够编写以下代码,生成RSA密钥,然后尝试使用DiffieHellman将私钥与相反的公钥连接起来
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class AESKeGenFromRSA
{
public static void main(String[] args)
{
try
{
// Generate RSA KeyPair for Alice
Cipher alice_rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// Get RSA KeyPairGenerator Object Instance
KeyPairGenerator alice_gen = KeyPairGenerator.getInstance("RSA");
// Generate RSA Assymetric KeyPair
KeyPair alice_pair = alice_gen.generateKeyPair();
// Extract Public Key
PublicKey alice_pub = alice_pair.getPublic();
// Extract Private Key
PrivateKey alice_pvt = alice_pair.getPrivate();
System.out.println();
System.out.println("Alice Public: " + alice_pub);
System.out.println();
System.out.println("Alice Private: " + alice_pvt);
// Generate RSA KeyPair for Bob
Cipher bob_rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// Get RSA KeyPairGenerator Object Instance
KeyPairGenerator bob_gen = KeyPairGenerator.getInstance("RSA");
// Generate RSA Assymetric KeyPair
KeyPair bob_pair = bob_gen.generateKeyPair();
// Extract Public Key
PublicKey bob_pub = bob_pair.getPublic();
// Extract Private Key
PrivateKey bob_pvt = bob_pair.getPrivate();
System.out.println();
System.out.println("Bob Public: " + bob_pub);
System.out.println();
System.out.println("Bob Private: " + bob_pvt);
// Create KeyAgreement for Alice
KeyAgreement alice_agreement = KeyAgreement.getInstance("DiffieHellman");
alice_agreement.init(alice_pvt);
alice_agreement.doPhase(bob_pub, true);
byte[] alice_secret = alice_agreement.generateSecret();
SecretKeySpec alice_aes = new SecretKeySpec(alice_secret, "AES");
// Create KeyAgreement for Bob
KeyAgreement bob_agreement = KeyAgreement.getInstance("DiffieHellman");
bob_agreement.init(bob_pvt);
bob_agreement.doPhase(alice_pub, true);
byte[] bob_secret = bob_agreement.generateSecret();
SecretKeySpec bob_aes = new SecretKeySpec(bob_secret, "AES");
System.out.println();
System.out.println(alice_aes.equals(bob_aes));
}
catch (NoSuchAlgorithmException e)
{e.printStackTrace();}
catch (NoSuchPaddingException e)
{e.printStackTrace();}
catch (InvalidKeyException e)
{e.printStackTrace();}
}
}这是当我试图运行程序时抛出的异常。我明白为什么会发生这种事,但我不知道该如何解决。
java.security.InvalidKeyException: No installed provider supports this key: sun.security.rsa.RSAPrivateCrtKeyImpl
at javax.crypto.KeyAgreement.chooseProvider(KeyAgreement.java:398)
at javax.crypto.KeyAgreement.init(KeyAgreement.java:464)
at javax.crypto.KeyAgreement.init(KeyAgreement.java:435)
at AESKeGenFromRSA.main(AESKeGenFromRSA.java:45)PrivateKey对象似乎不是KeyAgreement.init(键)函数的有效参数,如果有任何想法,我们将不胜感激。
https://stackoverflow.com/questions/23183991
复制相似问题