📄 readencryptedandsignedmail.java
字号:
package cn.edu.scut.smimeapi;
import java.security.cert.X509Certificate;
import javax.mail.internet.*;
import javax.mail.*;
import cn.edu.scut.certmgr.TokenPrivateKey;
import java.security.PrivateKey;
import org.bouncycastle.mail.smime.*;
/**
* a class that reads a basic SMIME Encrpted and signed mail.
*/
public class ReadEncryptedAndSignedMail extends ReceiveMail
{
/**
* Return a MimeBodyPart which has been descrpted autoally from the EncryptedAndSignedMsg by USB_Token .
*
* @param MimeMessage EncryptedMsg, the message which has been Encrypted.
* @paramchar[] userpin, the password which is used to open usb token.
* @throws Exception
*/
public MimeBodyPart ReadEncryptedAndSignedMailAutoByToken
(MimeMessage EncryptedMsg,char[] userpin)
throws Exception
{
MimeBodyPart res=null;
ReadEncryptedMail rem=new ReadEncryptedMail();
try
{
res=rem.ReadEncryptedMailAutoByToken(EncryptedMsg, userpin);
}
catch (Exception ex)
{
throw new Exception("ReadEncryptedMailAutoByToken error:"+ex);
}
if (res.isMimeType("multipart/signed")) //邮件解密后res为签名邮件
{
try
{
ReadSignedMail rsm = new ReadSignedMail();
res = rsm.ReadSignedMailByPassedCert(res);
}
catch (Exception ex2)
{
throw new Exception("read signed content error:"+ex2);
}
}
return res;
}
/**
* Return a MimeBodyPart which has been descrpted autoally from the EncryptedAndSignedMsg by USB_Token .
*
* @param MimeMessage EncryptedMsg, the message which has been Encrypted.
* @paramchar[] userpin, the password which is used to open usb token.
* @throws Exception
*/
public MimeBodyPart ReadEncryptedAndSignedMailByToken
(MimeMessage EncryptedMsg,TokenPrivateKey TokenKey,X509Certificate cert)
throws Exception
{
MimeBodyPart res=null;
ReadEncryptedMail rem=new ReadEncryptedMail();
try
{
res=rem.ReadEncryptedMailByToken(EncryptedMsg, TokenKey,cert);
}
catch (Exception ex)
{
throw new Exception("ReadEncryptedMailByToken error:"+ex);
}
if (res.isMimeType("multipart/signed")) //邮件解密后res为签名邮件
{
try
{
ReadSignedMail rsm = new ReadSignedMail();
res = rsm.ReadSignedMailByPassedCert(res);
}
catch (Exception ex2)
{
throw new Exception("read signed content error:"+ex2);
}
}
return res;
}
/**
* Return a MimeBodyPart which has been descrpted autoally from the EncryptedAndSignedMsg by LocalKey .
*
* @param MimeMessage EncryptedMsg, the message which has been Encrypted.
* @paramchar[] userpin, the password which is used to open Local keystore.
* @throws Exception
*/
public MimeBodyPart ReadEncryptedAndSignedMailAutoByLocalKey
(MimeMessage EncryptedMsg,char[] userpin)
throws Exception
{
MimeBodyPart res=null;
ReadEncryptedMail rem=new ReadEncryptedMail();
try
{
res=rem.ReadEncryptedMailAutoByLocalKey(EncryptedMsg, userpin);
}
catch (Exception ex)
{
throw new Exception("ReadEncryptedMailAutoByLocalKey error:"+ex);
}
if (res.isMimeType("multipart/signed")) //邮件解密后res为签名邮件
{
try
{
ReadSignedMail rsm = new ReadSignedMail();
res = rsm.ReadSignedMailByPassedCert(res);
}
catch (Exception ex2)
{
throw new Exception("read signed content error:"+ex2);
}
}
return res;
}
/**
* Return a MimeBodyPart which has been descrpted from the EncryptedAndSignedMsg by LocalKey .
*
* @param MimeMessage EncryptedMsg, the message which has been Encrypted.
* @param PrivateKey privatekey, the key which is to be used to descrpted the msg.
* @param cert, the X509Certificate to generate a suitable recipient identifier
* @throws Exception
*/
public MimeBodyPart ReadEncryptedAndSignedMailByLocalKey
(MimeMessage EncryptedMsg,PrivateKey privatekey,X509Certificate cert)
throws Exception
{
MimeBodyPart res=null;
ReadEncryptedMail rem=new ReadEncryptedMail();
try
{
res=rem.ReadEncryptedMailByLocalkey(EncryptedMsg, privatekey,cert);
}
catch (Exception ex)
{
throw new Exception("ReadEncryptedMailByLocalKey error:"+ex);
}
if (res.isMimeType("multipart/signed")) //邮件解密后res为签名邮件
{
try
{
ReadSignedMail rsm = new ReadSignedMail();
res = rsm.ReadSignedMailByPassedCert(res);
}
catch (Exception ex2)
{
throw new Exception("read signed content error:"+ex2);
}
}
return res;
}
public static void main(String[] args)
throws Exception
{
try
{
ReceiveMail recmail=new ReceiveMail();
Message[] msg = recmail.getMail("202.38.212.1", "yhliu", "yhliu");
System.out.println("您好,共收到"+msg.length+"封邮件");
for (int i = 0; i < msg.length; i++)
{
if (msg[i].isMimeType("text/*"))
{ //判断邮件类型为普通文本邮件
recmail.handleText(msg[i]);
}
else
{ //判断邮件类型为非文本邮件
//
// make sure this was a multipart/signed message - there should be
// two parts as we have one part for the content that was signed and
// one part for the actual signature.
//
if (msg[i].isMimeType("multipart/signed")) //签名邮件
{
recmail.handle(msg[i]);
SMIMESigned s = new SMIMESigned((MimeMultipart) msg[i].getContent());
//
// extract the content
//
MimeBodyPart content = s.getContent();
Object cont = content.getContent();
if (cont instanceof String)
{
System.out.println("邮件内容:");
System.out.println( (String) cont);
}
else if (cont instanceof Multipart)
{
Multipart mp = (Multipart) cont;
System.out.println( recmail.handleMultipart(mp));
}
System.out.println( verify(s));
}
else if (msg[i].isMimeType("application/pkcs7-mime")) //加密邮件
{
recmail.handle(msg[i]);
ReadEncryptedMail rem=new ReadEncryptedMail();
MimeBodyPart res=null;
res=rem.ReadEncryptedMailAutoByLocalKey((MimeMessage) msg[i],"tanglab".toCharArray());
if (res.isMimeType("multipart/signed")) //邮件解密后res为签名邮件
{
SMIMESigned s = new SMIMESigned((MimeMultipart) res.getContent());
//
// extract the content
//
MimeBodyPart content = s.getContent();
System.out.println("邮件解密后为签名邮件");
Object cont = content.getContent();
if (cont instanceof String)
{
System.out.println("邮件内容:");
System.out.println( (String) cont);
}
else if (cont instanceof Multipart)
{
Multipart mp = (Multipart) cont;
System.out.println( recmail.handleMultipart(mp));
}
System.out.println( verify(s));
}
else //邮件解密后的res为非签名邮件
{
if (res.isMimeType("text/*")) //邮件解密后的res为文本邮件
{
System.out.println("邮件内容:");
System.out.println(res.getContent());
}
else
{
System.out.println(recmail.handleMultipart((Multipart)res.getContent()));
}
}
}
else //Not a signed or an encrypted message!
{
System.out.println( recmail.handleMultipart(msg[i]));
}
}
System.out.println("---------------------------");
}
recmail.close();
}
catch(Exception e2)
{
System.out.println(e2);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -