【发布时间】:2020-01-20 11:12:42
【问题描述】:
我想用 java 发送一封加密邮件。 BouncyCastle(1.6.4 版)似乎很受欢迎。在他们的示例“CreateLargeEncryptedMail.java”中,您会发现:
/**
* a simple example that creates a single encrypted mail message.
* <p>
* The key store can be created using the class in
* org.bouncycastle.jce.examples.PKCS12Example - the program expects only one
* key to be present in the key file.
* <p>
* Note: while this means that both the private key is available to
* the program, the private key is retrieved from the keystore only for
* the purposes of locating the corresponding public key, in normal circumstances
* you would only be doing this with a certificate available.
*/
public class CreateLargeEncryptedMail
{
public static void main(
String args[])
throws Exception
{
if (args.length != 3)
{
System.err.println("usage: CreateLargeEncryptedMail pkcs12Keystore password inputFile");
System.exit(0);
}
//
// Open the key store
//
KeyStore ks = KeyStore.getInstance("PKCS12", "BC");
String keyAlias = ExampleUtils.findKeyAlias(ks, args[0], args[1].toCharArray());
Certificate[] chain = ks.getCertificateChain(keyAlias);
但是 ks.getCertificateChain() 没有私钥就无法工作,而且我通常没有收件人的私钥。 在我的尝试中,它返回 null。来自documentation
返回与给定别名关联的证书链。证书链必须已通过调用 setKeyEntry 或通过使用 PrivateKeyEntry 调用 setEntry 与别名相关联。
但我没有私钥。
另一种方法是使用CertificateFactory.getInstance("X.509"); is there a way to decrypt smime public key data。
但我只是来
java.security.cert.CertificateParsingException: signed fields invalid
在该异常中找到stackoverflow,但解决方案再次使用KeyStore.getCertificate()。
我有:适用于 Windows 信任库中的 SMIME 的证书。该证书在 Outlook 中有效。我可以将证书导出到文件中。
我想要:使用 BounceCastle 为 SMIME 工作的证书 (X509Certificate) 类型的 java 对象。
那么我必须使用哪种工具创建什么样的文件以及在 Java 中做什么才能初始化这个 X509Certificate?我需要该文件中的单个证书还是链?证书是自签名的。
【问题讨论】:
-
我认为您误解了一般加密的工作原理。您使用您的私钥加密某些内容,并将您的公钥提供给任何想要验证该消息是否已由您加密的人。您永远不会拥有客户的私钥,因为绝对不能共享私钥。
-
@leopal:我希望只有收件人能够阅读我的邮件。所以它必须是解密所必需的收件人私钥。我使用收件人公钥进行加密。
标签: java x509certificate public-key-encryption