Encrypt and decrypt

Asked

Viewed 85 times

0

I took this code here to encrypt and decrypt a Id of the type `Guid:

 public static string EncryptString(string text, string keyString)
    {
        var key = Encoding.UTF8.GetBytes(keyString);

        using (var aesAlg = Aes.Create())
        {
            using (var encryptor = aesAlg.CreateEncryptor(key, aesAlg.IV))
            {
                using (var msEncrypt = new MemoryStream())
                {
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    using (var swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(text);
                    }

                    var iv = aesAlg.IV;

                    var decryptedContent = msEncrypt.ToArray();

                    var result = new byte[iv.Length + decryptedContent.Length];

                    Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
                    Buffer.BlockCopy(decryptedContent, 0, result, iv.Length, decryptedContent.Length);

                    return Convert.ToBase64String(result);
                }
            }
        }
    }

    public static string DecryptString(string cipherText, string keyString)
    {
       var fullCipher = Convert.FromBase64String(cipherText);

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

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

        using (var aesAlg = Aes.Create())
        {
            using (var decryptor = aesAlg.CreateDecryptor(key, iv))
            {
                string result;
                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;
            }
        }
    }

It cripgraphs normal, but at the time of decryption, it gives error in this line:

  var fullCipher = Convert.FromBase64String(cipherText);

Error: The input is not a Valid Base-64 string as it contains a non-base 64 Character, more than two padding characters, or an illegal Character Among the padding characters.'

I don’t know what else to do to fix it, so if you have any other way to encrypt, decrypt, I’ll take suggestions.

1 answer

2


Check the use of the Padding option of your Base64 Encoder / Decoder. Use the NO_PADDING option and run a test. I believe C# should exist.

Sometimes it is not ciphering/deciphering right also because of IV (initialization vector)

Here is a good and functional example of AES encryption in CBC mode. There is also PKCS5/PKCS7. Give a study.

https://gist.github.com/therightstuff/30e5cbd9b1e0de1b8865c8fb6e2971e4

In this example, the code shows commented if you want to use a certain type of Preview and which mode it should operate:

//cipher.Mode = CipherMode.CBC;
//cipher.Padding = PaddingMode.PKCS7;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.