首页 > 解决方案 > openssl_encrypt 从不可解密的数组值创建不同的代码

问题描述

非常感谢会员提供的宝贵帮助。

我刚刚开始加密存储在 MySQL 数据库表中的数据,学习如何加密将上传到表中的二维数组值。

我现在遇到的第一个问题是 openssl_encrypt 生成不同的、不可解密的代码,而不是直接从纯文本生成代码,后者可以毫无问题地解密。

第二个问题是我无法让“for () {continue}”命令正常工作,因为我不希望对列或字段 [0] 和 [1] 进行加密,这会显示在生成的网页中:https ://secur-a-doc.com/php-sprdsht-sample6.0

我用来测试的PHP代码如下:

<?php
    header( 'Content-Type: text/html; charset=utf-8' );

    echo "Below is a 3-row, 2-dimensional array displaying sections, fields and values";
    $bgyaa = array (
    '[0]' => array ( 
        '[0]' => '2',
        '[1]' => 'bgyaa.ZBRDE5aTZsUGZmWQ',
        '[2]' => '12346',
        '[3]' => 'John Citizen',
        '[4]' => 'noy-pic-1.jpg',
        '[5]' => 'noy-pic-2.jpg',
        '[6]' => 'RESIDENT',
        '[7]' => '777 Sarangani Street',
        '[8]' => '03/27/84',
        '[9]' => 'B',
        '[10]' => '287-865-194',
        '[11]' =>' '),
    '[1]' => array ( 
        '[0]' => '3',
        '[1]' => 'bgyaa.ZMTEtpTC5qVGNTUQ',
        '[2]' => '12347',
        '[3]' => 'Dominador Pridas',
        '[4]' => 'domeng-pic-1.jpg',
        '[5]' => 'domeng-pic-2.jpg',
        '[6]' => 'TENANT',
        '[7]' => '321 Mango Drive',
        '[8]' => '03/27/84',
        '[9]' => 'B',
        '[10]' => '287-865-194',
        '[11]' =>' ' ),
    '[2]' => array ( 
        '[0]' => '4',
        '[1]' => 'bgyaa.ZpcEpteDJOZlBVQQ',
        '[2]' => '12348',
        '[3]' => 'Taylor Swift',
        '[4]' => 'taylorswift-pic-1.jpg',
        '[5]' => 'taylorswift-pic-2.jpg',
        '[6]' => 'TENANT',
        '[7]' => '826 Anonas Street',
        '[8]' => '03/27/84',
        '[9]' => 'B',
        '[10]' => '287-865-194',
        '[11]' =>' ' ), 
        );


    echo "<pre>";
    foreach ($bgyaa as $section => $items)
        {
            foreach ($items as $key => $value)
            {
                echo "$section:\t$key:\t$value<br/>";
            }
        }                       

    $key="c871754451c2b89d4cdb1b14705be457b7fabe967af6a559f3d20c79ded5b5ff18675e56fa77d75fdcd47c34271bb74e372d6d04652f7aa6f529a838ca4aa6bd";
    $iv= "f1e64276d153ad8a"; // this iv value is 16 bytes of hex characters
    $cipher = "aes-256-cbc-hmac-sha256";

    if (in_array($cipher, openssl_get_cipher_methods()))
    {
            $ivlen = openssl_cipher_iv_length($cipher);
        $plain_text = 'John Citizen';
        $encrypted = openssl_encrypt($plain_text, $cipher, $key, $options=0, $iv);
        echo "<br/><br/><br/>Bellw are from direct encrytion of the plain text name<br/>";
        echo "plain text is John Citizen " . "<br/>";
        echo "encrypted text is " . $encrypted . "<br/><br/><br/>";
    }

        echo "And then below are openssl_encrypt (cipher aes-256-cbc) encrypted array codes beside their plain text original values<br/>";
        echo "NOTE that the encrypted code q+vG/KXTZsYExxV5yX7DFw== for the name John Citizen is different to the above, and not decryptable<br/><br/>";
        foreach ($bgyaa as $section => $items)     // section is the sub array (starts from 0)
            // items are rows
        {
            foreach ($items as $key => $value) 
            //key represents field or column
            //value represents the values of row
            {
            if ($items < 2) 
                {
                    continue; //begin from key or field 4
                    }
                if (in_array($cipher, openssl_get_cipher_methods()))
                    {
                        $ivlen = openssl_cipher_iv_length($cipher);             
                        $encrypted = openssl_encrypt($value, $cipher, $key, $options=0, $iv);
                    }                   
                    echo $key . " : " . $encrypted . "  :  " . $value . "<br/>";                            
            }
    }
     
    echo "</pre>";   

?>

标签: phparraysencryption

解决方案


您正在覆盖循环$key中的加密。for

看:

$key="c871754451c2b89d4cdb1b14705be457b7fabe967af6a559f3d20c79ded5b5ff18675e56fa77d75fdcd47c34271bb74e372d6d04652f7aa6f529a838ca4aa6bd";

...

foreach ($bgyaa as $section => $items) // section is the sub array (starts from 0)
    // items are rows
    {
        foreach ($items as $key => $value) {
            // Now the encryption $key has been overwritten with the index. 
            // In your example, to decrypt 'John Citizen', 
            // You would need to decrypt using '[3]' as the encryption key. 
        }
    }
}

只需将底部 for 循环更改为索引变量使用不同的值,如下所示:

foreach ($items as $index => $value) // changed $key to $index
{
    //key represents field or column
    //value represents the values of row
    if ($items < 2) 
    {
        continue; //begin from key or field 4
    }
    if (in_array($cipher, openssl_get_cipher_methods()))
    {
        $ivlen = openssl_cipher_iv_length($cipher);             
        $encrypted = openssl_encrypt($value, $cipher, $key, $options=0, $iv);
    }    
    // Changed to also use $index instead of $key.   
    echo $index . " : " . $encrypted . "  :  " . $value . "<br/>";                            
}

至于您的continue问题,您是否真的希望将索引格式化为字符串[0][1]而不仅仅是数字?如果你想要数字索引,你应该像这样格式化数组:

0 => array ( 
    0 => '2',
    1 => 'bgyaa.ZBRDE5aTZsUGZmWQ',
    2' => '12346',
    3 => 'John Citizen'
)

然后您可以将continue条件更改为:

if ($index < 2) 
{
    continue; //begin from key or field 4
}

否则,如果您想将格式保留为像 一样的文字字符串"[2]",那么您需要在进行比较之前删除括号。

if (str_replace(['[',']'], '', $index) < 2) 
{
    continue; //begin from key or field 4
}

总而言之,这是我对应用所有内容的最终更改的建议:

foreach ($items as $index => $value) // changed $key to $index
{
    //key represents field or column
    //value represents the values of row
    //if ($index < 2)  // UNCOMMENT IF YOU USE NUMERICAL INDEXES
    if (str_replace(['[',']'], '', $index) < 2) // KEEP IF YOU USE STRING INDEXES WITH BRACKETS
    {
        continue; //begin from key or field 4
    }
    if (in_array($cipher, openssl_get_cipher_methods()))
    {
        $ivlen = openssl_cipher_iv_length($cipher);             
        $encrypted = openssl_encrypt($value, $cipher, $key, $options=0, $iv);
    }    
    // Changed to also use $index instead of $key.   
    echo $index . " : " . $encrypted . "  :  " . $value . "<br/>";                            
}

推荐阅读