DES加密不一致问题,求大神解决

乔木3124343 2014-03-27 10:20:34
各位大神
小弟在做一个文件传输项目
对方系统用的是c#写的des加解密算法
密钥:Bank2014
加密偏移量:Bank2014
加密模式:CipherMode.CBC、PaddingMode.PKCS5
块长度:64
文件编码:GBK

小弟这边用的是c写的加解密算法我就只设置了密钥:Bank2014
代码是参照大赛写的 位置在:http://www.iteye.com/topic/478024
目前的问题就是对方公司加密出来的文件跟我加密出来的文件不一样
我的代码是否需要设置加密偏移量呢 还是代码本身就有问题
求大神指点 先跪谢了
...全文
309 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-03-28
  • 打赏
  • 举报
回复
using  System;
using  System.Security.Cryptography;
using  System.IO;
using  System.Text;

private	 byte[]	 Keys  =  {	 0xEF,	0xAB,  0x56,  0x78,	 0x90,	0x34,  0xCD,  0x12	};//默认密钥向量
///	 <summary>
///	 DES加密字符串
///	 </summary>
///	 <param	 name="encryptString">待加密的字符串</param>
///	 <param	 name="encryptKey">加密密钥,要求为8位</param>
///	 <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public	string	EncryptDES(string  encryptString,  string  encryptKey)
{
		try
		{
				byte[]	rgbKey	=  Encoding.UTF8.GetBytes(encryptKey.Substring(0,  8));
				byte[]	rgbIV  =  Keys;
				byte[]	inputByteArray	=  Encoding.UTF8.GetBytes(encryptString);
				DESCryptoServiceProvider  dCSP	=  new	DESCryptoServiceProvider();
				MemoryStream  mStream  =  new  MemoryStream();
				CryptoStream  cStream  =  new  CryptoStream(mStream,  dCSP.CreateEncryptor(rgbKey,	rgbIV),	 CryptoStreamMode.Write);
				cStream.Write(inputByteArray,  0,  inputByteArray.Length);
				cStream.FlushFinalBlock();
				return	Convert.ToBase64String(mStream.ToArray());
		}
		catch
		{
				return	encryptString;
		}
}

///	 <summary>
///	 DES解密字符串
///	 </summary>
///	 <param	 name="decryptString">待解密的字符串</param>
///	 <param	 name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
///	 <returns>解密成功返回解密后的字符串,失败返源串</returns>
public	string	DecryptDES(string  decryptString,  string  decryptKey)
{
		try
		{
				byte[]	rgbKey	=  Encoding.UTF8.GetBytes(decryptKey.Substring(0,  8));
				byte[]	rgbIV  =  Keys;
				byte[]	inputByteArray	=  Convert.FromBase64String(decryptString);
				DESCryptoServiceProvider  DCSP	=  new	DESCryptoServiceProvider();
				MemoryStream  mStream  =  new  MemoryStream();
				CryptoStream  cStream  =  new  CryptoStream(mStream,  DCSP.CreateDecryptor(rgbKey,	rgbIV),	 CryptoStreamMode.Write);
				cStream.Write(inputByteArray,  0,  inputByteArray.Length);
				cStream.FlushFinalBlock();
				return	Encoding.UTF8.GetString(mStream.ToArray());
		}
		catch
		{
				return	decryptString;
		}
}
///	 <summary>
///	 use  sha1	to	encrypt	 string
///	 </summary>
public	string	SHA1_Encrypt(string	 Source_String)
{
		byte[]	StrRes	=  Encoding.Default.GetBytes(Source_String);
		HashAlgorithm  iSHA	 =	new	 SHA1CryptoServiceProvider();
		StrRes	=  iSHA.ComputeHash(StrRes);
		StringBuilder  EnText  =  new  StringBuilder();
		foreach	 (byte	iByte  in  StrRes)
		{
				EnText.AppendFormat("{0:x2}",  iByte);
		}
		return	EnText.ToString();
}

