BlowfishEncrypter.php 972 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wilson
  5. * Date: 16/7/7
  6. * Time: 16:21
  7. */
  8. namespace app\common\modules\yop\sdk\Util;
  9. abstract class BlowfishEncrypter{
  10. /**
  11. * 算法,另外还有192和256两种长度
  12. */
  13. const CIPHER = MCRYPT_BLOWFISH;
  14. /**
  15. * 模式
  16. */
  17. const MODE = MCRYPT_MODE_CFB;
  18. /**
  19. * 加密
  20. * @param string $str 需加密的字符串
  21. * @param string $key 密钥
  22. * @return type
  23. */
  24. static public function encode( $str, $key){
  25. $md5Key = md5($key);
  26. return base64_encode(mcrypt_encrypt(self::CIPHER, substr($md5Key,0,16), $str, self::MODE, substr($md5Key,0,8)));
  27. }
  28. /**
  29. * 解密
  30. * @param type $str
  31. * @param type $key
  32. * @return type
  33. */
  34. static public function decode( $str, $key ){
  35. $md5Key = md5($key);
  36. return mcrypt_decrypt(self::CIPHER, substr($md5Key,0,16),base64_decode($str), self::MODE, substr($md5Key,0,8));
  37. }
  38. }