4r1y4n

GCM mod of operation in Java BouncyCastle

Aug 31st, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1.     SecretKeySpec   key = new SecretKeySpec(keyBytes, "AES");
  2.     IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
  3.     Cipher          cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
  4.     byte[] block = new byte[1048576];
  5.     int i;
  6.     long st,et;
  7.  
  8.     cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
  9.  
  10.     BufferedInputStream bIn=new BufferedInputStream(new ProgressMonitorInputStream(null,"Encrypting ...",new FileInputStream("input")));
  11.     CipherInputStream       cIn = new CipherInputStream(bIn, cipher);
  12.     BufferedOutputStream bOut=new BufferedOutputStream(new FileOutputStream("output.enc"));
  13.  
  14.     int ch;
  15.     while ((i = cIn.read(block)) != -1) {
  16.         bOut.write(block, 0, i);
  17.     }
  18.     cIn.close();
  19.     bOut.close();
  20.  
  21.     Thread.sleep(5000);
  22.  
  23.     cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
  24.  
  25.     BufferedInputStream fis=new BufferedInputStream(new ProgressMonitorInputStream(null,"Decrypting ...",new FileInputStream("output.enc")));
  26.     //FileInputStream fis=new FileInputStream("output.enc");
  27.     //FileOutputStream ro=new FileOutputStream("regen.plain");
  28.     BufferedOutputStream ro=new BufferedOutputStream(new FileOutputStream("regen.plain"));
  29.  
  30.     CipherInputStream dcIn = new CipherInputStream(fis, cipher);
  31.  
  32.     while ((i = dcIn.read(block)) != -1) {
  33.         ro.write(block, 0, i);
  34.     }
  35.  
  36.     dcIn.close();
  37.     ro.close();
Advertisement
Add Comment
Please, Sign In to add comment