public static String encryptByAES ( final String content , final String key ) { try { final KeyGenerator kgen = KeyGenerator . getInstance ( "AES" ) ; final SecureRandom secureRandom = SecureRandom . getInstance ( "SHA1PRNG" ) ; secureRandom . setSeed ( key . getBytes ( ) ) ; kgen . init ( 128 , secureRandom ) ; final SecretKey secretKey = kgen . generateKey ( ) ; final byte [ ] enCodeFormat = secretKey . getEncoded ( ) ; final SecretKeySpec keySpec = new SecretKeySpec ( enCodeFormat , "AES" ) ; final Cipher cipher = Cipher . getInstance ( "AES" ) ; final byte [ ] byteContent = content . getBytes ( "UTF-8" ) ; cipher . init ( Cipher . ENCRYPT_MODE , keySpec ) ; final byte [ ] result = cipher . doFinal ( byteContent ) ; return Hex . encodeHexString ( result ) ; } catch ( final Exception e ) { LOGGER . log ( Level . WARN , "Encrypt failed" , e ) ; return null ; } }
Encrypts by AES .