基于 defuse/php-encryption 的 AES 在线加解密工具

由于网上现有的 AES 加解密工具似乎不能正常解密我们 Defuse 库加密出来的字符串,就写了一个加解密工具,方便开发时加密解密 AES,基于的是 defuse/php-encryption,部署在 Azure 应用服务上面。

输入密钥

根据 README 描述,这个库似乎只支持 256-bit 的密钥,请使用 AES 密钥在线生成器 生成 256-bit 的密钥使用。

输入文本


加密 / 解密结果

相关代码

encrypt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Exception as Ex;
use Defuse\Crypto\Key;
use Defuse\Crypto\Encoding;

function encrypt($text, $key, $decrypt) {
try {
// Defuse PHP-Encryption requires a key object instead of a string.
$key = Encoding::saveBytesToChecksummedAsciiSafeString(Key::KEY_CURRENT_VERSION, $key);
$key = Key::loadFromAsciiSafeString($key);
if ((bool) $decrypt) {
return Crypto::decrypt((string) $text, $key);
} else {
return Crypto::encrypt((string) $text, $key);
}
} catch (Ex\CryptoException $ex) {
return $decrypt ? 'Decryption failed' : 'Encryption failed';
}
}
composer.json
1
2
3
4
5
6
7
{
...,
"require": {
"php": ">=5.4.0",
"defuse/php-encryption": "~2.0"
}
}

来自 Drupal module - Real AES 的源码,有修改。

基于 defuse/php-encryption 的 AES 在线加解密工具

https://www.imaegoo.com/2020/aes-encryption/

作者

iMaeGoo

发布于

2020-03-25

更新于

2020-03-25

许可协议

CC BY 4.0

评论