user-0 提问: DES加密不一致问题,求大神解决

回复:
user: user-1
content: 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

user: user-1
content: 参考《算法精解C语言实现》例子代码\examples_pc\source\des.c ？

user: user-2
content: 引用 楼主 qiaomu22 的回复:各位大神  小弟在做一个文件传输项目   对方系统用的是c#写的des加解密算法  密钥：Bank2014 加密偏移量：Bank2014 加密模式:CipherMode.CBC、PaddingMode.PKCS5 块长度:64 文件编码：GBK  小弟这边用的是c写的加解密算法我就只设置了密钥:Bank2014 代码是参照大赛写的 位置在:http://www.iteye.com/topic/478024 目前的问题就是对方公司加密出来的文件跟我加密出来的文件不一样  我的代码是否需要设置加密偏移量呢 还是代码本身就有问题   求大神指点 先跪谢了  没接触过相关知识， 仅有的记忆是大学的网络安全课上 老师讲过的RSA加密算法，而且 也已经忘记了，爱莫能助

user: user-0
content: 引用 4 楼 ak47_wz 的回复:[quote=引用 3 楼 qiaomu22 的回复:] [quote=引用 2 楼 ak47_wz 的回复:] C#有这种内置加解密算法。 不过C语言没做过。不知道这种问题是不是因为字符编码的问题，中文有好多种编码，也是烦的不行。帮你顶顶～～ 哎 不懂这原理就是悲催啊 谢谢哈[/quote] 毕竟不是信息安全专业～～可以理解。  不过我记得密码学上面有介绍这种算法，是经过什么什么列变化，什么什么做的。可以找个学安全的人问问就懂了～。[/quote] 行 我再去找找他们看有没懂的

user: user-3
content: 引用 3 楼 qiaomu22 的回复:[quote=引用 2 楼 ak47_wz 的回复:] C#有这种内置加解密算法。 不过C语言没做过。不知道这种问题是不是因为字符编码的问题，中文有好多种编码，也是烦的不行。帮你顶顶～～ 哎 不懂这原理就是悲催啊 谢谢哈[/quote] 毕竟不是信息安全专业～～可以理解。  不过我记得密码学上面有介绍这种算法，是经过什么什么列变化，什么什么做的。可以找个学安全的人问问就懂了～。

user: user-0
content: 引用 2 楼 ak47_wz 的回复:C#有这种内置加解密算法。 不过C语言没做过。不知道这种问题是不是因为字符编码的问题，中文有好多种编码，也是烦的不行。帮你顶顶～～ 哎 不懂这原理就是悲催啊 谢谢哈

user: user-3
content: C#有这种内置加解密算法。 不过C语言没做过。不知道这种问题是不是因为字符编码的问题，中文有好多种编码，也是烦的不行。帮你顶顶～～

user: user-0
content: DES_Encrypt("1.txt","Bank2014","2.txt"); 小弟是这样调用的