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);
}
char
in C# is 16 bits. – Segment