2017-02-17 93 views
-1

這裏結束返回垃圾文字是我使用的加密和解密文本類:與Rijndael算法(AES)解密的字符串

class Cipher { 

    private $key, $iv; 

    function __construct() { 

     $this->key = "edrtjfjfjlldldld"; 
     $this->iv = "5666685225155700988888995512AbCd"; 
    } 

    function encrypt($text) { 

     $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); 
     $padding = $block - (strlen($text) % $block); 
     $text .= str_repeat(chr($padding), $padding); 
     $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->key, $text, MCRYPT_MODE_CBC, $this->iv); 

     return rtrim(base64_encode($crypttext),"\0"); 
    } 

    function decrypt($input) { 

     $dectext = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->key, base64_decode($input), MCRYPT_MODE_CBC, $this->iv),"\0"); 
     return $dectext; 
    } 
} 

輸出:

Input : 103500.00 
Encrypt: or+JRAS+lnLVcHgKfrV8URMAdWLs440EEapTT7PlQck= 
Decrypt: 103500.00(+garbled characters) 

爲什麼是垃圾字符出現在解密文本中?

+1

這與java有什麼關係? – px06

+0

我試過php ... – sridhard

回答

0

我會說這有什麼應對的填充或密鑰大小。您要求RIJNDAEK 256位,但提供128位密鑰。

0

請嘗試以下功能,在應用程序上完美工作 此處 $ q是$輸入。

function encryptIt($q) { 
     $cryptKey = 'qJB0rGtInRCD5UB1xG03efyCpRKRV'; 
     $qEncoded  = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($cryptKey), $q, MCRYPT_MODE_CBC, md5(md5($cryptKey)))); 
     return($qEncoded); 
    } 

function decryptIt($q) { 
    $cryptKey = 'qJB0rGtInRCD5UB1xG03efyCpRKRV'; 
    $qDecoded  = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($cryptKey), base64_decode($q), MCRYPT_MODE_CBC, md5(md5($cryptKey))), "\0"); 
    return($qDecoded); 
} 
相關問題