加密 - 解密 AES PHP

4

我想使用AES CCM加密和解密一些数据!

我已经在同一个php文件中完成了这个操作。但是我希望能够将加密数据发送到另一个页面进行解密。但是不可能...尽管我发送了iv、标记和加密数据。

你有解决方案吗?

我遇到了以下错误:

警告:openssl_decrypt():在adddata1.php的第18行设置AEAD密码解密标记失败

致命错误:未捕获异常:OpenSSL错误:error:0607A082:digital envelope routines:EVP_CIPHER_CTX_set_key_length:invalid key length in adddata1.php:21 Stack trace: #0 {main} thrown in dddata1.php on line 21

First file :

$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>';
?>

Second file :

$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);
?>

In one file :

<?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);
?>

Thanks


你能否编辑你的帖子,展示在"one"文件中有效的代码吗?此外,在使用错误报告功能时,是否有任何返回信息?http://php.net/manual/en/function.error-reporting.php - Funk Forty Niner
假设传输问题。看起来base64的东西是反过来的?为什么在接收端进行编码,而不是解码?发送时何时进行编码?为什么在输出时更改,但在使用openssl函数时不更改?检查它通过电线时是否更改。 - ficuscr
没有任何返回,我有以下错误: 警告:openssl_decrypt():在adddata1.php的第18行中设置AEAD密码解密标记失败致命错误:未捕获的异常:OpenSSL错误:error:0607A082:digital envelope routines:EVP_CIPHER_CTX_set_key_length:invalid key length in adddata1.php:21堆栈跟踪:#0 {main}抛出adddata1.php的第21行 - user46510
1个回答

6
问题可能出在iv上。您正在生成随机字节并将它们作为请求参数添加到URL中,其中字符串编码很重要。请将这些字节转换为URL有效的字符。使用bin2hex是一种简单的方法: bin2hex
echo '<a href="adddata1?data='.$ciphertext.'&iv='.bin2hex($iv)...

在接收端将其转换回来:

$iv = hex2bin($_GET['iv']);

太完美了!非常感谢! - user46510

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接