作者: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加密的任督二脉。
分享到:
相关推荐
21世纪教育研究院:应对人口变局_深化教育改革-20230522-24页(1)
基于大数据的智慧消防整体解决方案.pdf
AUTOSAR_SRS_DIODriver.pdf
外包
平方差
纸片战争Beta压缩试行代码
B2092 开关灯
python模块onnxruntime版本
未命名12
Java毕业设计-人事档案管理演示视频(高分期末大作业)
小玉的零花钱
redis基本命令
6951 - Robot Command Center.mpd
vhdl,洗衣机VHDL控制器实验报告洗衣机VHDL控制器实验报告
onnxruntime-1.15.1-cp311-cp311-linux_armv7l.whl.zip
Java毕业设计-ssm基于微信小程序的农产品销售平台演示录像(高分期末大作业)
7101 - Lightsaber Duel.mpd
vuInhub靶场实战系列-Kioptrix Level #3
6844 - Seismologic Vehicle.mpd
幻方