!Mark! 100分求C#加解密字符串的方法(速度快为主)

bzhi268 2006-10-26 11:51:19
如题!!!
...全文
268 10 打赏 收藏 举报
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
viena 2006-10-26
  • 打赏
  • 举报
回复
就是初始化字典比较费时,可以事先定义好~
viena 2006-10-26
  • 打赏
  • 举报
回复
自定义加密最快,用自定义字典(字符串数组,字符编码为索引)查表转换即可
wings0201 2006-10-26
  • 打赏
  • 举报
回复
用 triple des 就不错
CH_MARS 2006-10-26
  • 打赏
  • 举报
回复
/// <summary>
/// 使用DES解密
/// </summary>
/// <param name="encryptedValue">待解密的字符串</param>
/// <param name="key">密钥(最大长度8)</param>
/// <returns>解密后的字符串</returns>
public string DESDecrypt(string encryptedValue,string key)
{
return DESDecrypt(encryptedValue,key,key);
}

/// <summary>
/// 将二进制数组转为字符串
/// </summary>
/// <param name="Byte">二进制数组</param>
/// <returns></returns>
private string GetStringValue(byte[] Byte)
{
string tmpString = "";

if (this.isReturnNum == false)
{
ASCIIEncoding Asc = new ASCIIEncoding();
tmpString = Asc.GetString(Byte);
}
else
{
int iCounter;

for(iCounter=0;iCounter<Byte.Length;iCounter++)
{
tmpString = tmpString + Byte[iCounter].ToString();
}

}

return tmpString;
}

/// <summary>
/// 返回字符串的二进制数组
/// </summary>
/// <param name="strKey">字符串</param>
/// <returns></returns>
private byte[] GetKeyByteArray(string strKey)
{

ASCIIEncoding Asc = new ASCIIEncoding();

int tmpStrLen = strKey.Length;

byte[] tmpByte = new byte[tmpStrLen-1];

tmpByte = Asc.GetBytes(strKey);

return tmpByte;

}

}


}

程序中这样调用:
public static readonly string ConnectionStringLocalTransaction =new HashEncrypt(true,false).DESDecrypt( ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString,"web","web");
CH_MARS 2006-10-26
  • 打赏
  • 举报
回复
/// <summary>
/// 使用DES加密
/// </summary>
/// <param name="originalValue">待加密的字符串</param>
/// <param name="key">密钥(最大长度8)</param>
/// <param name="IV">初始化向量(最大长度8)</param>
/// <returns>加密后的字符串</returns>
public string DESEncrypt(string originalValue,string key,string IV)
{
//将key和IV处理成8个字符
key += "12345678";
IV += "12345678";
key = key.Substring(0,8);
IV = IV.Substring(0,8);

SymmetricAlgorithm sa;
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;

sa = new DESCryptoServiceProvider();
sa.Key = Encoding.UTF8.GetBytes(key);
sa.IV = Encoding.UTF8.GetBytes(IV);
ct = sa.CreateEncryptor();

byt = Encoding.UTF8.GetBytes(originalValue);

ms = new MemoryStream();
cs = new CryptoStream(ms, ct,CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();

cs.Close();

return Convert.ToBase64String(ms.ToArray());

}

/// <summary>
/// 使用DES加密
/// </summary>
/// <param name="originalValue">待加密的字符串</param>
/// <param name="key">密钥(最大长度8)</param>
/// <returns>加密后的字符串</returns>
public string DESEncrypt(string originalValue,string key)
{
return DESEncrypt(originalValue,key,key);
}
aafshzj 2006-10-26
  • 打赏
  • 举报
回复
常用的DES/MD5/RAS都挺快的啊。你要快到什么程度?够用就好。
zhangzengping 2006-10-26
  • 打赏
  • 举报
回复
和你的名字 异或,
idda 2006-10-26
  • 打赏
  • 举报
回复
不对称加密不适合用来加密大量数据, 但是小量数据还是不对称加密好
idda 2006-10-26
  • 打赏
  • 举报
回复
对称加密速度比不对称加密要快

/// <summary>
/// 获得密钥
/// </summary>
/// <returns>密钥</returns>
private byte[] GetLegalKey()
{
string sTemp = Key;
mobjCryptoService.GenerateKey();
byte[] bytTemp = mobjCryptoService.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// 获得初始向量IV
/// </summary>
/// <returns>初试向量IV</returns>
private byte[] GetLegalIV()
{
string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
mobjCryptoService.GenerateIV();
byte[] bytTemp = mobjCryptoService.IV;
int IVLength = bytTemp.Length;
if (sTemp.Length > IVLength)
sTemp = sTemp.Substring(0, IVLength);
else if (sTemp.Length < IVLength)
sTemp = sTemp.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// 加密方法
/// </summary>
/// <param name="Source">待加密的串</param>
/// <returns>经过加密的串</returns>
public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
}
/// <summary>
/// 解密方法
/// </summary>
/// <param name="Source">待解密的串</param>
/// <returns>经过解密的串</returns>
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
}
}
江海汇 2006-10-26
  • 打赏
  • 举报
回复
mark
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2006-10-26 11:51
社区公告

让您成为最强悍的C#开发者