du27271 2017-12-14 11:27
浏览 129
已采纳

PHP openssl_decrypt大约有10%的时间返回奇怪的输出

Using all the knowledge I could find on the internet I came up with a salt openssl encrypt and decrypt function. It works 90% of the time but every once in a wile the decrypt returns wither an empty sting or random bytes that have nothing to do with the encrypted data. The code I'm using is:

function Encrypt($subject, $method, $saltLength, $key){
    if($method==0){
        $salt=random_bytes($saltLength);
        $key=hash('sha256', $salt.$key);
        $iv=random_bytes(16);
        $encrypt=base64_encode(openssl_encrypt($subject, 'aes-256-ctr', $key, 0, $iv));
        $return=base64_encode("$method:$salt:$iv:$encrypt");
    }
    return($return);
}
function Decrypt($subject, $key){
    $components = explode( ':', base64_decode($subject));
    $method=$components[0];
    if($method==0){
        $salt=$components[1];
        $key = hash('sha256', $salt.$key);
        $iv=$components[2];
        $encrypt=base64_decode($components[3]);
        $return=(openssl_decrypt($encrypt, 'aes-256-ctr', $key, 0, $iv));
    }
    return($return);
}

$testE=Encrypt('Test', 0, 20, 'Key0');
var_dump($testE);
echo('<br><br>');
$testD=Decrypt($testE, 'Key0');
var_dump($testD);
echo('<br><br>');
$testD=Decrypt($testE, 'Key0');
var_dump($testD);
echo('<br><br>');

And an example of the faulty results for this code would be:

string(72) "MDrVsJAIE8OFjyM6y5dt+xkij5m/rTrHTnzeWs0IRrTKvNR1L7wfOmJrSlNRWFIzUFQwPQ==" 

string(1) "�" 

string(1) "�" 

Or the return of a completely empty string:

string(72) "MDrQ+5c83vJXnva1OjrbJ2RWGjU5ajrwCn+qCneIIOqh48Gt0JmbOllsZFVUMHRuUFQwPQ==" 

string(0) "" 

string(0) "" 

The reason I did the decrypt twice is to see if the fault is in the decrypt or the encrypt, the results clearly show that the encrypt is faulty in some way.

My question is what is causing this faulty encryption of the data? & can I improve this method and make it even more secure ?

  • 写回答

1条回答 默认 最新

  • dsadsadsa1231 2017-12-14 12:21
    关注

    While not the answer to why it is happening, oftentimes the result is what is most important.

    If that is the case, then, if you get 90% accuracy, a while loop will quickly get you on your way.....

    while(strlen($testD) < 5) {
        $testD=Decrypt($testE, 'Key0');
    }
    

    If your goal is to figure out what causes the minor faults, then you may have to look much deeper into the core code. However, in my position, "get it done and move on" is often what the bosses say, so finding a working solution for gathering the info is often what I have to work with.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!