原理
加密:使用密钥(2进制)对2进制数据异或运算 解密:使用密钥对加密的二进制数据进行异或运算 例如:
- 加密
对数字1(0001)加密 密钥是2(0010) 异或运算 同0非1 0001 0010 异或 得到加密数据 0011 - 解密:
0011 0010 异或 得到解密数据 0001
实例
Student student = new Student();
byte key = 66;
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter binary = new BinaryFormatter();
binary.Serialize(ms, student);
byte[] bs = ms.GetBuffer();
for (int i = 0; i < bs.Length; i++)
{
bs[i] ^= key;
}
File.WriteAllBytes(Application.dataPath + "/safe.data", bs);
ms.Flush();
ms.Close();
}
byte[] b2 = File.ReadAllBytes(Application.dataPath + "/safe.data");
for (int i = 0; i < b2.Length; i++)
{
b2[i] ^= key;
}
using (MemoryStream ms = new MemoryStream(b2))
{
BinaryFormatter binary = new BinaryFormatter();
Student sss = binary.Deserialize(ms) as Student;
ms.Close();
}
|