java代码
public class Encryption {
private static final String ALGORITHM = "AES";
public static SecretKey getSecretKey(String key) throws Exception{
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), ALGORITHM);
return secretKeySpec;
}
public static byte[] encryptionByAES(byte[] data,String key){
try {
// 创建解密器
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// 获取Key
SecretKey secretKey = getSecretKey(key);
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
return cipher.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("method AESUtils.decryptionByAES is error");
}
}
public static void main(String[] args) {
String str = "sadfsdfsdfsdfsf";
String key = "1234567891234567";
byte[] ret = encryptionByAES(str.getBytes(), key);
String encryptedStr = Base64.getEncoder().encodeToString(ret);
System.out.println(encryptedStr);
}
}
PHP代码
public function test()
{
$plaintext = "sadfsdfsdfsdfsf";
$cipher = "AES-128-ECB";
$key = "1234567891234567";
$encrypted = openssl_encrypt($plaintext, $cipher, $key, OPENSSL_PKCS1_PADDING);
var_dump(base64_encode($encrypted));
}
特此记录。