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请各位牛人帮忙解决,谢谢!
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请各位牛人帮忙解决,谢谢!
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;
}
}
}
你还要一个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();
//将秘钥编码成为二进制数组
PasswordDeriveBytes derive = new PasswordDeriveBytes(key,null);
byte[] tdesIV=new byte[8];
byte[] btaKey=derive.CryptDeriveKey("TripleDES","SHA1",0,tdesIV); //从字符串转换为字节组
byte[] btaCode=System.Convert.FromBase64String(encryptedString); tdes.Mode = CipherMode.ECB;
MemoryStream ms=new MemoryStream(btaCode); CryptoStream encStream = new CryptoStream(ms,tdes.CreateDecryptor(btaKey,tdesIV),CryptoStreamMode.Read);
StreamReader sr=new StreamReader(encStream,System.Text.Encoding.Unicode);
string strtmp=sr.ReadToEnd();
sr.Close();
encStream.Close(); return strtmp;
} /// <summary>
/// TripleDES加密
/// </summary>
public static string TripleDESEcrypt(string plainString,string key)
{
if(plainString.Equals(string.Empty))
{
return plainString;
} TripleDESCryptoServiceProvider tdes=new TripleDESCryptoServiceProvider();
//将秘钥编码成为二进制数组
PasswordDeriveBytes derive = new PasswordDeriveBytes(key,null);
byte[] tdesIV=new byte[8];
byte[] btaKey=derive.CryptDeriveKey("TripleDES","SHA1",0,tdesIV); //从字符串转换为字节组
byte[] btaCode=Encoding.Unicode.GetBytes(plainString); tdes.Mode = CipherMode.ECB;
MemoryStream ms=new MemoryStream();
CryptoStream encStream = new CryptoStream(ms,tdes.CreateEncryptor(btaKey,tdesIV),CryptoStreamMode.Write);
encStream.Write(btaCode, 0, btaCode.Length);
encStream.FlushFinalBlock();
encStream.Close(); //再转换为一个字符串
return System.Convert.ToBase64String(ms.ToArray());
}
#endregion
都改成ASC的.
/// <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