How to encrypt strings that may have non-base 64 characters
<p><strong>UPDATED:</strong> The problem was <strong>me</strong>! I did a mistake otherwise both Cods (below and the one at <strong>PS</strong> are correct) But still thanks to great answer from @Luke Park that I learned something new.</p>

<p>I am not familiar with encryption/decryption algorithms, Therefor I search the net and found this class:</p>

<p><a href="https://stackoverflow.com/questions/10168240/encrypting-decrypting-a-string-in-c-sharp/10177020#10177020">Encrypting &amp; Decrypting a String in C#</a></p>

<p>the code is :
(I add a Try/Catch in <code>Decrypt</code> method in case the password was wrong it will <code>return "";</code>)</p>

<pre><code>using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Linq;

namespace EncryptStringSample
{
    public static class StringCipher
    {
        // This constant is used to determine the keysize of the encryption algorithm in bits.
        // We divide this by 8 within the code below to get the equivalent number of bytes.
        private const int Keysize = 256;

        // This constant determines the number of iterations for the password bytes generation function.
        private const int DerivationIterations = 1000;

        public static string Encrypt(string plainText, string passPhrase)
        {
            // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
            // so that the same Salt and IV values can be used when decrypting.  
            var saltStringBytes = Generate256BitsOfRandomEntropy();
            var ivStringBytes = Generate256BitsOfRandomEntropy();
            var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
            {
                var keyBytes = password.GetBytes(Keysize / 8);
                using (var symmetricKey = new RijndaelManaged())
                {
                    symmetricKey.BlockSize = 256;
                    symmetricKey.Mode = CipherMode.CBC;
                    symmetricKey.Padding = PaddingMode.PKCS7;
                    using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                            {
                                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                                cryptoStream.FlushFinalBlock();
                                // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
                                var cipherTextBytes = saltStringBytes;
                                cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
                                cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
                                memoryStream.Close();
                                cryptoStream.Close();
                                return Convert.ToBase64String(cipherTextBytes);
                            }
                        }
                    }
                }
            }
        }

    public static string Decrypt(string cipherText, string passPhrase)
    {
        // Get the complete stream of bytes that represent:
        // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
        var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
        // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
        var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
        // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
        var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
        // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
        var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
        try
        {
            using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
            {
                var keyBytes = password.GetBytes(Keysize / 8);
                using (var symmetricKey = new RijndaelManaged())
                {
                    symmetricKey.BlockSize = 256;
                    symmetricKey.Mode = CipherMode.CBC;
                    symmetricKey.Padding = PaddingMode.PKCS7;
                    using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
                    {
                        using (var memoryStream = new MemoryStream(cipherTextBytes))
                        {
                            using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                            {
                                var plainTextBytes = new byte[cipherTextBytes.Length];
                                var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                                memoryStream.Close();
                                cryptoStream.Close();
                                return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception)
        {
            return "";
        }
    }

        private static byte[] Generate256BitsOfRandomEntropy()
        {
            var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits.
            using (var rngCsp = new RNGCryptoServiceProvider())
            {
                // Fill the array with cryptographically secure random bytes.
                rngCsp.GetBytes(randomBytes);
            }
            return randomBytes;
        }
    }
}
</code></pre>

<p>I used that class at my application like this:</p>

<pre><code>string plaintext = "InsertedPasswordByUserToEncrypt";
string password = plaintext; // use its own password as encryption key
string encryptedstring = StringCipher.Encrypt(plaintext, password);
</code></pre>

<p>and I like that class be cause it give me different encryption results if I repeat last line with same data.</p>

<p>But now, I found out if a string have any characters other than base64 characters it would rise this exception:
"<strong>The input is not a valid Base-64 string as it contains a non-base 64 character</strong>" I search the net and I found many answers to this problem. Like these:</p>

<p><a href="https://stackoverflow.com/questions/15114044/the-input-is-not-a-valid-base-64-string-as-it-contains-a-non-base-64-character">The input is not a valid Base-64 string as it contains a non-base 64 character</a></p>

<p><a href="https://stackoverflow.com/questions/16774024/how-can-i-solver-an-base64-invalid-characters-error">How can I solver an &quot;base64 invalid characters&quot; error?</a></p>

<p>In all those questions, answers was the same: </p>

<blockquote>
  <p>Remove non-base64 characters from your string!!!</p>
</blockquote>

<p>But what if I or my application users wants to insert a string like this: "<strong>A@S#D$?</strong>" or "<strong>CanYouGu3$$Me?</strong>" or .... to be encrypted?</p>

<p><strong>My Question:</strong> </p>

<p><strong>A1.</strong> Is there any way to solve the above Class problem (that I mention at above) without replacing or removing any characters that users may insert to be encrypted?</p>

<p><strong>A2.</strong> If there are no fix, So what are the other best ways? what method I can use that It can encrypt/decrypt any string with any characters in it.</p>

<p>PS: This Code also is a good one and do not have any problem with existing non-base64 characters (cause it also use this method <code>Encoding.UTF8.GetBytes</code> to prevent any exceptions): <a href="https://codereview.stackexchange.com/questions/14892/simplified-secure-encryption-of-a-string">https://codereview.stackexchange.com/questions/14892/simplified-secure-encryption-of-a-string</a></p>

<p>Thanks for your time</p>