Chiddix

AES Cryption

May 27th, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. import java.security.NoSuchAlgorithmException;
  2.  
  3. import javax.crypto.Cipher;
  4. import javax.crypto.KeyGenerator;
  5. import javax.crypto.NoSuchPaddingException;
  6. import javax.crypto.SecretKey;
  7. import javax.crypto.spec.SecretKeySpec;
  8.  
  9. /**
  10.  * Uses AES to encrypt and decrypt a message.
  11.  *
  12.  * @author CENSORED FOR JU DYLAN
  13.  */
  14. public class AES {
  15.  
  16.     /**
  17.      * The message.
  18.      */
  19.     private static final String MESSAGE = "Hello world!";
  20.  
  21.     /**
  22.      * The key generator instance.
  23.      */
  24.     private static KeyGenerator keyGenerator;
  25.  
  26.     /**
  27.      * The secret key spec instance.
  28.      */
  29.     private static SecretKeySpec secretkeySpec;
  30.  
  31.     /**
  32.      * The cipher instance.
  33.      */
  34.     private static Cipher cipher;
  35.  
  36.     /**
  37.      * The secret key instance.
  38.      */
  39.     private static SecretKey secretKey;
  40.  
  41.     /**
  42.      * The key size.
  43.      */
  44.     private static final int KEY_SIZE = 128;
  45.  
  46.     /**
  47.      * Decrypts the message.
  48.      *
  49.      * @param encrypted
  50.      *            The encrypted message.
  51.      * @return The decrypted message.
  52.      */
  53.     private static String decrypt(byte[] encrypted) {
  54.         try {
  55.             cipher.init(Cipher.DECRYPT_MODE, secretkeySpec);
  56.             return new String(cipher.doFinal(encrypted));
  57.         } catch (Exception ex) {
  58.             System.err.println(ex);
  59.         }
  60.         return null;
  61.     }
  62.  
  63.     /**
  64.      * Encrypts the message.
  65.      *
  66.      * @return The encrypted message.
  67.      */
  68.     private static byte[] encrypt(String decrypted) {
  69.         try {
  70.             cipher.init(Cipher.ENCRYPT_MODE, secretkeySpec);
  71.             return cipher.doFinal(decrypted.getBytes());
  72.         } catch (Exception ex) {
  73.             System.err.println(ex);
  74.         }
  75.         return null;
  76.     }
  77.  
  78.     /**
  79.      * The constructor.
  80.      */
  81.     private AES() {
  82.         try {
  83.             cipher = Cipher.getInstance("AES");
  84.             keyGenerator = KeyGenerator.getInstance("AES");
  85.             keyGenerator.init(KEY_SIZE);
  86.             secretKey = keyGenerator.generateKey();
  87.             byte[] raw = secretKey.getEncoded();
  88.             secretkeySpec = new SecretKeySpec(raw, "AES");
  89.         } catch (NoSuchAlgorithmException ex) {
  90.             System.err.println(ex);
  91.         } catch (NoSuchPaddingException ex) {
  92.             System.err.println(ex);
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Runs the program.
  98.      *
  99.      * @param args
  100.      *            The running arguments
  101.      */
  102.     public static void main(String[] args) {
  103.         try {
  104.             new AES();
  105.             byte[] encrypted = encrypt(MESSAGE);
  106.             System.out.println("Encrypted: " + encrypted);
  107.             System.out.println("Decrypted: " + decrypt(encrypted));
  108.         } catch (Exception ex) {
  109.             System.err.println(ex);
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment