Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.security.NoSuchAlgorithmException;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.NoSuchPaddingException;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- /**
- * Uses AES to encrypt and decrypt a message.
- *
- * @author CENSORED FOR JU DYLAN
- */
- public class AES {
- /**
- * The message.
- */
- private static final String MESSAGE = "Hello world!";
- /**
- * The key generator instance.
- */
- private static KeyGenerator keyGenerator;
- /**
- * The secret key spec instance.
- */
- private static SecretKeySpec secretkeySpec;
- /**
- * The cipher instance.
- */
- private static Cipher cipher;
- /**
- * The secret key instance.
- */
- private static SecretKey secretKey;
- /**
- * The key size.
- */
- private static final int KEY_SIZE = 128;
- /**
- * Decrypts the message.
- *
- * @param encrypted
- * The encrypted message.
- * @return The decrypted message.
- */
- private static String decrypt(byte[] encrypted) {
- try {
- cipher.init(Cipher.DECRYPT_MODE, secretkeySpec);
- return new String(cipher.doFinal(encrypted));
- } catch (Exception ex) {
- System.err.println(ex);
- }
- return null;
- }
- /**
- * Encrypts the message.
- *
- * @return The encrypted message.
- */
- private static byte[] encrypt(String decrypted) {
- try {
- cipher.init(Cipher.ENCRYPT_MODE, secretkeySpec);
- return cipher.doFinal(decrypted.getBytes());
- } catch (Exception ex) {
- System.err.println(ex);
- }
- return null;
- }
- /**
- * The constructor.
- */
- private AES() {
- try {
- cipher = Cipher.getInstance("AES");
- keyGenerator = KeyGenerator.getInstance("AES");
- keyGenerator.init(KEY_SIZE);
- secretKey = keyGenerator.generateKey();
- byte[] raw = secretKey.getEncoded();
- secretkeySpec = new SecretKeySpec(raw, "AES");
- } catch (NoSuchAlgorithmException ex) {
- System.err.println(ex);
- } catch (NoSuchPaddingException ex) {
- System.err.println(ex);
- }
- }
- /**
- * Runs the program.
- *
- * @param args
- * The running arguments
- */
- public static void main(String[] args) {
- try {
- new AES();
- byte[] encrypted = encrypt(MESSAGE);
- System.out.println("Encrypted: " + encrypted);
- System.out.println("Decrypted: " + decrypt(encrypted));
- } catch (Exception ex) {
- System.err.println(ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment