SHARE
TWEET

Untitled

a guest Aug 12th, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.hyperion.util;
  2.  
  3. import java.security.Key;
  4.  
  5. import javax.crypto.Cipher;
  6. import javax.crypto.KeyGenerator;
  7.  
  8. /**
  9.  * Encrypts and decrypts using the DESede algorithm.
  10.  *
  11.  * @author Ryan Greene
  12.  *
  13.  */
  14. public class Cryption {
  15.  
  16.     /**
  17.      * The sentence being crypted and decrypted.
  18.      */
  19.     private static final String sentence = "Ryan is such a fucking pro if this works.";
  20.  
  21.     /**
  22.      * The algorithm being used to crypt and decrypt.
  23.      */
  24.     private static final String algorithm = "DESede";
  25.  
  26.     /**
  27.      * The key instance.
  28.      */
  29.     private static Key key = null;
  30.  
  31.     /**
  32.      * The cipher instance.
  33.      */
  34.     private static Cipher cipher = null;
  35.  
  36.     /**
  37.      * Decrypts a string using the DESede algorithm.
  38.      *
  39.      * @param bytes
  40.      *            The encrypted string.
  41.      * @return The decrypted string.
  42.      */
  43.     private static String decrypt(byte[] bytes) {
  44.         try {
  45.             cipher.init(Cipher.DECRYPT_MODE, key);
  46.             return new String(cipher.doFinal(bytes));
  47.         } catch (Exception e) {
  48.             System.err.println(e);
  49.             return null;
  50.         }
  51.     }
  52.  
  53.     /**
  54.      * Encrypts a string using the DESede algorithm.
  55.      *
  56.      * @param string
  57.      *            The string being encrypted.
  58.      * @return The encrypted string.
  59.      */
  60.     private static byte[] encrypt(String string) {
  61.         try {
  62.             cipher.init(Cipher.ENCRYPT_MODE, key);
  63.             return cipher.doFinal(string.getBytes());
  64.         } catch (Exception e) {
  65.             System.err.println(e);
  66.             return null;
  67.         }
  68.     }
  69.  
  70.     /**
  71.      * Runs the program.
  72.      *
  73.      * @param args
  74.      *            The running arguments.
  75.      */
  76.     public static void main(String[] args) {
  77.         try {
  78.             key = KeyGenerator.getInstance(algorithm).generateKey();
  79.             cipher = Cipher.getInstance(algorithm);
  80.             System.out.println(encrypt(sentence));
  81.             System.out.println(decrypt(encrypt(sentence)));
  82.         } catch (Exception e) {
  83.             System.err.println(e);
  84.         }
  85.     }
  86. }
RAW Paste Data
Pastebin PRO Autumn Special!
Get 40% OFF on Pastebin PRO accounts!
Top