namespace  Microsoft.Samples.Security.PublicKey
{
	class  App
	{
		//	Main  entry	 point
		static	void  Main(string[]	 args)
		{
			//	Instantiate	 3	People	for	 example.  See	the	 Person	 class	below
			Person	alice  =  new  Person("Alice");
			Person	bob	 =	new	 Person("Bob");
			Person	steve  =  new  Person("Steve");

			//	Messages  that	will  exchanged.  See  CipherMessage  class	 below
			CipherMessage  aliceMessage;
			CipherMessage  bobMessage;
			CipherMessage  steveMessage;

			//	Example	 of	 encrypting/decrypting	your  own  message
			Console.WriteLine("Encrypting/Decrypting  Your  Own  Message");
			Console.WriteLine("-----------------------------------------");

			//	Alice  encrypts	 a	message	 using	her	 own  public  key
			aliceMessage  =	 alice.EncryptMessage("Alice  wrote  this  message");
			//	then  using	 her  private  key	can	 decrypt  the  message
			alice.DecryptMessage(aliceMessage);
			//	Example	 of	 Exchanging	 Keys  and	Messages
			Console.WriteLine();
			Console.WriteLine("Exchanging  Keys  and  Messages");
			Console.WriteLine("-----------------------------------------");

			//	Alice  Sends  a	 copy  of  her	public	key	 to	 Bob  and  Steve
			bob.GetPublicKey(alice);
			steve.GetPublicKey(alice);

			//	Bob	 and  Steve	 both  encrypt	messages  to  send	to	Alice
			bobMessage	=  bob.EncryptMessage("Hi  Alice!  -  Bob.");
			steveMessage  =	 steve.EncryptMessage("How  are  you?  -  Steve");

			//	Alice  can	decrypt	 and  read	both  messages
			alice.DecryptMessage(bobMessage);
			alice.DecryptMessage(steveMessage);

			Console.WriteLine();
			Console.WriteLine("Private  Key  required  to  read  the  messages");
			Console.WriteLine("-----------------------------------------");

			//	Steve  cannot  read	 the  message  that	 Bob  encrypted
			steve.DecryptMessage(bobMessage);
			//	Not	 even  Bob	can	 use  the  Message	he	encrypted  for	Alice.
			//	The	 RSA  private  key	is	required  to  decrypt  the	RS2	 key  used
			//	in	the	 decryption.
			bob.DecryptMessage(bobMessage);

		}  //  method  Main
	}  //  class  App

	class  CipherMessage
	{
		public	byte[]	cipherBytes;	//	RC2	 encrypted	message	 text
		public	byte[]	rc2Key;				 //	 RSA  encrypted	 rc2  key
		public	byte[]	rc2IV;				  //  RC2  initialization  vector
	}

	class  Person
	{
		private	 RSACryptoServiceProvider  rsa;
		private	 RC2CryptoServiceProvider  rc2;
		private	 string	 name;

		//	Maximum	 key  size	for	 the  RC2  algorithm
		const  int	keySize	 =	128;

		//	Person	constructor
		public	Person(string  p_Name)
		{
			rsa	 =	new	 RSACryptoServiceProvider();
			rc2	 =	new	 RC2CryptoServiceProvider();
			rc2.KeySize	 =	keySize;
			name  =	 p_Name;
		}

		//	Used  to  send	the	 rsa  public  key  parameters
		public	RSAParameters  SendPublicKey()
		{
			RSAParameters  result  =  new  RSAParameters();
			try
			{
				result	=  rsa.ExportParameters(false);
			}
			catch  (CryptographicException	e)
			{
				Console.WriteLine(e.Message);
			}
			return	result;
		}

		//	Used  to  import  the  rsa	public	key	 parameters
		public	void  GetPublicKey(Person  receiver)
		{
			try
			{
				rsa.ImportParameters(receiver.SendPublicKey());
			}
			catch  (CryptographicException	e)
			{
				Console.WriteLine(e.Message);
			}
		}

