簡體   English   中英

Php openSLL 加密 - 防止特殊字符

[英]Php openSLL Encryption - to prevent special characters

我想為 url 中傳遞的獲取變量和異步調用創建和加密

例如:

$textToEncrypt = "Hello World";
$encryptionMethod = "AES-256-CBC";
$secretHash = "cVb67YtfAz328oOikl96vBn";
$iv = "adfrf54dmnlo09ax";
$encryptedText = openssl_encrypt($textToEncrypt,$encryptionMethod,$secretHash, 0, $iv);

結果是:W2p0S2qlSierJnIcA/AM3g==

有一些特殊字符,== 總是在最后。 我想阻止這個? 我怎么能output只有0-9和AZ和az字符?

謝謝

$ciphering = "AES-128-CTR";

使用它作為你的密碼字符串,它會從最后刪除任何類型的 ==

我遇到過同樣的問題。 我想刪除特殊字符。 所以,這就是我所做的。 使用base64_encode($encryptedText)將加密的文本轉換為十六進制值。 因此,將沒有特殊字符。 然后,要進行還原, base64_decode在傳遞給openssl_decrypt之前使用openssl_decrypt

我注意到我的加密字符串的末尾也有正好2個等號。 似乎最后總是有兩個等號。 這是我的解決方案

function encryptString($string, $action, $baseIP = 'false', $extraKey = ''){
    global $flag;

    $encryptedIP = '';

    if($baseIP){
        $encryptedIP = encryptString($_SERVER['REMOTE_ADDR'], 'encrypt', false);
    }

    $output = false;

    $encrypt_method = "AES-256-CBC";
    $secret_key = $flag['2nd-encrypt-key'].$encryptedIP.'-'.$extraKey;
    $secret_iv = $flag['2nd-encrypt-secret'].$encryptedIP.'-'.$extraKey;

    $key = hash('sha256', $secret_key);
    $iv = substr(hash('sha256', $secret_iv), 0, 16);

    $output;

    if($action == 'encrypt'){
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
        //replace equal signs with char that hopefully won't show up
        $output = str_replace('=', '[equal]', $output);
    }else if($action == 'decrypt'){
        //put back equal signs where your custom var is
        $setString = str_replace('[equal]', '=', $string);
        $output = openssl_decrypt(base64_decode($setString), $encrypt_method, $key, 0, $iv);
    }

    return $output;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM