I tried to create encrypt decrypt function on my C# project
But somehow it got issue when the text need to be encrypted is more than 16 characters
The decrypted text is missing characters at the end
If I use shorter plainText like "abcde" it can decrypt the text with no problem
But if the plainText is long like on the example, some of the last character is gone
Need your help to check this code
Thanks
program.cs
string plainText = "abcdefghijklmnopqrstuvwhyz1234567890";
string password = Convert.ToBase64String(Cryptography.GenerateRandomEntropy());
string encryptedText = Cryptography.Encrypt(plainText, password);
string decryptedText = Cryptography.Decrypt(encryptedText, password);
Console.WriteLine("Text: " + plainText); //abcdefghijklmnopqrstuvwhyz1234567890
Console.WriteLine("Pass: " + password);
Console.WriteLine("Enc: " + encryptedText);
Console.WriteLine("Dec: " + decryptedText); //abcdefghijklmnopqrstuvwhyz123456
crypt.cs
using System.Text;
using System.Security.Cryptography;
public static class Cryptography
{
private const int Keysize = 128;
private const int DerivationIterations = 1000;
public static string Encrypt(string plainText, string passPhrase)
{
var saltStringBytes = GenerateRandomEntropy();
var ivStringBytes = GenerateRandomEntropy();
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
{
var keyBytes = password.GetBytes(Keysize / 8);
using (var symmetricKey = Aes.Create())
{
symmetricKey.BlockSize = Keysize;
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
using (var key = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
{
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, key, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes);
cryptoStream.FlushFinalBlock();
var cryptTextBytes = saltStringBytes;
cryptTextBytes = cryptTextBytes.Concat(ivStringBytes).ToArray();
cryptTextBytes = cryptTextBytes.Concat(memoryStream.ToArray()).ToArray();
memoryStream.Close();
cryptoStream.Close();
return Convert.ToBase64String(cryptTextBytes);
}
}
}
}
}
}
public static string Decrypt(string cryptText, string passPhrase)
{
var cryptTextBytesWithSaltAndIv = Convert.FromBase64String(cryptText);
var saltStringBytes = cryptTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
var ivStringBytes = cryptTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
var cryptTextBytes = cryptTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cryptTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
{
var keyBytes = password.GetBytes(Keysize / 8);
using (var symmetricKey = Aes.Create())
{
symmetricKey.BlockSize = Keysize;
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
using (var key = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
{
using (var memoryStream = new MemoryStream(cryptTextBytes))
{
using (var cryptoStream = new CryptoStream(memoryStream, key, CryptoStreamMode.Read))
{
var plainTextBytes = new byte[cryptTextBytes.Length];
cryptoStream.Read(plainTextBytes);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes);
}
}
}
}
}
}
public static byte[] GenerateRandomEntropy()
{
var randomBytes = new byte[Keysize / 8];
using (var rngCsp = RandomNumberGenerator.Create())
{
rngCsp.GetBytes(randomBytes);
}
return randomBytes;
}
}