Skip to content

Instantly share code, notes, and snippets.

@maxpowel
Created December 18, 2017 17:30
Show Gist options
  • Save maxpowel/dd78308c83228f3234f216db49224057 to your computer and use it in GitHub Desktop.
Save maxpowel/dd78308c83228f3234f216db49224057 to your computer and use it in GitHub Desktop.
Java AES Cipher
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.security.SecureRandom;
import java.security.MessageDigest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import javax.crypto.BadPaddingException;
import java.security.InvalidAlgorithmParameterException;
public class AESCipher {
private static final int IV_SIZE = 16;
private SecretKeySpec secretKeySpec;
public AESCipher(String key) throws UnsupportedEncodingException, NoSuchAlgorithmException {
// Hashing key.
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(key.getBytes("UTF-8"));
byte[] keyBytes = new byte[32];
System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
secretKeySpec = new SecretKeySpec(keyBytes, "AES");
}
public String encrypt(String plainText) throws IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException{
byte[] clean = plainText.getBytes();
// Generating IV.
byte[] iv = new byte[IV_SIZE];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
// Encrypt.
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(clean);
// Combine IV and encrypted part.
byte[] encryptedIVAndText = new byte[IV_SIZE + encrypted.length];
System.arraycopy(iv, 0, encryptedIVAndText, 0, IV_SIZE);
System.arraycopy(encrypted, 0, encryptedIVAndText, IV_SIZE, encrypted.length);
return new String(Base64.getEncoder().encode(encryptedIVAndText));
}
public String decrypt(String inputText) throws InvalidKeyException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, BadPaddingException, NoSuchPaddingException{
// Extract IV.
byte[] iv = new byte[IV_SIZE];
byte[] encryptedIvTextBytes = Base64.getDecoder().decode(inputText);
System.arraycopy(encryptedIvTextBytes, 0, iv, 0, iv.length);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
// Extract encrypted part.
int encryptedSize = encryptedIvTextBytes.length - IV_SIZE;
byte[] encryptedBytes = new byte[encryptedSize];
System.arraycopy(encryptedIvTextBytes, IV_SIZE, encryptedBytes, 0, encryptedSize);
// Decrypt.
Cipher cipherDecrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherDecrypt.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] decrypted = cipherDecrypt.doFinal(encryptedBytes);
return new String(decrypted);
}
}
@maxpowel
Copy link
Author

Example of use

public class Main {


    public static void main(String[] args) throws Exception {
        AESCipher c = new AESCipher("123456789qwerty$");
        System.out.println(c.encrypt("FOO"));
        System.out.println(c.decrypt(c.encrypt("FOO")));

    }
}

@maxpowel
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment