求一段C#简单的字符串加密与解密代码

简单容懂的加分... 简单容懂的加分 展开
 我来答
luolujun1008
2009-11-11 · TA获得超过483个赞
知道答主
回答量:85
采纳率:0%
帮助的人:66万
展开全部
using System.Security.Cryptography;
using System.Text;

string get6(string 要加密的文字或者要解密的文字outXMLFilePath, string jia)
{
byte[] Keys ={ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
if (jia.Equals("加密"))
{
byte[] rgbKey ={ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = Encoding.UTF8.GetBytes(outXMLFilePath);
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());
}
else
{
byte[] rgbKey ={ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = Convert.FromBase64String(outXMLFilePath);
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());

}
}
finalyfantasy
2009-11-10 · TA获得超过1364个赞
知道小有建树答主
回答量:1631
采纳率:0%
帮助的人:573万
展开全部
public static string Encrypt(string data, string encryptKey)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(encryptKey);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(encryptKey);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
}
public static string Decrypt(string data, string encryptKey)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(encryptKey);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(encryptKey); byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return data;
} DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
bxfc
2009-11-09 · TA获得超过872个赞
知道小有建树答主
回答量:1104
采纳率:0%
帮助的人:674万
展开全部
加密:
byte[] bytes=Encoding.Default.GetBytes("要转换的字符串");
Convert.ToBase64String(bytes);
解密:、
byte[] outputb = Convert.FromBase64String("ztKwrsTj");
string orgStr= Encoding.Default.GetString(outputb);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式