一个简单的加密/解密类

public class Encryption
{
    public string PrimaryKey;
    public string SecondaryKey;
    public string NewKey;

    public Encryption()
    {
    }

    public string Decrypt(string TextToEncrypt)
    {
        MemoryStream memoryStream1 = new MemoryStream();
        memoryStream1.Write(Convert.FromBase64String(TextToEncrypt), 0, Convert.FromBase64String(TextToEncrypt).Length);
        memoryStream1.Position = 0;

        MemoryStream memoryStream2 = new MemoryStream();
        byte[] bs = new byte[2049];
        SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithm.Create("Rijndael");
        symmetricAlgorithm.IV = Encoding.Default.GetBytes(SecondaryKey);
        symmetricAlgorithm.Key = Getkey();

        ICryptoTransform iCryptoTransform = symmetricAlgorithm.CreateDecryptor();
        CryptoStream cryptoStream = new CryptoStream(memoryStream1, iCryptoTransform, CryptoStreamMode.Read);

        for (int i = cryptoStream.Read(bs, 0, bs.Length); i > 0; i = cryptoStream.Read(bs, 0, bs.Length))
        {
            memoryStream2.Write(bs, 0, i);
        }
        string str = Encoding.Default.GetString(memoryStream2.ToArray());

        memoryStream1.Close();
        memoryStream2.Close();
        return str;
    }

    public byte[] Getkey()
    {
        if (Strings.Len(PrimaryKey) >= 16)
        {
            NewKey = Strings.Left(PrimaryKey, 16);
        }
        else
        {
            NewKey = PrimaryKey;
            for (int i = Strings.Len(PrimaryKey) + 1; i <= 16; i++)
            {
                NewKey = String.Concat(NewKey, "0");
            }
        }
        byte[] bs = Encoding.Default.GetBytes(NewKey);
        return bs;
    }

    public string Encrypt(string TextToEncrypt)
    {
        MemoryStream memoryStream1 = new MemoryStream();
        memoryStream1.Write(Encoding.Default.GetBytes(TextToEncrypt), 0, Encoding.Default.GetBytes(TextToEncrypt).Length);
        memoryStream1.Position = 0;

        MemoryStream memoryStream2 = new MemoryStream();
        byte[] bs = new byte[2049];
        SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithm.Create("Rijndael");
        symmetricAlgorithm.IV = Encoding.Default.GetBytes(SecondaryKey);
        symmetricAlgorithm.Key = Getkey();

        ICryptoTransform iCryptoTransform = symmetricAlgorithm.CreateEncryptor();
        CryptoStream cryptoStream = new CryptoStream(memoryStream2, iCryptoTransform, CryptoStreamMode.Write);

        for (int i = memoryStream1.Read(bs, 0, bs.Length); i > 0; i = memoryStream1.Read(bs, 0, bs.Length))
        {
            cryptoStream.Write(bs, 0, i);
        }
        cryptoStream.FlushFinalBlock();

        string str = Convert.ToBase64String(memoryStream2.ToArray());
        cryptoStream.Close();
        memoryStream1.Close();
        return str;
    }
}
Contributors: FHL