作者:caocao(网络隐士),http://www.caocao.name,http://www.caocao.mobi
转载请注明来源:http://caocao.iteye.com/blog/96902
隐士为了在asp.net环境里应用Blowfish加密技术实现单点登录,对Blowfish.NET进行了简单的封装。要下载Blowfish.NET包可自行Google之。
闲话不多说,上代码:
c# 代码
- public string Decrypt(string inString,string keyString)
- {
- UTF8Encoding ue = new UTF8Encoding();
- byte[] key=ue.GetBytes(keyString);
- byte[] inBuffer=ue.GetBytes(inString);
- BlowfishECB ecb=new BlowfishECB(key, 0, key.Length);
- try
- {
- inBuffer=Convert.FromBase64String(inString);
- if (inBuffer.Length % BlowfishECB.BLOCK_SIZE != 0)
- {
- return null;
- }
- }
- catch (FormatException)
- {
- return null;
- }
-
- byte[] outBuffer = new byte[inBuffer.Length];
- Array.Clear(outBuffer, 0, outBuffer.Length);
- ecb.Decrypt(inBuffer,
- 0,
- outBuffer,
- 0,
- inBuffer.Length);
- return ue.GetString(outBuffer, 0, outBuffer.Length);
- }
-
- public string Encrypt(string inString,string keyString)
- {
- UTF8Encoding ue = new UTF8Encoding();
- byte[] key=ue.GetBytes(keyString);
- byte[] inStringBytes=ue.GetBytes(inString);
- int length=inStringBytes.Length;
- length=Convert.ToInt32(Math.Ceiling(1.0*length/BlowfishECB.BLOCK_SIZE))*BlowfishECB.BLOCK_SIZE;
-
- byte[] inBuffer = new byte[length];
- byte[] outBuffer = new byte[length];
- Array.Clear(inBuffer, 0, inBuffer.Length);
- Array.Clear(outBuffer, 0, outBuffer.Length);
- Array.Copy(inStringBytes, inBuffer, inStringBytes.Length);
-
- BlowfishECB ecb=new BlowfishECB(key, 0, key.Length);
- int nCount=ecb.Encrypt(inBuffer,
- 0,
- outBuffer,
- 0,
- inBuffer.Length);
- return Convert.ToBase64String(outBuffer);
- }
实现类似功能的PHP代码很简洁:
PHP 代码
- $outString=base64_encode(@mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $inString, MCRYPT_MODE_ECB));
- $outString=@mcrypt_decrypt(MCRYPT_BLOWFISH, $key, base64_decode($inString), MCRYPT_MODE_ECB);
至此彻底打通PHP,asp.net之间Blowfish加密的任督二脉。
分享到:
相关推荐
用C#,对Blowfish.NET进行了简单的封装。 来源:http://caocao.iteye.com/blog/96902
blowfish 加密解密
blowfish算法,包括Blowfish-ECB,Blowfish-CBC,Blowfish-CTR,Blowfish-OFB,Blowfish-CFB,目前我的Demo只实现了ECB是如何运用的。[/b][/i]注意:嵌入式开发的小伙伴们,如果blowfish.h找不到头文件的时候,需要将...
Blowfish加解密算法(C++实现的)
Java中的Blowfish对称密钥加密算法类和实例
BlowFish Java版实现,里面有工具类,注意修改一下包名!
Blowfish加密算法Blowfish加密算法Blowfish加密算法Blowfish加密算法
blowfish加密解密算法的C#实现,blowfish加密解密算法的C#实现
BlowFish测试程序,可以对任意长度字符串加密。
C#Blowfish加密算法,经测试可用
Blowfish加密算法 Blowfish is a symmetric block cipher that can be effectively used for encryption and safeguarding of data. It takes a variable-length key, from 32 bits to 448 bits, making it ideal ...
用VB实现Blowfish算法。内附完整的Blowfish实现代码。
对称加密算法Blowfish(java).rar 可运行 对称加密算法Blowfish(java).rar 可运行
简单易用的加密算法 BlowFish算法用来加密64Bit长度的字符串。 BlowFish算法使用两个“盒”——ungignedlongpbox[18]和unsignedlongsbox[4,256]。 BlowFish算法中,有一个核心加密函数:BF_En(后文详细介绍)。该...
javascript-河豚Blowfish加密库Javascript,jquery,coffeescript(blowfish.js) 在旧的和新的浏览器中都可以很好地工作。 Blowfish是块密码,块长度是8个字节。 在线 。 该库的主要优点是,它可以与UTF-8中的字符...
blowfish加密程序,带验证代码,验证通过。
blowfish系列题目详解 一份不错的资料。。
Blowfish算法的C#与java实现,代码已经过测试,可以实现C#、java加密解密互通。
Blowfish是一个对称加密块算法,由Bruce Schneider于1993年设计,现已应用在多种加密产品。Blowfish能保证很好的加密速度,并且目前为止没有发现有效地破解方法
BlowFish加密算法For Delphi7