php - PHP 加密/解密 - AES-256-ECB
问题描述
我正在尝试使用 AES-256-ECB 解密电子邮件地址。这有点困难,因为每个来源都展示了不同的方法,而我得到了不同的结果。当然,我没有得到我想要的结果。请轻松评论我尝试过的代码——这个函数到现在已经改变了大约一百万次。
废话代码:
function my_simple_crypt( $string, $action = 'e' ) {
$secret_key = hex2bin('9cc25c7879fc94d5a19eeb8e47573b8423becb608a9a4e9d3c25c20aa7e04357');
$output = false;
$encrypt_method = "AES-256-ECB";
//$secret_key = openssl_digest($secret_key, $encrypt_method, true);
//$key = hash( 'sha256', $secret_key );
if( $action == 'e' ) {
$output = openssl_encrypt( $string, $encrypt_method, $secret_key, 3 );
//$output = bin2hex($output);
//$output = unpack('H*', $output);
} else if( $action == 'd' ) {
$output = openssl_decrypt( $string, $encrypt_method, $secret_key, 3 );
//$output = base64_encode($output);
$output = bin2hex($output);
}
return $output;
}
echo '<pre>';
print_r(my_simple_crypt( 'testuser@gmail.com', 'e' ));
echo '</pre>';
echo 'Encrypt: ' . my_simple_crypt( 'testuser@gmail.com', 'e' ) . '<br>';
echo 'Decrypt: ' . my_simple_crypt( hex2bin('8dd714df21027133cd422d0301af3cb973374ee72008c3f9bd255f6d236da65e'), 'd' );
解决方案
由于您的密钥和密文采用十六进制编码,您需要将它们转换回二进制数据,然后才能将它们提供给解密函数。
下面的代码给出了这个输出:
plaintext decrypted: testuser@gmail.com
plaintext expected: testuser@gmail.com
**安全警告:以下代码使用UNSECURE ECB 模式:
<?php
$keyHex = '9cc25c7879fc94d5a19eeb8e47573b8423becb608a9a4e9d3c25c20aa7e04357';
$ciphertextHex = '8dd714df21027133cd422d0301af3cb973374ee72008c3f9bd255f6d236da65e';
$plaintextExpected = 'testuser@gmail.com';
$key = hex2bin($keyHex);
$ciphertext = hex2bin($ciphertextHex);
$plaintext = openssl_decrypt($ciphertext, 'aes-256-ecb', $key, true);
echo 'plaintext decrypted: ' . $plaintext . PHP_EOL;
echo 'plaintext expected: ' . $plaintextExpected . PHP_EOL;
推荐阅读
- python - 创建新字典以首先对键进行排序,然后对值进行排序
- c++ - 单链表,C++ 分段错误,添加到列表,打印
- html - Bootstrap 优先于我的 CSS - 具有正确的实现顺序
- c# - 什么空;在foreach cicle 中做什么?
- bash - 如何搜索文本文件每行的第一个字段,如果找到匹配项则删除整行
- nlp - 语言模型中的“theta”是什么意思?
- c++ - 在 Windows 上将 Poco 中的错误与 Conan 包管理链接
- vue.js - 在 NUXT 中使用 vue-apollo 从 Vuex 商店访问 this.$apollo?
- javascript - 元素的直接子元素的 Element.querySelector
- httprequest - 调用需要 API 26(当前最少 21):getHeader