您好,登錄后才能下訂單哦!
如何在php中使用blowfish加密解密算法?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
php是一個嵌套的縮寫名稱,是英文超級文本預(yù)處理語言,它的語法混合了C、Java、Perl以及php自創(chuàng)新的語法,主要用來做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。
具體內(nèi)容如下
<?php /** * php blowfish 算法 * Class blowfish */ class blowfish{ /** * blowfish + cbc模式 + pkcs5補(bǔ)碼 加密 * @param string $str 需要加密的數(shù)據(jù) * @return string 加密后base64加密的數(shù)據(jù) */ public function blowfish_cbc_pkcs5_encrypt($str) { $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, ''); //pkcs5補(bǔ)碼 $size = mcrypt_get_block_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC); $str = $this->pkcs5_pad($str, $size); if (mcrypt_generic_init($cipher, $this->key, $this->iv) != -1) { $cipherText = mcrypt_generic($cipher, $str); mcrypt_generic_deinit($cipher); return base64_encode($cipherText); } mcrypt_module_close($cipher); } /** * blowfish + cbc模式 + pkcs5 解密 去補(bǔ)碼 * @param string $str 加密的數(shù)據(jù) * @return string 解密的數(shù)據(jù) */ public function blowfish_cbc_pkcs5_decrypt($str) { $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, ''); if (mcrypt_generic_init($cipher, $this->key, $this->iv) != -1) { $cipherText = mdecrypt_generic($cipher, base64_decode($str)); mcrypt_generic_deinit($cipher); return $this->pkcs5_unpad($cipherText); } mcrypt_module_close($cipher); } private function pkcs5_pad($text, $blocksize){ $pad = $blocksize - (strlen ( $text ) % $blocksize); return $text . str_repeat ( chr ( $pad ), $pad ); } private function pkcs5_unpad($str){ $pad = ord($str[($len = strlen($str)) - 1]); return substr($str, 0, strlen($str) - $pad); } }
BlowFish加密算法在php的使用第二例
<?php $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, ''); // The block-size of the Blowfish algorithm is 64-bits, therefore our IV // is always 8 bytes: $iv = '12345678'; $key256 = '1234567890123456ABCDEFGHIJKLMNOP'; $key128 = '1234567890123456'; printf("iv: %s\n",bin2hex($iv)); printf("key256: %s\n",bin2hex($key256)); printf("key128: %s\n",bin2hex($key128)); $cleartext = 'The quick brown fox jumped over the lazy dog'; printf("clearText: %s\n\n",$cleartext); // Do 256-bit blowfish encryption: // The strengh of the encryption is determined by the length of the key // passed to mcrypt_generic_init if (mcrypt_generic_init($cipher, $key256, $iv) != -1) { // PHP pads with NULL bytes if $cleartext is not a multiple of the block size.. $cipherText = mcrypt_generic($cipher,$cleartext ); mcrypt_generic_deinit($cipher); // Display the result in hex. printf("256-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText)); } // 128-bit blowfish encryption: if (mcrypt_generic_init($cipher, $key128, $iv) != -1) { // PHP pads with NULL bytes if $cleartext is not a multiple of the block size.. $cipherText = mcrypt_generic($cipher,$cleartext ); mcrypt_generic_deinit($cipher); // Display the result in hex. printf("128-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText)); } // ------- // Results // ------- // You may use these as test vectors for testing your Blowfish implementations... // // iv: 3132333435363738 // key256: 313233343536373839303132333435364142434445464748494a4b4c4d4e4f50 // key128: 31323334353637383930313233343536 // clearText: The quick brown fox jumped over the lazy dog // // 256-bit blowfish encrypted: // 276855ca6c0d60f7d9708210440c1072e05d078e733b34b4198d609dc2fcc2f0c30926cdef3b6d52baf6e345aa03f83e // // 128-bit blowfish encrypted: // d2b5abb73208aea3790621d028afcc74d8dd65fb9ea8e666444a72523f5ecca60df79a424e2c714fa6efbafcc40bdca0 ?>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。