Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: bgColor

...

This noncompliant code example uses the Electronic Codebook (ECB) mode of operation, which is generally insecure.

Code Block
bgColor#ccccff#FFCCCC
Cipher cipher = Cipher.getInstance("AES");             
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256 bits may be unavailable

SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");  // defaults to ECB mode
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

// Encode bytes as UTF8; strToBeEncrypted contains
// the input string that is to be encrypted 
byte[] encoded = strToBeEncrypted.getBytes("UTF8");
    
// Perform encryption
byte[] encrypted = cipher.doFinal(encoded);   

...