Portable PGP: Java Program to generate Signature File



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.SignatureException;
import java.util.Iterator;

import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureGenerator;
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
import org.bouncycastle.openpgp.PGPUtil;

public class test {
 private final static int BUFFER_SIZE = 1 << 16;

 public static void main(String[] args) {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

  InputStream _inputFile = null;
  try {
   /* Input text file */
   _inputFile = new FileInputStream(new File("D://TEST/textFile.txt"));

  } catch (FileNotFoundException e4) {

   e4.printStackTrace();
  }
  OutputStream _sigFile = null;

  try {
   /* Signature file */
   _sigFile = new FileOutputStream("D://TEST/textFile.sig");

  } catch (FileNotFoundException e3) {

   e3.printStackTrace();
  }

  String keyFilePath = "D://TEST/privateKey";
  FileInputStream secretKey = null;
  try {
   secretKey = new FileInputStream(keyFilePath);
  } catch (FileNotFoundException e2) {
   // TODO Auto-generated catch block
   e2.printStackTrace();
  }
  PGPSecretKey key = null;
  try {
   key = readKey(secretKey);
  } catch (Exception e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }

  try {
   signFile(_inputFile, _sigFile, key, "1234".toCharArray(), false);
   /* "1234" is the passphrase of key */

  } catch (NoSuchAlgorithmException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchProviderException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SignatureException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (PGPException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 public static void signFile(InputStream in, OutputStream out,
   PGPSecretKey key, char[] pass, boolean textmode)
   throws IOException, NoSuchAlgorithmException,
   NoSuchProviderException, PGPException, SignatureException {
  PGPPrivateKey priK = key.extractPrivateKey(pass, "BC");

  PGPSignatureGenerator sGen = new PGPSignatureGenerator(key
    .getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");

  if (textmode)
   sGen.initSign(PGPSignature.CANONICAL_TEXT_DOCUMENT, priK);
  else
   sGen.initSign(PGPSignature.BINARY_DOCUMENT, priK);

  Iterator it = key.getPublicKey().getUserIDs();
  if (it.hasNext()) {
   PGPSignatureSubpacketGenerator spGen = 
     new PGPSignatureSubpacketGenerator();
   spGen.setSignerUserID(false, (String) it.next());
   sGen.setHashedSubpackets(spGen.generate());
  }

  BCPGOutputStream bOut = new BCPGOutputStream(out);
  int rSize = 0;
  byte[] buf = new byte[BUFFER_SIZE];

  while ((rSize = in.read(buf)) >= 0) {
   sGen.update(buf, 0, rSize);
  }
  org.bouncycastle.openpgp.PGPSignature sig = sGen.generate();
  sig.encode(bOut);

 }

 private static PGPSecretKey readKey(InputStream in) throws Exception {

  in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);

  PGPSecretKeyRing pkRing = null;
  PGPSecretKeyRingCollection pkCol = new PGPSecretKeyRingCollection(in);
  Iterator it = pkCol.getKeyRings();
  while (it.hasNext()) {
   pkRing = (PGPSecretKeyRing) it.next();
   Iterator pkIt = pkRing.getSecretKeys();
   while (pkIt.hasNext()) {
    PGPSecretKey key = (PGPSecretKey) pkIt.next();

    if ((key).isSigningKey())
     return key;
   }
  }
  return null;
 }

}





SHARE

About Unknown

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment