如何在没有私钥的情况下获得将邮件加密到Java的证书?
我想用java发送加密邮件。1.6.4版似乎很流行这样做。在他们的示例CreateLargeEncryptedMail.java中,您可以找到:如何在没有私钥的情况下获得将邮件加密到Java的证书?,java,x509certificate,public-key-encryption,Java,X509certificate,Public Key Encryption,我想用java发送加密邮件。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.PKCS12Exam
/**
* 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。从
返回与给定别名关联的证书链。证书链必须通过调用setKeyEntry或使用PrivateKeyEntry调用setEntry与别名关联
但是我没有私钥
另一种方法是使用CertificateFactory.getInstanceX.509
但我只是来
java.security.cert.CertificateParsingException:签名字段无效
发现该异常,但解决方案再次使用KeyStore.getCertificate
我拥有:适用于Windows trust store中SMIME的证书。证书在outlook中工作。我可以将证书导出到文件中
我想要:一个类型为Certificate X509Certificate的java对象,使用BounceCastle为SMIME工作
那么,为了初始化这个X509证书,我必须使用哪个工具创建什么样的文件,以及在Java中要做什么?我需要那个文件中的单个证书还是链?证书是自签名的。BouncyCastle不仅支持SMIME加密,还包含一个CertificateFactory,它可以加载我从Windows certmgr导出的p7b文件。对于导出,我选择不带私钥和密钥链。该文件适用于我,使用:
import org.bouncycastle.jcajce.provider.asymmetric.x509.CertificateFactory;
...
/**
* Reads the Certificate from the file with filename.
* Works for p7b-files.
* @param filename the name and path of a key-file.
* @return a Certificate
*/
public static Certificate getCertificate(String filename) {
Certificate cert = null;
try (InputStream is = new FileInputStream(filename)) {
CertificateFactory fact = new CertificateFactory();
cert = fact.engineGenerateCertificate(is);
}
catch (Exception ex) {
ex.printStackTrace();
}
return cert;
}
我想你误解了加密的工作原理。您可以使用私钥加密某些内容,并将公钥提供给任何希望验证邮件是否由您加密的人。您将永远不会拥有客户端私钥,因为私钥永远不能共享。@leopal:我只希望收件人能够阅读我的邮件。因此,它必须是收件人的私钥,这是解密所必需的。我使用收件人的公钥进行加密。