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

解决方案 »

  1.   

    异或简单加密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;
      

  2.   

    字符串一般使用Base64加密。请看这里 http://dev.csdn.net/article/27/27056.shtm
      

  3.   

    用 DPAPI (Data Protection API) 对数据库 进行加密=========目前正在研究代码更多 加密的 只是 参见 我的博客[★]基于.NET 的 加密 解密 算法总结[一] 
    http://www.cnblogs.com/hjs1573/archive/2007/07/06/808808.html
    [★]基于.NET 的 加密 解密 算法总结[二] 
    http://www.cnblogs.com/hjs1573/archive/2007/07/06/808821.html
      

  4.   

    微软提供SecureString 来加密STRING,
    如果需要还可以参考加密部分。 
    自己的算法最好不用。
    那时学生的做法。