2014-01-25 96 views
0

我正在使用mcrypt函數來加密一些數據 - 來自php.net的例子。mcrypt - 可能的加密數

我想知道某些字符串的加密次數。假設我將運行1千萬次的「產品」字加密。我會得到1千萬種不同的加密嗎?

function encrypt($s) 
{ 
    # --- ENCRYPTION --- 

    # the key should be random binary, use scrypt, bcrypt or PBKDF2 to 
    # convert a string into a key 
    # key is specified using hexadecimal 
    $key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3"); 

    # show key size use either 16, 24 or 32 byte keys for AES-128, 192 
    # and 256 respectively 
    $key_size = strlen($key); 

    # create a random IV to use with CBC encoding 
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); 
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); 

    # creates a cipher text compatible with AES (Rijndael block size = 128) 
    # to keep the text confidential 
    # only suitable for encoded input that never ends with value 00h 
    # (because of default zero padding) 
    $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, 
           $s, MCRYPT_MODE_CBC, $iv); 

    # prepend the IV for it to be available for decryption 
    $ciphertext = $iv . $ciphertext; 

    # encode the resulting cipher text so it can be represented by a string 
    $ciphertext_base64 = base64_encode($ciphertext); 

    return $ciphertext_base64; 

} 
+0

不,如果你使用相同的鹽。 –

+0

每當我刷新頁面時,我會得到不同的字符串 – DeiForm

+0

我們需要查看代碼。分組密碼是確定性的 - 它們將爲相同的輸入提供相同的輸出。但關鍵,IV或鹽可能會有所不同。 –

回答

0

如果您將相同的參數傳遞給mcrypt_encrypt(),您將得到相同的結果。如果您傳遞不同的參數,例如隨機IV或隨機密碼哈希,那麼您將得到不同的結果。你如何構建你的電話?