使用PGP使用javamail发送加密邮件

时间:2013-07-01 12:20:57

标签: java javamail bouncycastle pgp

我尝试使用PGP加密邮件时出错了

错误:

**java.security.NoSuchProviderException: No provider configured for PGP**
at    net.suberic.crypto.EncryptionManager.getEncryptionUtils(EncryptionManager.java:106)
at    tools.Encryption.encrypt(Encryption.java:29)
at    core.mails.HtmlSeriesUploadingMail.sendMail(HtmlSeriesUploadingMail.java:93)
at    core.services.MailService.run(MailService.java:71)
at    java.lang.Thread.run(Unknown Source)

我看到我在类路径中需要一些库,所以这是我的类路径:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="lib/mail.jar"/>
<classpathentry kind="lib" path="lib/wrapper.jar"/>
<classpathentry kind="lib" path="lib/mysql.jar"/>
<classpathentry kind="lib" path="lib/pd4ml.jar"/>
<classpathentry kind="lib" path="lib/ini4j-0.5.1.jar"/>
<classpathentry kind="lib" path="lib/jdom-2.0.4.jar"/>
<classpathentry kind="lib" path="lib/bcmail-jdk14-139.jar"/>
<classpathentry kind="lib" path="lib/bcprov-jdk14-139.jar"/>
<classpathentry kind="lib" path="lib/javamail-crypto_060622.jar"/>
<classpathentry kind="lib" path="lib/javamail-crypto-bouncycastle-smime.jar"/>
<classpathentry kind="lib" path="lib/ss_css2.jar"/>
<classpathentry kind="lib" path="lib/certpath-api-compat.jar"/>
<classpathentry kind="lib" path="lib/cryptix-message-api.jar"/>
<classpathentry kind="lib" path="lib/cryptix-openpgp-provider.jar"/>
<classpathentry kind="lib" path="lib/cryptix-pki-api.jar"/>
<classpathentry kind="lib" path="lib/cryptix-jce-provider-2.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

这是我的加密代码:

   public MimeMessage encrypt(Session session, MimeMessage mimeMessage, InternetAddress recipient) throws Exception {
      // get the PGP EncryptionUtilities
      EncryptionUtils pgpUtils = EncryptionManager.getEncryptionUtils(EncryptionManager.PGP);
      // load the PGP keystore from the given file.
      EncryptionKeyManager pgpKeyMgr = pgpUtils.createKeyManager();
      pgpKeyMgr.loadPublicKeystore(new FileInputStream(new File(SystemData.getWatchDogConfig() + "test.asc")), null);
      // get the PGP public key for encryption
      java.security.Key pgpKey = pgpKeyMgr.getPublicKey((String) pgpKeyMgr.publicKeyAliases().iterator().next());
      // encrypt the message
      return pgpUtils.encryptMessage(session, mimeMessage, pgpKey);
   }

我不知道我应该做什么......我从javamail和bouncycastle开始。

2 个答案:

答案 0 :(得分:0)

查看此链接,它显示了如何使用Bouncycastle进行PGP加密:http://fastpicket.com/blog/2012/05/14/easy-pgp-in-java-bouncy-castle/

答案 1 :(得分:0)

NoSuchProviderException在找不到EncryptionManager中指定的提供程序时抛出异常getEncryptionUtils。通过从文件EncryptionManager中检索提供程序,可以在META-INF/javamail-crypto.providers类中静态加载提供程序。因此,为了EncryptionManager加载PGP提供程序,您必须使用以下行将javamail-crypto.providers文件添加到META-INF文件夹中:

protocol=PGP;class=net.suberic.crypto.PGPEncryptionUtils