Aspnet string encryption and decryption engine

I am trying to decrypt a string using a fairly standard algorithm.

public static string DecryptString(string cipherText)
{
    string keyString = string.Empty;

    // Check whether the environment variable exists.
    keyString = Environment.GetEnvironmentVariable("EncryptKey");

    if (keyString == null)
    {
        keyString = "E546C8DF278CD5931069B522E695D4F2";
    }

    var fullCipher = Convert.FromBase64String(cipherText);
    using (var aesAlg = Aes.Create())
    {
        byte[] iv = new byte[aesAlg.BlockSize / 8];
        var cipher = new byte[16];

        Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
        Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, iv.Length);
        var key = Encoding.UTF8.GetBytes(keyString);

        string result;
        using (var decryptor = aesAlg.CreateDecryptor(key, iv))
        using (var msDecrypt = new MemoryStream(cipher))
        using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
        using (var srDecrypt = new StreamReader(csDecrypt))
        {
            result = srDecrypt.ReadToEnd();
        }
        return result;
    }
}

I get an error message:

System.Security.Cryptography.CryptographicException: The specified fill mode is not valid for this algorithm.

I tried several ways like this

var iv = new byte[16];
var cipher = new byte[16];

or

var iv = aesAlg.IV;

I am still getting an error at this point. What am I doing wrong?

+9
source share
3 answers

Two changes required

var cipher = new byte[fullCipher.Length - iv.Length];

and

Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, fullCipher.Length - iv.Length);
0
source
public static string Decrypt(string cipherText)
  {
     string EncryptionKey  = string.Empty;

    // Check whether the environment variable exists.
    EncryptionKey = Environment.GetEnvironmentVariable("EncryptKey");

    if (EncryptionKey == null)
    {
        EncryptionKey = "E546C8DF278CD5931069B522E695D4F2";
    }
            byte[] cipherBytes;
            try
            {
                cipherBytes = Convert.FromBase64String(cipherText);
            }
            catch
            {
                return cipherText;
            }
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    cipherText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return cipherText;
        }

Note: - You need to provide the same key that you specified to encrypt the string.

-1
source

keyString.

1: keyString

keyString = Environment.GetEnvironmentVariable("EncryptKey");

, keyString , "E546C8DF278CD5931069B522E695D4F G", " ."

: keyString , " e 546C8DF278CD5931069B522E695D4F2", .

, "EncryptKey" .

Case 2: When keyString is not within 32 characters (not in your case)

This is indicated in the specified key is not valid for this algorithm

Assuming keyString does not have 32 hexadecimal characters, then you will get the error message " The specified key is not valid for this algorithm. "

I hope for this help. Happy coding.

-3
source

Source: https://habr.com/ru/post/1016918/


All Articles