9

I've written an encryption routine built from several methods on the net. I'm currently testing the encryption and have noticed that every encrypted value end in '==' ?

Does anyone know why this might be the case ?

This is the code I'm using. The key is a 32 char value and the IV is a 16 char value.

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 returns an AESManaged class as below:

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

Currently the test method looks like this:

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);
                }
2
  • 2
    If you have two questions then make two questions. You are asking two completely different questions in this one question. Most people will answer only one of them, and then you'll have a problem trying to choose which answer to accept. My advice is that you delete the second question from this question and start a new question. Jun 19, 2013 at 15:54
  • 1
    Also, char in C# is 16 bits. Jun 19, 2013 at 15:58

1 Answer 1

15

That is standard for base 64 encoding. Read the "padding" section of the Wikipedia article for details.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.