简体   繁体   English

Java中的RSA加密/解密

[英]RSA Encrypt/Decrypt in Java

I am new to Cryptography. 我是密码学的新手。 I was provided with a RSA public key in base64 format. 为我提供了base64格式的RSA公钥。 Is there any way to encrypt some text using just the public key. 有什么方法可以仅使用公共密钥来加密某些文本。 Do I need modulus/exponent too? 我也需要模数/指数吗?

Any pointers would be very helpful. 任何指针将非常有帮助。 Thanks 谢谢

I was interested as well, I went "Google-ing" and found this interesting page. 我也很感兴趣,我去了“ Google-ing”并找到了这个有趣的页面。 It is a lot of information, but very interesting in my opinion. 这是很多信息,但我认为非常有趣。

http://www.di-mgt.com.au/rsa_alg.html http://www.di-mgt.com.au/rsa_alg.html

Saying it is in base64 format is not enough information. 仅以base64格式表示信息不足。 There are at least two common ways that an RSA public key may be formatted, and either one may be base64 encoded. RSA公钥的格式至少有两种常见的方式,而任一种都可以被base64编码。 If you are lucky, the key is an X509EncodedKeySpec. 如果幸运的话,密钥是X509EncodedKeySpec。 If so, you need to base64 decode it, create a KeyFactory , then use the KeyFactory to generate the public key. 如果是这样,则需要对base64进行解码,创建一个KeyFactory ,然后使用KeyFactory生成公共密钥。 Here some untested, not even compiled, code that hopefully shows these steps. 这里有一些未经测试甚至没有编译的代码有望显示这些步骤。

// Example using the base64 class from http://iharder.sourceforge.net/current/java/base64/

byte [] x509Key = Base64.decode(base64Key);
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKey rsaPub = (RSAPublicKey) kf.generatePublic();

If there are -----BEGIN and ----END lines surrounding your base64 key just delete them. 如果base64键周围有----- BEGIN和---- END行,则将其删除。

The public key is used to decrypt ciphertext which has been encrypted by the private key. 公钥用于解密已由私钥加密的密文。

Each user that want to communicate has a pair of cryptographic keys—a public encryption key and a private decryption key. 每个要通信的用户都有一对加密密钥-公共加密密钥和私有解密密钥。

You can encrypt using the public key becuse the keys are derived mathematically, but parameters are chosen so that determining the private key from the public key is prohibitively expensive. 您可以使用公共密钥进行加密,因为密钥是通过数学方法得出的,但是选择了参数,因此从公共密钥确定私钥的成本过高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM