简体   繁体   English

加密 - 解密AES PHP

[英]Encrypt - Decrypt AES PHP

I would like to encrypt and decrypt somes data with AES CCM! 我想用AES CCM加密和解密一些数据!

I managed to do this operation in the same php file. 我设法在同一个php文件中执行此操作。 But I would like to be able to send the encrypted data to another page to decrypt it with. 但我希望能够将加密数据发送到另一个页面进行解密。 But impossible ... Yet I send the iv, the tag and the encrypted data. 但不可能......但我发送了iv,标签和加密数据。 Do you have a solution? 你有解决方案吗?

I have these errors: 我有这些错误:

Warning: openssl_decrypt(): Setting tag for AEAD cipher decryption failed in adddata1.php on line 18 警告:openssl_decrypt():在第18行的adddata1.php中为AEAD密码解密设置标记失败

Fatal error: Uncaught Exception: OpenSSL error: 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 致命错误:未捕获异常:OpenSSL错误:错误:0607A082:数字信封例程:EVP_CIPHER_CTX_set_key_length:adddata1.php中的密钥长度无效:21堆栈跟踪:第21行dddata1.php中引发的#0 {main}

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 谢谢

The issue is likely the iv. 问题可能是iv。 You are generating random bytes and adding them as a request parameter in a URL, where string encodings matter. 您正在生成随机字节并将其作为请求参数添加到URL中,其中字符串编码很重要。 Convert the bytes to characters which are valid in a URL. 将字节转换为URL中有效的字符。 bin2hex is one simple method: bin2hex是一个简单的方法:

echo '<a href="adddata1?data='.$ciphertext.'&iv='.bin2hex($iv)...

And on the receiving end convert it back: 并在接收端将其转换回来:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM