Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ************************************************************************
  #
  #  DivConq
  #
  #  http://divconq.com/
  #
  #  Copyright:
  #    Copyright 2014 eTimeline, LLC. All rights reserved.
  #
 #  License:
 #    See the license.txt file in the project's top-level directory for details.
 #
 #  Authors:
 #    * Andy White
 #
 ************************************************************************ */
 package divconq.pgp;
 
 
 
 
Basic utility class, borrowed from Bouncy Castle since it was not public access.
 
 public class PGPUtil
 {
     public static String getDigestName(
         int        hashAlgorithm)
         throws PGPException
     {
         switch (hashAlgorithm)
         {
         case .:
             return "SHA1";
         case .:
             return "MD2";
         case .:
             return "MD5";
         case .:
             return "RIPEMD160";
         case .:
             return "SHA256";
         case .:
             return "SHA384";
         case .:
             return "SHA512";
         case .:
             return "SHA224";
         case .:
             return "TIGER";
         default:
             throw new PGPException("unknown hash algorithm tag in getDigestName: " + hashAlgorithm);
         }
     }
     
     public static String getSignatureName(
         int        keyAlgorithm,
         int        hashAlgorithm)
         throws PGPException
     {
         String     encAlg;
                 
         switch (keyAlgorithm)
         {
         case .:
         case .:
             encAlg = "RSA";
             break;
         case .:
             encAlg = "DSA";
             break;
         case .// in some malformed cases.
         case .:
             encAlg = "ElGamal";
             break;
         default:
             throw new PGPException("unknown algorithm tag in signature:" + keyAlgorithm);
         }
 
         return getDigestName(hashAlgorithm) + "with" + encAlg;
     }
    
    public static String getSymmetricCipherName(
        int    algorithm)
    {
        switch (algorithm)
        {
        case .:
            return null;
            return "DESEDE";
        case .:
            return "IDEA";
        case .:
            return "CAST5";
        case .:
            return "Blowfish";
        case .:
            return "SAFER";
        case .:
            return "DES";
        case .:
            return "AES";
        case .:
            return "AES";
        case .:
            return "AES";
            return "Camellia";
            return "Camellia";
            return "Camellia";
        case .:
            return "Twofish";
        default:
            throw new IllegalArgumentException("unknown symmetric algorithm: " + algorithm);
        }
    }
    
    static public SecretKey makeSymmetricKey(
        int             algorithm,
        byte[]          keyBytes)
        throws PGPException
    {
        String    algName = getSymmetricCipherName(algorithm);
        if (algName == null)
        {
            throw new PGPException("unknown symmetric algorithm: " + algorithm);
        }
        return new SecretKeySpec(keyBytesalgName);
    }
    
    static public void encryptFile(String outputFileNameString inputFileNameString encKeyFileName)
    {
    	Path fileIn = Paths.get(inputFileName);
    	Path fileOut = Paths.get(outputFileName);
    	Path fileKey = Paths.get(encKeyFileName);
    	
        OutputStream out = new BufferedOutputStream(new FileOutputStream(fileOut.toFile()));
        try {    
        	EncryptedFileStream pw = new EncryptedFileStream(); 
        	
        	//pw.setAlgorithm(SymmetricKeyAlgorithmTags.NULL);
        	pw.setFileName(fileIn.getFileName().toString());
        	pw.loadPublicKey(fileKey);
        	
        	pw.init();
        	
            FileInputStream in = new FileInputStream(fileIn.toFile());
            byte[] ibuf = new byte[31 * 1024];
            int len;
            
            while ((len = in.read(ibuf)) > 0) {
                pw.writeData(ibuf, 0, len);
                
                ByteBuf buf = pw.nextReadyBuffer();
                
                while (buf != null) {
                	out.write(buf.array(), buf.arrayOffset(), buf.readableBytes()); 
                
                	buf.release();
                	
                	buf = pw.nextReadyBuffer();
                }
            }
            
            in.close();
            pw.close();
            
            ByteBuf buf = pw.nextReadyBuffer();
            
            while (buf != null) {
            	out.write(buf.array(), buf.arrayOffset(), buf.readableBytes()); 
            
            	buf.release();
            	
            	buf = pw.nextReadyBuffer();
            }
            
            out.close();
        }
        catch (Exception e) {
            ..println(e);
            
            e.printStackTrace();
        }
    }
    
    /*
     * 
     * TODO develop a packet reader/lister
				case 110: {
					byte[] in = Files.readAllBytes(Paths.get("/Work/Temp/Dest/story2.xml.gpg"));
					int idx = 0;
					while (idx < in.length) {
						int hdr = in[idx] & 0xFF;
				        
				        if ((hdr & 0x80) == 0) {
				            System.out.println("invalid header encountered");
				            break;
				        }
				        boolean newPacket = ((hdr & 0x40) != 0);
				        int tag = 0;
				        int bodyLen = 0;
				        int hdrLen = 1;
				        boolean partial = false;
				        if (newPacket) {
				            tag = hdr & 0x3f;
							System.out.println("New Packet: " + HexUtil.charToHex(hdr) + " tag: " + tag);
				        	
				            int l = in[idx + 1] & 0xFF;
				            if (l < 192)
				            {
				                bodyLen = l;
				                hdrLen = 2;
				            }
				            else if (l <= 223)
				            {
				                int b = in[idx + 2] & 0xFF;
				                bodyLen = ((l - 192) << 8) + (b) + 192;
				                hdrLen = 3;
				            }
				            else if (l == 255)
				            {
				                //bodyLen = (in[idx + 2] << 24) | (in[idx + 3] << 16) | (in[idx + 4] << 8)  | in[idx + 5];
				                bodyLen = ((in[idx + 2] & 0xFF) << 24) | ((in[idx + 3] & 0xFF) << 16) |  ((in[idx + 4] & 0xFF) << 8)  | (in[idx + 5] & 0xFF);
				                hdrLen = 6;
				            }
				            else
				            {
				                partial = true;
				                bodyLen = 1 << (l & 0x1f);
				                hdrLen = 2;
				            }
				        }
				        else {
				            tag = (hdr & 0x3f) >> 2;
							System.out.println("Old Packet: " + HexUtil.charToHex(hdr) + " tag: " + tag);
				        	
				            int lengthType = hdr & 0x3;
				            switch (lengthType)
				            {
				            case 0:
				                hdrLen = 2;
				                bodyLen = in[idx + 1] & 0xFF;
				                break;
				            case 1:
				                hdrLen = 3;
				                //bodyLen = (in[idx + 1] << 8) | in[idx + 2];
				                bodyLen = ((in[idx + 1] & 0xFF) << 8) | (in[idx + 2] & 0xFF);
				                break;
				            case 2:
				                hdrLen = 5;
				                //bodyLen = (in[idx + 1] << 24) | (in[idx + 2] << 16) | (in[idx + 3] << 8) | in[idx + 4];
				                bodyLen = ((in[idx + 1] & 0xFF) << 24) | ((in[idx + 2] & 0xFF) << 16) | ((in[idx + 3] & 0xFF) << 8) | (in[idx + 4] & 0xFF);
				                break;
				            case 3:
				                partial = true;
				                break;
				            default:
				                throw new IOException("unknown length type encountered");
				            }
				        }	
				        
				        System.out.println("- Length: " + bodyLen + " partial: " + partial + " hdr len: " + hdrLen);
				        
				        idx += bodyLen + hdrLen;
					}
					System.out.println("end of file");
					break;
				}
     * 
     */
New to GrepCode? Check out our FAQ X