关于C#中使用DES加解密问题
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
private static 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;
}
}
private static 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(Exception ex)
{
return decryptString;
}
}
以上是我使用的对于字符串加解密的方法,以前使用都是OK的,但是现在使用加密时在 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
就有错误了,但是还可以继续,可以返回加密后的字符串,但是等到解密时,系统会Catch到Exception,也是在 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);时就有错误了,错误信息是
+Length“cStream.Length”引发了“System.NotSupportedException”类型的异常long {System.NotSupportedException}
,,,+Position“cStream.Position”引发了“System.NotSupportedException”类型的异常long {System.NotSupportedException}
然后要运行到cStream.FlushFinalBlock();才会跳到异常捕获代码。异常信息为“不正确的数据”,ex.StackTrace为:
在 System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
在 System.Security.Cryptography.Utils._DecryptData(SafeKeyHandle hKey, Byte[] data, Int32 ib, Int32 cb, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode PaddingMode, Boolean fDone)
在 System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
在 System.Security.Cryptography.CryptoStream.FlushFinalBlock()
在 KM.Web.Utility.CryptBase.DecryptDES(String decryptString, String decryptKey) 位置 d:\KM3.0\03_Code\01_SourceCode\KM3.0\KM3.0\KMWeb\App_Code\CryptBase.cs:行号 57
请各位牛人帮忙解决,谢谢!
[解决办法]
友情帮顶
[解决办法]
记得C#里边的DES加密只能用ASC,用别的报错.
[解决办法]
用C#实现DES加密解密
using System;using System.Security.Cryptography;using System.Text;using System.IO;namespace Common...{ /**//// <summary> /// DESEncrypt加密解密算法。 /// </summary> public sealed class DESEncrypt ...{ private DESEncrypt() ...{ // // TODO: 在此处添加构造函数逻辑 // } private static string key = "zhoufoxcn"; /**//// <summary> /// 对称加密解密的密钥 /// </summary> public static string Key ...{ get ...{ return key; } set ...{ key = value; } } /**//// <summary> /// DES加密 /// </summary> /// <param name="encryptString"></param> /// <returns></returns> public static string DesEncrypt(string encryptString) ...{ byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8)); byte[] keyIV = keyBytes; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } /**//// <summary> /// DES解密 /// </summary> /// <param name="decryptString"></param> /// <returns></returns> public static string DesDecrypt(string decryptString) ...{ byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8)); byte[] keyIV = keyBytes; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } }}
[解决办法]
早先我写的,习惯不太好,几乎没有注释。
[解决办法]
加密与解密只要有加密就够用了
你将得到的字符串进行加密然后比较两串加密的字符串就可以。
在我的空间里有MD5,DES等加解密的案例
[解决办法]
我测试了一下你的代码.很正常
public class Program { private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; static void Main(string[] args) { string key = "123456789"; string str = "cc"; string str2 = EncryptDES(str, key); Console.WriteLine(str2); Console.WriteLine(DecryptDES(str2, key)); Console.ReadKey(); } private static 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; } } private static 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 (Exception ex) { return decryptString; } } }
[解决办法]
帮顶
[解决办法]
不过你的代码看着就怪.那两个方法都有一个Key了(第二个参数)
你还要一个private的Keys干啥?
代码我也没仔细看,感觉很奇怪.不过能测试通过
发两个方法,参考一下.我一直在用
#region TripleDES加/解密 /// <summary> /// TripleDES解密 /// </summary> public static string TripleDESDecrypt(string encryptedString,string key) { if(encryptedString.Equals(string.Empty)) { return encryptedString; } TripleDESCryptoServiceProvider tdes=new TripleDESCryptoServiceProvider();= 10)
[解决办法]
你看一下这篇文章下面的评论.
[解决办法]
byte[] rgbKey = Encoding.ASCII.GetBytes(encryptKey.Substring(0, 8));
都改成ASC的.
[解决办法]
用过加解密:
#region 对数据进行加密 /// <summary> /// 对数据进行加密 /// </summary> /// <param name="encryptstring">需要加密的数据</param> /// <returns></returns> public string DESEncrypt(string encryptstring) { string strRtn; try { DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密 byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey); byte[] data = System.Text.Encoding.Unicode.GetBytes(encryptstring); MemoryStream ms = new MemoryStream();//存储加密后的数据 CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write); cs.Write(data, 0, data.Length);//进行加密 cs.FlushFinalBlock(); strRtn = Convert.ToBase64String(ms.ToArray()); return strRtn; } catch (Exception ex) { MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); return null; } } #endregion #region 对数据进行解密 /// <summary> /// 对数据进行解密 /// </summary> /// <param name="decryptstring">需要解密的数据</param> /// <returns></returns> public string DESDecrypt(string decryptstring) { string strRtn; try { DESCryptoServiceProvider desc = new DESCryptoServiceProvider(); byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey); byte[] data = Convert.FromBase64String(decryptstring); MemoryStream ms = new MemoryStream();//存储解密后的数据 CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(key, key), CryptoStreamMode.Write); cs.Write(data, 0, data.Length);//解密数据 cs.FlushFinalBlock(); strRtn = System.Text.Encoding.Unicode.GetString(ms.ToArray()); return strRtn; } catch (Exception ex) { MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); return null; } } #endregion