【发布时间】:2017-09-14 17:08:15
【问题描述】:
我想用 AES CCM 加密和解密一些数据!
我设法在同一个 php 文件中执行此操作。但我希望能够将加密的数据发送到另一个页面以对其进行解密。但不可能......但我发送了静脉注射、标签和加密数据。 你有解决办法吗?
我有这些错误:
警告:openssl_decrypt():在第 18 行的 adddata1.php 中设置 AEAD 密码解密的标签失败
致命错误:未捕获的异常:OpenSSL 错误:错误:0607A082:数字信封例程:EVP_CIPHER_CTX_set_key_length:adddata1.php:21 中的密钥长度无效堆栈跟踪:#0 {main} 在第 21 行的 dddata1.php 中抛出
第一个文件:
$algo = 'aes-128-ccm';
$iv = random_bytes(openssl_cipher_iv_length($algo));
$key = "cd9344040aa9f9217871d46ee871c59c";
$data = '00000000010-3b57af';
$ciphertext = openssl_encrypt(
$data,
$algo,
$key,
OPENSSL_RAW_DATA,
$iv,
$tag
);
echo'<a href="adddata1?data='.$ciphertext.'&tag='.$tag.'&iv='.$iv.'">"decrypte"</a>';
?>
第二个文件:
$algo = 'aes-128-ccm';
$key = "cd9344040aa9f9217871d46ee871c59c";
$ciphertext = $_GET['data'];
$iv = $_GET['iv'];
$tag = $_GET['tag'];
// Change 1 bit in additional authenticated data
// $i = rand(0, mb_strlen($aad, '8bit') - 1);
// $aad[$i] = $aad[$i] ^ chr(1);
$decrypt = openssl_decrypt(
$ciphertext,
$algo,
$key,
OPENSSL_RAW_DATA,
$iv,
$tag
);
if (false === $decrypt) {
throw new Exception(sprintf(
"OpenSSL error: %s", openssl_error_string()
));
}
printf ("Decryption %s\n", $data === $decrypt ? 'Ok' : 'Failed');
printf("<br/>");
printf(base64_encode($tag));
printf("<br/>");
printf(base64_encode($iv));
printf("<br/>");
printf(base64_encode($ciphertext));
printf("<br/>");
printf($data);
?>
在一个文件中:
<?php
$algo = 'aes-128-ccm';
$iv = random_bytes(openssl_cipher_iv_length($algo));
$key = "cd9344040aa9f9217871d46ee871c59c";
$data = '00000000010-3b57af';
$ciphertext = openssl_encrypt(
$data,
$algo,
$key,
OPENSSL_RAW_DATA,
$iv,
$tag
);
// Change 1 bit in additional authenticated data
// $i = rand(0, mb_strlen($aad, '8bit') - 1);
// $aad[$i] = $aad[$i] ^ chr(1);
$decrypt = openssl_decrypt(
$ciphertext,
$algo,
$key,
OPENSSL_RAW_DATA,
$iv,
$tag
);
if (false === $decrypt) {
throw new Exception(sprintf(
"OpenSSL error: %s", openssl_error_string()
));
}
printf ("Decryption %s\n", $data === $decrypt ? 'Ok' : 'Failed');
printf("<br/>");
printf(base64_encode($tag));
printf("<br/>");
printf(base64_encode($iv));
printf("<br/>");
printf(base64_encode($ciphertext));
printf("<br/>");
printf($data);
?>
谢谢
【问题讨论】:
-
您可以编辑您的帖子以显示“one”文件中的工作原理吗?另外,使用错误报告时是否有任何返回? php.net/manual/en/function.error-reporting.php
-
假设传输问题。好像base64的东西是倒退的?为什么在接收端编码,不应该解码?发送时在哪里编码?为什么在输出时改变但在 openssl 函数中使用时不改变?当它通过电线时检查变化。
-
什么都没有回来,我有这些错误:警告:openssl_decrypt():在第 18 行的 adddata1.php 中设置 AEAD 密码解密的标记失败致命错误:未捕获的异常:OpenSSL 错误:错误:0607A082 :digital envelope routines:EVP_CIPHER_CTX_set_key_length:invalid key length in adddata1.php:21 Stack trace: #0 {main} throw in adddata1.php on line 21
标签: php encryption cryptography