PHP blowfish encryption and decryption algorithm _php skills

Source: Internet
Author: User
Tags learn php pkcs5

PHP Blowfish algorithm encryption and decryption for your reference, the specific contents are as follows

<?php/** * PHP blowfish algorithm * Class Blowfish/class blowfish{/** * Blowfish + CBC mode + PKCS5 complement encryption * @param  String $str data that needs to be encrypted * base64 encrypted data after @return string encryption */Public Function Blowfish_cbc_pkcs5_encrypt ($str) {$cipher
  = Mcrypt_module_open (Mcrypt_blowfish, ', MCRYPT_MODE_CBC, ');
  PKCS5 Complement $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 mode + PKCS5 decryption to complement * @param string $STR Encrypted data * @return String decrypted data */Public Function B
  Lowfish_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 encryption algorithm in PHP use the second example

<?php $cipher = Mcrypt_module_open (Mcrypt_blowfish, ', MCRYPT_MODE_CBC, ');
 The block-size of the Blowfish algorithm is 64-bits therefore we IV/is always 8 bytes: $iv = ' 12345678 ';
 $key 256 = ' 1234567890123456ABCDEFGHIJKLMNOP ';
 $key 128 = ' 1234567890123456 ';
 printf ("IV:%s\n", Bin2Hex ($IV));
 printf ("key256:%s\n", Bin2Hex ($key 256));
 printf ("key128:%s\n", Bin2Hex ($key 128));
 $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 are determined by the length of the key//passed to Mcrypt_generic_init if (Mcrypt_generic_init ($cipher, $key 256, $IV)!=-1) {//PHP pads with NULL bytes if $cleartext
  Is isn't 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, $key 128, $iv)!=-1) {//PHP pads with NULL bytes if $clear
  The text is not a multiple to 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 could use this as test vectors for testing your Blowfish implementations.
 . iv:3132333435363738//KEY256:313233343536373839303132333435364142434445464748494A4B4C4D4E4F50//key128:3132 3334353637383930313233343536//Cleartext:the quick brown fox jumped over the lazy dog///256-bit Blowfish-Encrypt Ed://276855ca6c0d60f7d9708210440c1072e05d078e733b34b4198d609dc2fcc2f0c30926cdef3b6d52baf6e345aa03f83e/////128- Bit Blowfish encrypted:// D2B5ABB73208AEA3790621D028AFCC74D8DD65FB9EA8E666444A72523F5ECCA60DF79A424E2C714FA6EFBAFCC40BDCA0?>

The above is the entire content of this article, I hope that you learn PHP program help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.