近期项目论坛,需要把网站的登陆注册用户这方面要全部走上面给的用户中心数据库的接口,接口文档中用到了AES加密,原以为用PHP的mcrypt_encrypt或者openssl_encrypt写就好了,没想到没那么简单,原来接口文档中秘钥又进行了一次SHA1PRNG算法的加密,话不多了上代码
Java代码
public static String encrypt(String content, String password) {
try {
if (StringUtils.isEmpty(content))
return "";
KeyGeneratorkgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
kgen.init(128, random);
SecretKeysecretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
String str = Base64.getEncoder().encodeToString(result);
returnstr;
} catch (NoSuchAlgorithmException e) {
// e.printStackTrace();
} catch (NoSuchPaddingException e) {
// e.printStackTrace();
} catch (InvalidKeyException e) {
// e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// e.printStackTrace();
} catch (BadPaddingException e) {
// e.printStackTrace();
}
return null;
}
public static String decrypt(String str, String password) {
try {
byte[] content = Base64.getDecoder().decode(str);
KeyGeneratorkgen = KeyGenerator.getInstance("AES");
SecureRandomsecureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
kgen.init(128, secureRandom);
SecretKeysecretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return new String(result,"UTF-8");
} catch (NoSuchAlgorithmException e) {
// e.printStackTrace();
} catch (NoSuchPaddingException e) {
// e.printStackTrace();
} catch (InvalidKeyException e) {
// e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// e.printStackTrace();
} catch (BadPaddingException e) {
// e.printStackTrace();
} catch (Exception e) {
// e.printStackTrace();
}
return "";
}
然后对应封装了一个PHP类
PHP代码
class AES {
//PKCS5Padding 补码方式
private function _pkcs5Pad($text, $blockSize) {
$pad = $blockSize - (strlen($text) % $blockSize);
return $text . str_repeat(chr($pad), $pad);
}
private function _pkcs5Unpad($text) {
$end = substr($text, -1);
$last = ord($end);
$len = strlen($text) - $last;
if(substr($text, $len) == str_repeat($end, $last)) {
return substr($text, 0, $len);
}
return false;
}
//加密
public function encrypt($encrypt, $key) {
$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$paddedData = $this->_pkcs5Pad($encrypt, $blockSize);
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
$key2 = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true),0,16);
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key2, $paddedData, MCRYPT_MODE_ECB, $iv);
return base64_encode($encrypted);
}
//解密
public function decrypt($decrypt, $key) {
$decoded = base64_decode($decrypt);
$blockSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($blockSize, MCRYPT_RAND);
$key2 = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key2, $decoded, MCRYPT_MODE_ECB, $iv);
return $this->_pkcs5Unpad($decrypted);
}
}
附录:
感谢 php中关于AES加密对应Java中的SHA1PRNG方式加密的实例详解 文章的启发