php blowfish加密解密具体算法
PHP Blowfish 算法的加密解密,供大家参考,具体内容如下
<?php
/**
* php blowfish 算法
* Class blowfish
*/
class blowfish{
/**
* blowfish + cbc模式 + pkcs5补码 加密
* @param string $str 需要加密的数据
* @return string 加密后base64加密的数据
*/
public function blowfish_cbc_pkcs5_encrypt($str)
{
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
//pkcs5补码
$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 解密 去补码
* @param string $str 加密的数据
* @return string 解密的数据
*/
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
?>
php blowfish加密解密具体算法的更多相关文章
- 使用python进行加密解密AES算法
使用python进行加密解密AES算法-代码分享-PYTHON开发者社区-pythoner.org 使用python进行加密解密AES算法 TY 发布于 2011-09-26 21:36:53,分类: ...
- 重新想象 Windows 8 Store Apps (32) - 加密解密: 非对称算法, 数据转换的辅助类
原文:重新想象 Windows 8 Store Apps (32) - 加密解密: 非对称算法, 数据转换的辅助类 [源码下载] 重新想象 Windows 8 Store Apps (32) - 加密 ...
- C#与Java同步加密解密DES算法
在实际项目中,往往前端和后端使用不同的语言.比如使用C#开发客户端,使用Java开发服务器端.有时出于安全性考虑需要将字符加密传输后,由服务器解密获取.本文介绍一种采用DES算法的C#与Java同步加 ...
- JAVA实现RSA加密解密 非对称算法
首先RSA是一个非对称的加密算法.所以在使用该算法加密解密之前,必须先行生成密钥对.包含公钥和私钥 JDK中提供了生成密钥对的类KeyPairGenerator,实比例如以下: public stat ...
- Cookie中存放数据l加密解密的算法
public class CookieUtil { /** * * @param response HttpServletResponse类型的响应 * @param cookie 要设置httpOn ...
- 重新想象 Windows 8 Store Apps (31) - 加密解密: 哈希算法, 对称算法
原文:重新想象 Windows 8 Store Apps (31) - 加密解密: 哈希算法, 对称算法 [源码下载] 重新想象 Windows 8 Store Apps (31) - 加密解密: 哈 ...
- Golang之AES/DES加密解密
AES/DES加密/解密涉及4个概念:1. Block, 也叫分组, 相应加密/解密的算法. 2. BlockMode, 模式, 相应加密/解密的处理.3. InitalVectory, 初始向量4. ...
- C/C++使用openssl进行摘要和加密解密(md5, sha256, des, rsa)
openssl里面有很多用于摘要哈希.加密解密的算法,方便集成于工程项目,被广泛应用于网络报文中的安全传输和认证.下面以md5,sha256,des,rsa几个典型的api简单使用作为例子. 算法介绍 ...
- 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输
Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...
随机推荐
- mysql 恢复
一.备份的目的 做灾难恢复:对损坏的数据进行恢复和还原需求改变:因需求改变而需要把数据还原到改变以前测试:测试新功能是否可用 二.备份需要考虑的问题 可以容忍丢失多长时间的数据:恢复数据要在多长时间内 ...
- mac电脑忘记账户名密码解决方法
1,重启电脑 ,开机按command+R 2,等苹果开机完成后会进入单用户模式.然后从单用户模式进入终端. 3,在终端里面输入resetpassword. 4,选择系统(root)账户重新输入密码,存 ...
- [GDAL]读取HDF格式的calipso数据
探测地球云层分布的CloudSat和CALIPSO卫星 http://www.nasa.gov/mission_pages/calipso/main/index.html http://www.nas ...
- [SharpDevelop]程序入口
在StartUp项目中,SharpDevelopMain类的Main函数.
- windows安装pip 和easy_install
先安装windows版的easy_install 下载 然后下载pip ,python setup.py install 安装好的 pip和easy_install通常在 python目录的 Scr ...
- C++ 基础复习 1
1. 友元 友元的作用是,友元函数内部可以直接访问外围类的private的字段或方法.通俗的理解就是解决了访问权限的问题. 1) 有点像java的内部类,但是只能在外围类中声明,定义(实现)部分要写在 ...
- 解决td标签上的position:relative属性在各浏览器中的兼容性问题
在css中的position属性规定了页面元素的定位类型,它有以下几个值: absolute:绝对定位,相对于static以外的第一个父元素进行定位: fixed:生成绝对定位的元素,相对于浏览器窗口 ...
- 源码安装zabbix
源码安装zabbix 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 欢迎加入:高级运维工程师之路 598432640 前言:参考网上多篇源码安装的连接,自己把安装过程丢在这 ...
- session 实现保存用户信息
index.jsp <body> <div style="margin: 0 auto; width: 500px; text-align: center;"&g ...
- 纪念我sgu第一个10题!
哎,等下次再做20题纪念一下!尼玛,根本做不出来,还要到处翻别人的555555555555