日期:2014-05-19  浏览次数:20821 次

字符串加密问题
如何用一段成C#程序对一段输入的字符串进行加密,比如依次加3。
同样的,如何在加密后又进行解密

------解决方案--------------------
异或简单加密

byte key = 126;
//假设异或密钥为126
string str = "一个字符串123abc ";
//此字符串逐字节异或后写入C:\1.txt
//C:\1.txt打开为乱码
byte[] buffer = Encoding.Unicode.GetBytes(str);
int count = buffer.Length;
for(int i=0;i <count;i++)
{
buffer[i] = (byte)(buffer[i] ^ key);
}
FileStream fsw = new FileStream( "C:\\2.txt ",FileMode.Create);
fsw.Write(buffer,0,count);
fsw.Close();

//从C:\1.txt读逐字节异或还原为字符串str2
FileStream fsr = new FileStream( "C:\\2.txt ",FileMode.Open);
int count2 = (int)fsr.Length;
byte[] buffer2 = new byte[count2];
fsr.Read(buffer2,0,count2);
for(int i=0;i <count2;i++)
{
buffer2[i] = (byte)(buffer2[i] ^ key);
}
fsr.Close();
string str2 = Encoding.Unicode.GetString(buffer2);//str2 一个字符串123abc


需要
using System.IO;
using System.Text;