java 邮件 加密_基于Java的邮件加密解密

生成密钥安装证书

邮件的加密与解密需要证书。首要任务是生成证书。我们使用.jks生成证书。所以需要创建.jks文件。通过以下命令生成:

keytool -genkey -alias test -keyalg RSA -validity 1825

-keystore "testmail.jks"

-storetype JKS -dname "CN=Pavan Solapure,O=Opencodez"

-ext san=email:user2@testmail.com

-keypass 123456 -storepass 123456

之后便是生成证书。同时证书需要被本机所认证。我们使用上述生成的.jks文件生成证书并进行安装,使得本机认证生成的证书。这样当发送加密文件的时候,邮件客户端能够正确的证书和密钥。

keytool -importkeystore -srckeystore "testmail.jks"

-destkeystore "testmail.p12" -srcstoretype JKS

-deststoretype PKCS12 -srcstorepass 123456

-deststorepass 123456 -srcalias test -destalias test

-srckeypass 123456 -destkeypass 123456 -noprompt

生成证书后,双击testmail.p12,你将被引导安装文件。之后生成的证书将被本机所认证。

引入jar包

3c0c352cbb74

读取证书(MailEncryptionUtil.class)

首先我们先获取证书。

private static X509Certificate getRecipientPublicCertificate(MimeMessage message) throws Exception{

ClassLoader classLoader = MailEncryptionUtil.class.getClassLoader();

File file = new File(classLoader.getResource("testmail.p12").getFile());

FileInputStream fis = new FileInputStream(file);

KeyStore ks = KeyStore.getInstance("PKCS12");

ks.load(fis, "123456".toCharArray());

X509Certificate recipientCert = (X509Certificate) ks.getCertificate("test");

return recipientCert;

}

对邮件信息进行加密

public static MimeMessage encryptMessage(MimeMessage message) throws Exception{

Security.addProvider(new BouncyCastleProvider());

SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();

X509Certificate recipientCert = getRecipientPublicCertificate(message);

gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(recipientCert).setProvider("BC"));

MimeBodyPart msg = new MimeBodyPart();

msg.setContent(message.getContent(), message.getContentType());

MimeBodyPart mp = gen.generate(msg, new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC).setProvider("BC").build());

message.setContent(mp.getContent(), mp.getContentType());

message.saveChanges();

return message;

}

首先我们将BouncyCastle作为安全提供者。之后是获取证书。之后是生成可以用于发送了邮件消息类。

邮件的发送(EmailService.class)

public void sendPlainTextMail(Email email){

String host = "localhost";

Properties properties = System.getProperties();

properties.setProperty("mail.smtp.host", host);

Session session = Session.getDefaultInstance(properties);

try {

MimeMessage message = new MimeMessage(session);

BodyPart messageBodyPart = new MimeBodyPart();

message.setFrom(new InternetAddress(email.getFrom()));

message.addRecipient(Message.RecipientType.TO, new InternetAddress(email.getToAsList()));

message.setSubject(email.getSubject());

messageBodyPart.setText(email.getMessage());

Multipart multipart = new MimeMultipart();

multipart.addBodyPart(messageBodyPart);

message.setText(email.getMessage());

//Transport.send(message);

Transport.send(MailEncryptionUtil.encryptMessage(message));

System.out.println("Sent message successfully.");

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

首先我们要在Session服务器中配置邮件服务器的基本信息。之后指定邮件的发送发、接收方、主题和内容等信息。之后是用之前写的MailEncryptionUtil对邮件信息进行加密并发送。

在主类中指定发送邮件的基本信息

public static void main(String[] args) {

String from = "rhf0410@testmail.com";

String to = "user2@testmail.com";

String subject = "Java mail which has been encrypted";

String message = "Hello encrypted mail.";

Email email = new Email(from, to, subject, message);

//Sent email

emailService.sendPlainTextMail(email);

}

相关资源:PGP加密软件Java实现
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页