		public	CipherMessage  EncryptMessage(string  text)
		{
			//	Convert	 string	 to	 a	byte  array
			CipherMessage  message	=  new	CipherMessage();
			byte[]	plainBytes	=  Encoding.Unicode.GetBytes(text.ToCharArray());

			//	A  new	key	 and  iv  are  generated  for  every  message
			rc2.GenerateKey();
			rc2.GenerateIV();

			//	The	 rc2  initialization  doesnt  need	to	be	encrypted,	but	 will
			//	be	used  in  conjunction  with	 the  key  to  decrypt	the	 message.
			message.rc2IV  =  rc2.IV;
			try
			{
				//	Encrypt	 the  RC2  key	using  RSA	encryption
				message.rc2Key	=  rsa.Encrypt(rc2.Key,	 false);
			}
			catch  (CryptographicException	e)
			{
				//	The	 High  Encryption  Pack	 is	 required  to  run	this	sample
				//	because	 we	 are  using	 a	128-bit	 key.  See	the	 readme	 for
				//	additional	information.
				Console.WriteLine("Encryption  Failed.  Ensure  that  the"  +
					"  High  Encryption  Pack  is  installed.");
				Console.WriteLine("Error  Message:  "  +  e.Message);
				Environment.Exit(0);
			}
			//	Encrypt	 the  Text	Message	 using	RC2	 (Symmetric	 algorithm)
			ICryptoTransform  sse  =  rc2.CreateEncryptor();
			MemoryStream  ms  =	 new  MemoryStream();
			CryptoStream  cs  =	 new  CryptoStream(ms,	sse,  CryptoStreamMode.Write);
			try
			{
					cs.Write(plainBytes,  0,  plainBytes.Length);
					cs.FlushFinalBlock();
					message.cipherBytes	 =	ms.ToArray();
			}
			catch  (Exception  e)
			{
					Console.WriteLine(e.Message);
			}
			finally
			{
				ms.Close();
				cs.Close();
			}
			return	message;
		}  //  method  EncryptMessage


		public	void  DecryptMessage(CipherMessage	message)
		{
			//	Get	 the  RC2  Key	and	 Initialization	 Vector
			rc2.IV	=  message.rc2IV;
			try
			{
				//	Try	 decrypting	 the  rc2  key
				rc2.Key	 =	rsa.Decrypt(message.rc2Key,	 false);
			}
			catch  (CryptographicException	e)
			{
				Console.WriteLine("Decryption  Failed:  "  +  e.Message);
				return;
			}

			ICryptoTransform  ssd  =  rc2.CreateDecryptor();
			//	Put	 the  encrypted	 message  in  a	 memorystream
			MemoryStream  ms  =	 new  MemoryStream(message.cipherBytes);
			//	the	 CryptoStream  will	 read  cipher  text	 from  the	MemoryStream
			CryptoStream  cs  =	 new  CryptoStream(ms,	ssd,  CryptoStreamMode.Read);
			byte[]	initialText	 =	new	 Byte[message.cipherBytes.Length];

			try
			{
					//	Decrypt	 the  message  and	store  in  byte	 array
					cs.Read(initialText,  0,  initialText.Length);
			}
			catch  (Exception  e)
			{
					Console.WriteLine(e.Message);
			}
			finally
			{
				ms.Close();
				cs.Close();
			}

			//	Display	 the  message  received
			Console.WriteLine(name	+  "  received  the  following  message:");
			Console.WriteLine("    "  +  Encoding.Unicode.GetString(initialText));
		}  //  method  DecryptMessage
	}  //  class  Person
}  //  namespace  PublicKey

赵4老师 2014-03-28
  • 打赏
  • 举报
回复
参考《算法精解C语言实现》例子代码\examples_pc\source\des.c ?
N_Sev7 2014-03-28
  • 打赏
  • 举报
回复
引用 楼主 qiaomu22 的回复:
各位大神 小弟在做一个文件传输项目 对方系统用的是c#写的des加解密算法 密钥:Bank2014 加密偏移量:Bank2014 加密模式:CipherMode.CBC、PaddingMode.PKCS5 块长度:64 文件编码:GBK 小弟这边用的是c写的加解密算法我就只设置了密钥:Bank2014 代码是参照大赛写的 位置在:http://www.iteye.com/topic/478024 目前的问题就是对方公司加密出来的文件跟我加密出来的文件不一样 我的代码是否需要设置加密偏移量呢 还是代码本身就有问题 求大神指点 先跪谢了
没接触过相关知识, 仅有的记忆是大学的网络安全课上 老师讲过的RSA加密算法,而且 也已经忘记了,爱莫能助
乔木3124343 2014-03-27
  • 打赏
  • 举报
回复
引用 4 楼 ak47_wz 的回复:
[quote=引用 3 楼 qiaomu22 的回复:] [quote=引用 2 楼 ak47_wz 的回复:] C#有这种内置加解密算法。 不过C语言没做过。不知道这种问题是不是因为字符编码的问题,中文有好多种编码,也是烦的不行。帮你顶顶~~
哎 不懂这原理就是悲催啊 谢谢哈[/quote] 毕竟不是信息安全专业~~可以理解。 不过我记得密码学上面有介绍这种算法,是经过什么什么列变化,什么什么做的。可以找个学安全的人问问就懂了~。[/quote] 行 我再去找找他们看有没懂的
水平不流 2014-03-27
  • 打赏
  • 举报
回复
引用 3 楼 qiaomu22 的回复:
[quote=引用 2 楼 ak47_wz 的回复:] C#有这种内置加解密算法。 不过C语言没做过。不知道这种问题是不是因为字符编码的问题,中文有好多种编码,也是烦的不行。帮你顶顶~~
哎 不懂这原理就是悲催啊 谢谢哈[/quote] 毕竟不是信息安全专业~~可以理解。 不过我记得密码学上面有介绍这种算法,是经过什么什么列变化,什么什么做的。可以找个学安全的人问问就懂了~。
乔木3124343 2014-03-27
  • 打赏
  • 举报
回复
引用 2 楼 ak47_wz 的回复:
C#有这种内置加解密算法。 不过C语言没做过。不知道这种问题是不是因为字符编码的问题,中文有好多种编码,也是烦的不行。帮你顶顶~~
哎 不懂这原理就是悲催啊 谢谢哈
水平不流 2014-03-27
  • 打赏
  • 举报
回复
C#有这种内置加解密算法。 不过C语言没做过。不知道这种问题是不是因为字符编码的问题,中文有好多种编码,也是烦的不行。帮你顶顶~~
乔木3124343 2014-03-27
  • 打赏
  • 举报
