【问题标题】:openssl decrypt function not working properly after php upgradephp升级后openssl解密功能无法正常工作
【发布时间】:2021-03-29 10:52:29
【问题描述】:

我有一个使用openssl_encryptopenssl_decrypt 的PHP 应用程序,它在过去四年中一直运行良好。
最近,该应用在调用openssl_decrypt函数时显示false

这是加密部分:

<?php
$password = "iR0nM@N2017!?KOreVoNick";
$method = "aes128";
$iv = "69kjg23423L@cEv7";

$montant = htmlentities($_POST['montant'])-$mutation;
$numeroCheque = openssl_encrypt(htmlentities($_POST['numeroCheque']), $method, $password, 0, $iv);
$designationSociete = openssl_encrypt(htmlentities($_POST['designationSociete']), $method, $password, 0, $iv);
$designationPersonne = openssl_encrypt(htmlentities($_POST['designationPersonne']), $method, $password, 0, $iv);
$dateCheque = htmlentities($_POST['dateCheque']);
$idProjet = htmlentities($_POST['idProjet']);
$createdBy = $login;
$created = date('d/m/Y h:m');
$statut = htmlentities($_POST['statut']);
$compteBancaire = openssl_encrypt(htmlentities($_POST['compteBancaire']), $method, $password, 0, $iv);
$url = "";
$cheque = new Cheque(array('numero' => $numeroCheque , 'montant' => $montant,
    'designationSociete' => $designationSociete, 'designationPersonne' => $designationPersonne, 
    'dateCheque' => $dateCheque, 'idProjet' =>$idProjet, 'idSociete' => $idSociete, 'compteBancaire' => $compteBancaire, 'createdBy' => $createdBy, 'created' => $created,
    'statut' => $statut, 'url' => $url));
    $chequeManager = new ChequeManager($pdo);
    $chequeManager->add($cheque);

解密很简单:

    openssl_decrypt($cheque->numero(), $method, $password, 0, $iv);

这是我使用openssl_error_string()时遇到的错误

'error:0607A082:digital envelope routines:EVP_CIPHER_CTX_set_key_length:invalid key length'

有什么建议吗?

【问题讨论】:

  • $cheque-&gt;numero()的内容是什么?
  • $cheque->numero() 它是一个字符串
  • 看看这个过程的另一半可能会有用,你能显示你的加密代码吗
  • 我猜你的问题中的关键与你程序中的关键不一样,这是明智的。在完全相同的长度上尝试一下,看看是否仍然出现错误...
  • 如果您向我们展示的代码确实有效,它会有所帮助。 Minimal, Complete and Verifiable Example

标签: php openssl


【解决方案1】:

我尝试了以下方法:

<?php

$cleartext = "The quick brown fox jumps over the lazy dog";
$password = "iR0nM@N2017!?KOreVoNick";
$method = "aes128";
$iv = "69kjg23423L@cEv7";

$enctext = openssl_encrypt($cleartext, $method, $password, 0, $iv);

$dectext = openssl_decrypt($enctext, $method, $password, 0, $iv);
header("content-type:text/plain");

echo "decrypted: $dectext\n\n";
echo "encrypted: $enctext\n\n";
echo "orig: $cleartext\n";

结果

decrypted: The quick brown fox jumps over the lazy dog

encrypted: fyYcGEVOpH9cEZuBIN4S1GRDp/kU+Kzv1UJUp2UBGpPv/R+BxxbBDArKa+ugvOOr

orig: The quick brown fox jumps over the lazy dog

因此我的结论是,您在解密内容的长度或填充方面存在一些问题。

PHP版本:

Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.4.3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 2017-06-10
    • 1970-01-01
    相关资源
    最近更新 更多