9

我已经编写了一个由网络上的几种方法构建的加密例程。我目前正在测试加密,并注意到每个加密值都以 '==' 结尾?

有谁知道为什么会这样?

这是我正在使用的代码。键是 32 个字符的值,IV 是 16 个字符的值。

private static readonly byte[] key = Encoding.Default.GetBytes(getKey());
    private static readonly byte[] iv = Encoding.Default.GetBytes("$ruVe4E!eM#kupuc");

    /// <summary>
    /// 
    /// </summary>
    /// <param name="val"></param>
    /// <returns></returns>
    public static string Encrypt(string val)
    {
        string result = string.Empty;

        var aes = getEncryptionType();

        using (MemoryStream msEncrypt = new MemoryStream())
        {
            using (ICryptoTransform encryptor = aes.CreateEncryptor(key, iv))
            {
                using(CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using(StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(val);
                    }
                }
            }
            result = Convert.ToBase64String(msEncrypt.ToArray());
        }
        aes.Clear();
        return result;
    }

getEncryptionType 返回一个 AESManaged 类,如下所示:

private static AesManaged getEncryptionType()
    {
        AesManaged aes = new AesManaged();
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;
        return aes;
    }

目前测试方法如下所示:

Random rnd = new Random();

                for (int i = 0; i < 50000; i++)
                {
                    int random = rnd.Next(1147483647, int.MaxValue);
                    Guid guid = dal.getToken(CryptoService.Encrypt(random.ToString()));

                    if (i % 100 == 0)
                        addLog(string.Format("{0} new values added", i.ToString()), LogType.Dialog);
                }
4

1 回答 1

15

这是 base 64 编码的标准。有关详细信息,请阅读 Wikipedia 文章的“填充”部分。

http://en.wikipedia.org/wiki/Base64

于 2013-06-19T15:52:15.317 回答