Skip to content

Instantly share code, notes, and snippets.

@twuni
Last active February 18, 2022 19:09
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save twuni/5668121 to your computer and use it in GitHub Desktop.
Save twuni/5668121 to your computer and use it in GitHub Desktop.
A basic example of how to perform symmetric key encryption/decryption using AES and Java's cryptography API.
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.codec.binary.Base64;
public class CryptoHelper {
public static void main( String [] args ) throws Exception {
CryptoHelper crypto = new CryptoHelper();
String plaintext = "This is a good secret.";
System.out.println( plaintext );
String ciphertext = crypto.encrypt( plaintext );
System.out.println( ciphertext );
String decrypted = crypto.decrypt( ciphertext );
System.out.println( decrypted );
}
public String encrypt( String plaintext ) throws Exception {
return encrypt( generateIV(), plaintext );
}
public String encrypt( byte [] iv, String plaintext ) throws Exception {
byte [] decrypted = plaintext.getBytes();
byte [] encrypted = encrypt( iv, decrypted );
StringBuilder ciphertext = new StringBuilder();
ciphertext.append( Base64.encodeBase64String( iv ) );
ciphertext.append( ":" );
ciphertext.append( Base64.encodeBase64String( encrypted ) );
return ciphertext.toString();
}
public String decrypt( String ciphertext ) throws Exception {
String [] parts = ciphertext.split( ":" );
byte [] iv = Base64.decodeBase64( parts[0] );
byte [] encrypted = Base64.decodeBase64( parts[1] );
byte [] decrypted = decrypt( iv, encrypted );
return new String( decrypted );
}
private Key key;
public CryptoHelper( Key key ) {
this.key = key;
}
public CryptoHelper() throws Exception {
this( generateSymmetricKey() );
}
public Key getKey() {
return key;
}
public void setKey( Key key ) {
this.key = key;
}
public static byte [] generateIV() {
SecureRandom random = new SecureRandom();
byte [] iv = new byte [16];
random.nextBytes( iv );
return iv;
}
public static Key generateSymmetricKey() throws Exception {
KeyGenerator generator = KeyGenerator.getInstance( "AES" );
SecretKey key = generator.generateKey();
return key;
}
public byte [] encrypt( byte [] iv, byte [] plaintext ) throws Exception {
Cipher cipher = Cipher.getInstance( key.getAlgorithm() + "/CBC/PKCS5Padding" );
cipher.init( Cipher.ENCRYPT_MODE, key, new IvParameterSpec( iv ) );
return cipher.doFinal( plaintext );
}
public byte [] decrypt( byte [] iv, byte [] ciphertext ) throws Exception {
Cipher cipher = Cipher.getInstance( key.getAlgorithm() + "/CBC/PKCS5Padding" );
cipher.init( Cipher.DECRYPT_MODE, key, new IvParameterSpec( iv ) );
return cipher.doFinal( ciphertext );
}
}
@stevemu
Copy link

stevemu commented Dec 25, 2017

Thank you! Thanks to you, I could do this.

@ddold
Copy link

ddold commented Mar 8, 2018

Brilliant tutorial, just what I have been looking for

@pelmeshka35
Copy link

no brilliant tutorial !!

@AmitBaanerjee
Copy link

is this program an example of AES NI implementation? as there are no initialization vectors in NI ..according to my understanding!
Kindly reply soon

@gwokudasam
Copy link

how do you generate the encryption key

@maverickabhi1991
Copy link

For symmetric keys, is signing same as encryption?

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