Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: how to decrypt emails and attachments in java

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to decrypt emails and attachments in java

    Hi People,

    Currently my java program is reading emails along with attachments from the mailing server. It works fine as long as the email and the attachments are "not" encrypted.

    The problem comes when the email and/or the attachment are encrypted.

    I need to extend my application so that it should be able to handle encrypted emails and attachments as well.

    I did some research and found out that for decrypting the contents i need the private key of the recipient email id and the digital signature of the sender?

    If my above understanding is correct, could someone please refer me to some reference java examples / snippets where i get some insight into the java api used to perform this?

    Many thanks in advance.

    fynn.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to decrypt emails and attachments in java

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    There are multiple ways to encrypt and therefore multiple corresponding ways to correctly decrypt. There are standards, which, if followed, narrow the number of possibilities. The approach you described is one standard way - at least as far as key management is concerned - but the application of the keys to encrypt and decrypt may vary.

    You need to do your own searching. Start by determining the email server or client from which the emails originate and which encryption scheme is used. Then continue searching to determine how to build a client that can accept those emails and decrypt them. For example, Thunderbird does not encrypt/decrypt out of the box but plugins can be added to give it the capability. There is likely some dependence on the sender and receiver using the same or compatible plugins and there must be some exchange of keys in the background or using a service. You'll have to figure all that out for your application.

    Good luck!

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JavaMail-Crypto Decryption Example

    Hi All,

    I am looking for some example snippets where i can see JavaMail-Crypto api doing the decryption of the encrypted email and its attacment.

    I have an encrypted email and the private key, i need to know how the api suports in decrypting the email content and attachment?

    Many thanks.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to decrypt emails and attachments in java

    Threads merged.

    Look for an email encrypt/decrypt tutorial or review the documentation that comes with the encryption/decryption tools you're using.

  5. #5

    Default Re: how to decrypt emails and attachments in java

    private static byte[] cmsDecrypt(byte[] message, PrivateKey key) throws
    Exception {
    CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(message);
    RecipientInformationStore recipients = ep.getRecipientInfos();
    Collection c = recipients.getRecipients();
    Iterator iter = c.iterator();
    RecipientInformation recipient = (RecipientInformation) iter.next();
    return recipient.getContent(key, new BouncyCastleProvider());
    }



    Path path = Paths.get("fileToDecrypt.p7m");
    byte[] data = Files.readAllBytes(path);
    try {
    System.out.println(new String(cmsDecrypt(data, key)));
    } catch (Exception e) {
    e.printStackTrace();
    }

Similar Threads

  1. Java Code to download attachments from lotus notes
    By singhnk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 11th, 2014, 06:15 AM
  2. Need some different approch on sending emails through java
    By mkhilate in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 3rd, 2013, 02:55 AM
  3. [SOLVED] Encrypt Decrypt
    By dianac in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 14th, 2013, 11:01 AM
  4. Java sound functions + emails
    By deanbyrne95 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 07:44 AM