AES managed encryption values always end in ==?
Asked Answered
A

1

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);
                }
Abloom answered 19/6, 2013 at 15:39 Comment(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.Segment
Also, char in C# is 16 bits.Segment
S
15

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

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

Segment answered 19/6, 2013 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.