AES Key generator : Advanced Encryption Standard « Security « Java Tutorial






import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

public class MainClass {

  public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");
    generator.init(128);

    Key keyToBeWrapped = generator.generateKey();

    System.out.println("input    : " + new String(keyToBeWrapped.getEncoded()));
    Cipher cipher = Cipher.getInstance("AESWrap", "BC");

    KeyGenerator KeyGen = KeyGenerator.getInstance("AES", "BC");
    KeyGen.init(256);

    Key wrapKey = KeyGen.generateKey();
    cipher.init(Cipher.WRAP_MODE, wrapKey);
    byte[] wrappedKey = cipher.wrap(keyToBeWrapped);

    System.out.println("wrapped : " + new String(wrappedKey));

    cipher.init(Cipher.UNWRAP_MODE, wrapKey);
    Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
    System.out.println("unwrapped: " + new String(key.getEncoded()));
  }
}








36.2.Advanced Encryption Standard
36.2.1.using the KeyGenerator class and showing how to create a SecretKeySpec from an encoded key.
36.2.2.AES Key generator
36.2.3.Tampered message, plain encryption, AES in CTR mode
36.2.4.Tampered message, encryption with digest, AES in CTR mode
36.2.5.Tampered message with HMac, encryption with AES in CTR mode
36.2.6.AES wraps RSA