简体   繁体   中英

encrypting email with attachment in java

My project is to create simple mail client that handle basic operation like sending mail and read from inbox. However I want to make it more secure with some encryption. I know some about encryption using built-in javax.crypto. Also, i've successfully develop non-encryption mail client with java-mail library.
Oh yes, I also have check the bouncy castle and java-mail crypto.

Here is my code

public void send(...) {
// some config on mail props
// .....

Message message = ...;
message.setContent(multipart, "multipart");
MTPTransport smtpTransport = (SMTPTransport) session.getTransport("smtps");
smtpTransport.connect(host, username, password);

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, true));
smtpTransport.sendMessage(message, message.getAllRecipients());
....
}

At this point, im started to thinking wheter i have to encrypt "multipart" object, like :

message.setContent(AESEncrypt.encryptObject(multipart), "multipart");

and so on the reading mail method, there will be decrypt object. Or what?

And here is part of my encryption method

public SealedObject encryptObject(Multipart multipart) {
SealedObject sealedObject = null;
Key secretKey = generateKey(startingKey);
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, secretKey);
sealedObject = new SealedObject(multipart, c);
return sealedObject;
}

I've ever read that we need serialized object first to be able to encrypt. Well, multipart isnt serialized object.
Am i got it wrong way? Do you have any better suggestion/reccomendation on which way is better? thanks in advance.

You don't need a java.io.Serializable object, you just need the object to be "serialized", which in this case means turning it into a MIME format byte stream.

You can find Bouncy Castle examples as described here , and can download some other examples from this page . Your best bet is to start with one of these example programs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM