C#,目前最好的字符串加密和解密的算法是什么?

 我来答
ThinkIBM
推荐于2016-08-14 · TA获得超过5289个赞
知道大有可为答主
回答量:3263
采纳率:0%
帮助的人:5476万
展开全部
无所谓什么最好不好,都有一定的局限,你可以加上一些自己的处理,就会比较好了,呵呵,附上我自己用的加密解密方法:
/// <summary>
/// 使用DES加密指定字符串
/// </summary>
/// <param name="encryptStr">待加密的字符串</param>
/// <param name="key">密钥(最大长度8)</param>
/// <param name="IV">初始化向量(最大长度8)</param>
/// <returns>加密后的字符串</returns>
public static string DESEncrypt(string encryptStr,string key,string IV)
{
//将key和IV处理成8个字符
key += "12345678";
IV += "12345678";
key = key.Substring(0,8);
IV = IV.Substring(0,8);

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

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

byt = Encoding.UTF8.GetBytes(encryptStr);

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

cs.Close();

//加上一些干扰字符
string retVal = Convert.ToBase64String(ms.ToArray());
System.Random ra = new Random();

for (int i=0;i<8;i++)
{
int radNum = ra.Next(36);
char radChr = Convert.ToChar(radNum + 65);//生成一个随机字符

retVal = retVal.Substring(0,2*i+1) + radChr.ToString() + retVal.Substring(2*i+1);
}

return retVal;
}

/// <summary>
/// 使用DES解密指定字符串
/// </summary>
/// <param name="encryptedValue">待解密的字符串</param>
/// <param name="key">密钥(最大长度8)</param>
/// <param name="IV">初始化向量(最大长度8)</param>
/// <returns>解密后的字符串</returns>
public static string DESDecrypt(string encryptedValue,string key,string IV)
{
//去掉干扰字符
string tmp = encryptedValue;
if (tmp.Length < 16)
{
return "";
}

for (int i=0;i<8;i++)
{
tmp = tmp.Substring(0,i+1) + tmp.Substring(i+2);
}
encryptedValue = tmp;

//将key和IV处理成8个字符
key += "12345678";
IV += "12345678";
key = key.Substring(0,8);
IV = IV.Substring(0,8);

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

try
{
sa = new DESCryptoServiceProvider();
sa.Key = Encoding.UTF8.GetBytes(key);
sa.IV = Encoding.UTF8.GetBytes(IV);
ict = sa.CreateDecryptor();

byt = Convert.FromBase64String(encryptedValue);

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

cs.Close();

return Encoding.UTF8.GetString(ms.ToArray());
}
catch (System.Exception)
{
return "";
}

}
user830316
2007-09-21 · TA获得超过1390个赞
知道大有可为答主
回答量:1371
采纳率:0%
帮助的人:1540万
展开全部
//加密字符串函数
static public string Encryption(string str,Random R)
{
/* 加密算法 最长可加密255个字符 */
string md1, md2, pwd = "";
string[] str_t = new string[255];
int l1, l2;
for (int i = 0; i < str.Length; i++)
{
l1 = R.Next(0, 256);
md1 = l1.ToString("X");
if (md1.Length == 1) { md1 = "0" + md1; }

str_t[i] = str.Substring(i, 1);
System.Text.ASCIIEncoding AsciiEncoding = new System.Text.ASCIIEncoding();
l2 = (int)AsciiEncoding.GetBytes(str_t[i])[0];
md2 = (l1 ^ l2).ToString("X");
if (md2.Length == 1) { md2 = "0" + md2; }
pwd = pwd + md1 + md2;
}
return pwd;
}

//解密字符串函数
static public string Decrypt(string str)
{
/* 解密算法 */
string pwd = "";
int xl;
if (str.Length % 4 == 0)
{
xl = str.Length / 4;
string md1, md2;
int l1, l2;
for (int i = 0; i < xl; i++)
{
md1 = str.Substring(i * 4, 2);
md2 = str.Substring(i * 4 + 2, 2);
l1 = int.Parse(md1, System.Globalization.NumberStyles.AllowHexSpecifier);
l2 = int.Parse(md2, System.Globalization.NumberStyles.AllowHexSpecifier);
System.Text.ASCIIEncoding AsciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)(l1 ^ l2) };
md1 = AsciiEncoding.GetString(byteArray);
pwd = pwd + md1;
}
}
return pwd;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
三根鞋带
2007-09-21 · TA获得超过2260个赞
知道大有可为答主
回答量:1156
采纳率:0%
帮助的人:0
展开全部
//加密
String str = "this is a test.";
byte[] bytes = Encoding.ASCII.GetBytes(str);

DSACryptoServiceProvider dsac = new DSACryptoServiceProvider();
byte[] sign = dsac.SignData(bytes);
Response.Write(Convert.ToBase64String(sign));

//验证
DSACryptoServiceProvider dsac2 = new DSACryptoServiceProvider();
dsac2.FromXmlString(dsac.ToXmlString(false));
bool ver = dsac2.VerifyData(bytes, sign);
if (ver)
{
Console.WriteLine("通过"+Convert.ToBase64String(sign));
}
else
{
Console.WriteLine("不能通过");
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式