daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.security.InvalidKeyException;
  8. import java.security.MessageDigest;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.security.Provider;
  11.  
  12. import javax.crypto.BadPaddingException;
  13. import javax.crypto.Cipher;
  14. import javax.crypto.IllegalBlockSizeException;
  15.  
  16. import javax.crypto.NoSuchPaddingException;
  17. import javax.crypto.spec.IvParameterSpec;
  18. import javax.crypto.spec.SecretKeySpec;
  19.  
  20. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  21.  
  22. public class myClassName123 {
  23.  
  24. /**
  25.  * @param args
  26.  * @throws Exception
  27.  */
  28. public static void main(String[] args) throws Exception {
  29.     // TODO Auto-generated method stub
  30.  
  31.     try {
  32.         BufferedReader in = new BufferedReader(new FileReader("/Users/username/Documents/f12/words.txt"));
  33.         String str;
  34.         while ((str = in.readLine()) != null && str.getBytes().length<16) { //clue given in spec
  35.  
  36.                 System.out.println(decrypt(getBytesFromFile(new File("/Users/username/Documents/f21/some.aes-128-cbc")),str));
  37.  
  38.  
  39.         }
  40.         in.close();
  41.     } catch (IOException e) {
  42.     }
  43. }
  44.  
  45. /**
  46. * This method decrypts the input byte [] using AES Key byte []
  47. *
  48. * @param byte []
  49. * @param byte []
  50. * @return byte []
  51. * @throws Exception
  52. */
  53. public static byte[] decrypt(byte[] text, String key) throws Exception {
  54.     Cipher cipher;
  55.     byte[] bytes = null;
  56.  
  57.  
  58.     Provider provider = new BouncyCastleProvider();
  59.     MessageDigest digester = MessageDigest.getInstance("SHA-256", provider);
  60.     digester.update(key.getBytes("UTF-8"));
  61.     //byte[] key = digester.digest();
  62.     SecretKeySpec spec = new SecretKeySpec(digester.digest(), "AES");
  63.  
  64.     //SecretKeySpec spec = new SecretKeySpec(toByteArray(key.toCharArray()), "AES");
  65.      byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  66.         IvParameterSpec ivspec = new IvParameterSpec(iv);
  67.     try {
  68.             // Instantiate the cipher
  69.             cipher = Cipher.getInstance("AES/CBC/NoPadding");
  70.             cipher.init(Cipher.DECRYPT_MODE, spec, ivspec);
  71.  
  72.              bytes = cipher.doFinal(text);
  73.  
  74.              String value = new String(bytes, "UTF-8");
  75.  
  76.                 System.out.println("DEBUG HERE: "+value);
  77.  
  78.     }catch (NoSuchAlgorithmException e) {
  79.             e.printStackTrace();
  80.         //  throw new Exception(e);
  81.     } catch (NoSuchPaddingException e) {
  82.         e.printStackTrace();
  83.     //  throw new Exception(e);
  84.     } catch (InvalidKeyException e) {
  85.         e.printStackTrace();
  86.         //throw new Exception(e);
  87.     } catch (IllegalBlockSizeException e) {
  88.         e.printStackTrace();
  89.         //throw new Exception(e);
  90.     } catch (BadPaddingException e) {
  91.         e.printStackTrace();
  92.         //throw new Exception(e);
  93.     }
  94.     return bytes;
  95. }
  96.  
  97. // Returns the contents of the file in a byte array.
  98. public static byte[] getBytesFromFile(File file) throws IOException {
  99.     InputStream is = new FileInputStream(file);
  100.  
  101.     // Get the size of the file
  102.     long length = file.length();
  103.  
  104.     // Create the byte array to hold the data
  105.     byte[] bytes = new byte[(int)length];
  106.  
  107.     // Read in the bytes
  108.     int offset = 0;
  109.     int numRead = 0;
  110.     while (offset < bytes.length
  111.            && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
  112.         offset += numRead;
  113.     }
  114.  
  115.     // Ensure all the bytes have been read in
  116.     if (offset < bytes.length) {
  117.         throw new IOException("Could not completely read file "+file.getName());
  118.     }
  119.  
  120.     // Close the input stream and return bytes
  121.     is.close();
  122.     return bytes;
  123. }  
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top