php aes128ebc
时间: 2024-01-20 08:15:13 浏览: 121
对于PHP中的AES128-ECB加密算法,可以使用openssl扩展来实现。下面是一个示例代码:
```php
<?php
function aes128ecb_encrypt($data, $key) {
$cipher = "AES-128-ECB";
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
$encrypted = openssl_encrypt($data, $cipher, $key, OPENSSL_RAW_DATA, $iv);
return base64_encode($encrypted);
}
function aes128ecb_decrypt($data, $key) {
$cipher = "AES-128-ECB";
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
$decrypted = openssl_decrypt(base64_decode($data), $cipher, $key, OPENSSL_RAW_DATA, $iv);
return $decrypted;
}
// 示例用法
$data = "Hello, World!";
$key = "0123456789abcdef";
$encrypted = aes128ecb_encrypt($data, $key);
$decrypted = aes128ecb_decrypt($encrypted, $key);
echo "Encrypted: " . $encrypted . "\n";
echo "Decrypted: " . $decrypted . "\n";
?>
```
请注意,这只是一个简单的示例,实际使用中需要根据具体需求进行适当的安全性和错误处理。
阅读全文