skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, October 6, 2012

Encrypting a file through OpenPGP standard

Posted by Raju Gupta at 12:58 AM – 0 comments
 
Donwloaded Cryptix OpenPGP which is a Java implementation of the OpenPGP standard and tried a sample to encrypt and decrypt a text file.

//First read the key.
            FileInputStream in = new FileInputStream(keyPath);
            MessageFactory mf = MessageFactory.getInstance("OpenPGP");
            Collection msgs = mf.generateMessages(in);
            KeyBundleMessage kbm = (KeyBundleMessage)msgs.iterator().next();
            bundle = kbm.getKeyBundle();
            in.close();

//  Build the literal message
                LiteralMessage litmsg = null;
                BufferedReader in = new BufferedReader(new FileReader(filePath+toEncryptFile));
                String str;
                while ((str = in.readLine()) != null) {
              msg = msg + str+"\n";
                }
                in.close();

// Encrypt the message.
            Message msg = null;
            EncryptedMessageBuilder emb =
            EncryptedMessageBuilder.getInstance("OpenPGP");
            emb.init(litmsg);
            emb.addRecipient(bundle);
            msg = emb.build();

// Armour the message and write it to disk
            PGPArmouredMessage armoured;
            armoured = new PGPArmouredMessage(msg);
            FileOutputStream out = new FileOutputStream(filePath+encryptFileName);
            out.write(armoured.getEncoded());
            out.close();

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger