/**
*
* AES CBC 256 位加密
* @param content 加密内容字节数组
* @param keyBytes 加密字节数组
* @param iv 加密向量字节数组
* @param encryptLength 仅支持 128、256 长度
* @return 解密后字节内容
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws InvalidAlgorithmParameterException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] encryptAESCBC( byte[] content, byte[] keyBytes,byte[] iv, int encryptLength )
{
KeyGenerator keyGenerator = KeyGenerator.getInstance( "AES" );
SecureRandom secureRandom = SecureRandom.getInstance( "SHA1PRNG" );
secureRandom.setSeed( keyBytes );
keyGenerator.init( encryptLength, secureRandom );
SecretKey key = keyGenerator.generateKey();
// 以上应该是使用任意长度的 keyBytes,生成指定长度为 encryptLength 的 key
// 后面再使用 AES/CBC/PKCS5Padding 算法,对 content 加密,加密角度是上面生成的固定长度的 key
// 我的主要疑问就在这里, 如何用 golang 生成与 keyGenerator.generateKey() 一样的 key 呢?
Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
cipher.init( Cipher.ENCRYPT_MODE, key, new IvParameterSpec( iv ) );
byte[] result = cipher.doFinal( content );
return(result);
}