任何关于 Java 中公钥加密的教程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/338578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any tutorials on public key encryption in java?
提问by Eugene M
I've been able find information on symmetric encryption and hashing but I've been having quite a bit of trouble finding much information on any sort of public key encryption for java. What I'd like to do is make a very simple proof of concept program that takes a string ( or a file I suppose), encrypts it with a public key and then decrypts it with a private key.
我已经能够找到关于对称加密和散列的信息,但是我在寻找关于任何类型的 java 公钥加密的信息时遇到了很多麻烦。我想做的是制作一个非常简单的概念证明程序,它接受一个字符串(或者我想是一个文件),用公钥对其进行加密,然后用私钥对其进行解密。
Any tutorial links or examples would be appreciated. I just want to make something demonstrating how you can use public key encryption in Java.
任何教程链接或示例将不胜感激。我只想做一些事情来演示如何在 Java 中使用公钥加密。
采纳答案by Loki
There are quite a few resources on the web about that. Basically it turns around the KeyPairGenerator class.
网上有很多关于这方面的资源。基本上它围绕 KeyPairGenerator 类。
See http://www.informit.com/articles/article.aspx?p=170967&seqNum=4for an example program.
有关示例程序,请参见http://www.informit.com/articles/article.aspx?p=170967&seqNum=4。
回答by erickson
Normally, you use public key encryption to encrypt a symmetric key, in part because public key encryption is very slow. Typically, you'd send the recipient the following, so that they can decrypt your message:
通常,您使用公钥加密来加密对称密钥,部分原因是公钥加密非常慢。通常,您会向收件人发送以下内容,以便他们可以解密您的消息:
- The symmetric key, encrypted with the recipient's public key.
- Parameters for the algorithms used, usually an initialization vector for the symmetric cipher.
- Identifiers for the encryption algorithms used.
- The ciphertext—the actual message, encrypted under the symmetric cipher.
- 对称密钥,使用接收者的公钥加密。
- 所用算法的参数,通常是对称密码的初始化向量。
- 所用加密算法的标识符。
- 密文 - 实际消息,在对称密码下加密。
I found the sample code in the JCE documentationsufficient to get things working.
我发现JCE 文档中的示例代码足以让事情正常工作。
The standard format for bundling all of this information up is the Cryptographic Message Syntax, or CMS, which is used by S/MIME in email applications. I recommend using Bouncy Castle's libraries; they are solid, fairly simple, and actively maintained. The reference documentation is a bit weak, but they do provide code examples.
捆绑所有这些信息的标准格式是加密消息语法或 CMS,S/MIME 在电子邮件应用程序中使用它。我建议使用Bouncy Castle 的库;它们是可靠的,相当简单的,并且积极维护。参考文档有点薄弱,但它们确实提供了代码示例。