回复
DES_Encrypt("1.txt","Bank2014","2.txt"); 小弟是这样调用的
实例简介】autojs例子大全,一千六百多个脚本,简单的到复杂的例子,统统有,小白学完马上变大神大神学了变超神。 脚本内容包含: 几十种类型的UI脚本,抖音、QQ、微信、陌陌、支付宝等自动化操作的脚本、还有部分协议列表,HTTP协议(POST、GET)上传下载,接码模块,百度文字识别api模块,文件操作模块:txt文本读一行删一行,等等其他例子 【实例截图】 【核心代码】 └─1688 !运动点赞!.js (qq语音红包.js (协议)快阅读.js (可修改王者荣耀启动动画)视频播放器(1).js (实?).js (小瓜)九州行(720x1440)多账号游戏辅助.js -控件集合.js -控件集合2.js 0(1).js 0(2).js 0.js 00-仿真曲线滑动2.js 00-关闭指定应用-通用版.js 00-本地时间及网络时间验证改版.js 00-正则匹配关闭应用-适用大部分手机(1).js 00-正则匹配关闭应用-适用大部分手机.js 00-简化点击控件.js 00-结束事件与结束应用(1).js 00-结束事件与结束应用.js 00-读&删指定文本行.js 00-读取txt文本每一行&去空格.js 00-通知相册.js 001-Hello JS.js 002-if条件.js 003-循环break.js 004-循环for.js 005-循环while.js 0根据图色点击.js 0计分器.js 1(1).js 1.js 1024下载.js 11.js 111.js 11111111111.js 12.js 1233.js 1543275531466-mysl.js 18禁 小撸怡情,大撸伤身.js 190620_计算器.js 1gps码表.js 1别踩白块.js 1当前页面所有文字内容.js 1怎样动态增加text标签.js 1截图脚本.js 1提取QQ收藏完整内容.js 1改变字体颜色大小和内容.js 1易码获取短信.js 1查询本机IP地理位置.js 1比1比4悬浮窗可限制显示行数.js 1交集.js 1爬取bilibili视频弹幕.js 1箭头函数和function的this对比.js 1鸣人分身.js 2.0示例脚本合集.js 2.js 2.离线文字转语音~发声器.js 2018-05-16.js 2018年刑侦科推理题.js 2019-10-13蚂蚁森林.js 2048全自动(1).js 2048全自动.js 2048游戏机(1).js 2048游戏机(2).js 2048游戏机(3).js 2048游戏机(4).js 2048游戏机.js 2048游戏机UI版.js 2048玲珑棋局.js 2与 y960对比颜色找顶点.js 3d视角.js 6.0start(1).js 6.0start(2).js 6.0start(3).js 6.0start.js 643个城市数组.js 6(0.51).js 8.0打开关闭网络usb共享.js 9420-麦小兜(1).js 99乘法表.js a5main.js activity.js AD790179-8D8A-4CC6-BF68-25D58C7FD745.js adb5037常用命令.js aes加解密000.js AES加解密demo.js after work.js AJ-网易云签到.js AJ_api_search(1).js AJ_api_search.js aj内置文件管理.js AJ功能搜索(1).js AJ功能搜索.js aj数据文件管理.js Alipay_collect_energy_Optimize.js Animal.js Ant_forest.js apk打包成js格式.js(1).js apk打包成js格式.js.js app启动停止和输入(2)(1).js app启动停止和输入(2).js Auto.JS悬浮按钮(第二版).js Auto.JS悬浮按钮大柒(第二版).js Auto.js悬浮知识点.js Auto.js教程浏览器.js auto.js无root取短信.js autoGetCatCoin.js autojs常用函数(1).js autojs开启无障碍.js Autojs悬浮按钮(全版本可用).js Autojs悬浮按钮.js autojs爬取百度贴吧.js AvlTree(平衡树).js AvlTree.js badapple.js Base64(1).js base64.js Base64_Codec.js base64和des.js bmob上传文件.js bmob用户表的增删改查.js bmob用户验证demo.js bmob示例-对象的增删改查.js caiji.js Calender.js canvas画正方形.js ceshi.js click控件获取坐标位置.js ColorMatrix处理图片↘颜色矩阵.js ColorMatrix颜色矩阵的用法.js common(1).js common.js config.js DailyTask.js DensityUtil.js didi.js digit.js dou_yin_yang_hao(1).js dou_yin_yang_hao.js download.js drawBitMap之平移佐罗.js dy.js dy抖音评论.js en.js enable.js exec的实践.js fab.js fenduan.js Fermat素性测试.js file_chooser_dialog.js FlashPictureGet(1).js Fuck加密机(不支持注释不支持双斜杠不支持ui).js funnyEncoderPlus2.0.js gestures动作数据生成.js getPixels参数详解.js gitee-webhook.js git常用命令.js gpsui.js gps时间获取.js HAHA小视频无限撸money(无解说).js hello语音刷房脚本(雷电).js hl4a.js HTTP请.js http超时返回null.js ID3MusicDealer.js installInfo.js intent安装apk.js Intent生成器(2).js Intent生成器(3).js Intent生成器.js ipv6聊天室.js ipv6聊天室2.0.js.js ip地址查询.js ip地址查询UI.js java.js java代码改成autojs过程.js JD_Captcha_Test.js jimu.js jm_plp.js jquery1.js jsdroid.js json格式化.js jsoupDemo.js JS代码加密例子.js js加密器.js js块级作用域.js js查询.js js进阶教程02.js js进阶教程03.js juexin_Qtiao.js LayoutInflater.js leimu.js listView勾选框丢了怎么解决.js list隔行变色.js long.js long2.js ls_socket.js lunar.js LZ动作.js main(1)(1).js main(1)(2).js main(1)(3).js main(1)(4).js main(1).js main(10).js Main(11).js main(12).js main(13).js main(14).js main(2)(1).js main(2).js main(3).js main(5).js main(6).js main(7).js main(9).js main.js match的实践.js md5(1).js

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