Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 938127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:35:29+00:00 2026-05-15T21:35:29+00:00

I am trying to encrypt a string then covert it into a base64_encode string

  • 0

I am trying to encrypt a string then covert it into a base64_encode string to be passed to a website so that it can decrypt it.

My functions below dont seem to be working correctly and it won’t decrypt the base64_encode string back to the original string.

Anyone see the problem?

Thanks

public static string EncryptToString(string data, string KeyString, string IVString)
{
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] Data = encoding.GetBytes(data);
    byte[] Key = encoding.GetBytes(KeyString);
    byte[] IV = encoding.GetBytes(IVString);

    try
    {
        MemoryStream fStream = new MemoryStream(Data);

        // Set Rijndael object mode.

        Rijndael RijndaelAlg;
        RijndaelAlg = Rijndael.Create();
        RijndaelAlg.Mode = CipherMode.CBC;

        // Create a CryptoStream using the FileStream 
        // and the passed key and initialization vector (IV).
        CryptoStream cStream = new CryptoStream(fStream,
            RijndaelAlg.CreateEncryptor(Key, IV),
            CryptoStreamMode.Read);

        // Create a StreamReader using the CryptoStream.
        StreamReader sReader = new StreamReader(cStream);

        string encryptedString = null;

        try
        {
            // Read the data from the stream 
            // to decrypt it.
            encryptedString = sReader.ReadToEnd();

            // Convert result into a base64-encoded string.
            byte[] bytes = encoding.GetBytes(encryptedString);
            encryptedString = Convert.ToBase64String(bytes, 0, bytes.Length);

        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: {0}", e.Message);
        }
        finally
        {
            // Close the streams and
            // close the file.
            sReader.Close();
            cStream.Close();
            fStream.Close();
            RijndaelAlg.Dispose();
        }

        // Return the string. 
        return encryptedString;
    }
    catch (CryptographicException e)
    {
        Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
        return null;
    }
    catch (UnauthorizedAccessException e)
    {
        Console.WriteLine("A file error occurred: {0}", e.Message);
        return null;
    }
}

public static string DecryptToString(string data, string KeyString, string IVString)
{
    ASCIIEncoding encoding = new ASCIIEncoding();
    // Convert result from a base64-encoded to string.
    byte[] Data = Convert.FromBase64String(data);

    byte[] Key = encoding.GetBytes(KeyString);
    byte[] IV = encoding.GetBytes(IVString);

    try
    {
        MemoryStream fStream = new MemoryStream(Data);

        // Set Rijndael object mode.
        Rijndael RijndaelAlg;
        RijndaelAlg = Rijndael.Create();
        RijndaelAlg.Mode = CipherMode.CBC;

        // Create a CryptoStream using the FileStream 
        // and the passed key and initialization vector (IV).
        CryptoStream cStream = new CryptoStream(fStream,
            RijndaelAlg.CreateDecryptor(Key, IV),
            CryptoStreamMode.Read);

        // Create a StreamReader using the CryptoStream.
        StreamReader sReader = new StreamReader(cStream);

        string decryptedString = null;

        try
        {
            // Read the data from the stream 
            // to decrypt it.
            decryptedString = sReader.ReadToEnd();

        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: {0}", e.Message);
        }
        finally
        {
            // Close the streams and
            // close the file.
            sReader.Close();
            cStream.Close();
            fStream.Close();
            RijndaelAlg.Dispose();
        }

        // Return the string. 
        return decryptedString;
    }
    catch (CryptographicException e)
    {
        Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
        return null;
    }
    catch (UnauthorizedAccessException e)
    {
        Console.WriteLine("A file error occurred: {0}", e.Message);
        return null;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:35:30+00:00Added an answer on May 15, 2026 at 9:35 pm

    I’m not sure exactly where it is going wrong, but when I was doing this I had some strange issues too. I’ll share with you the code I wrote to get this working properly:

    
            static public String EncryptString(String message)
            {
                String sRet = "";
                RijndaelManaged rj = new RijndaelManaged();
                try
                {
                    rj.Key = Key;
                    rj.IV = IV;
                    MemoryStream ms = new MemoryStream();
    
                    using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(cs))
                        {
                            sw.Write(message);
                        }
                    }
                    byte[] encoded = ms.ToArray();
                    sRet = Convert.ToBase64String(encoded);
    
                }
                finally
                {
                    rj.Clear();
                }
    
                return sRet;
    
            }
    
            static public String DecryptString(String cypher)
            {
                String sRet = "";
                RijndaelManaged rj = new RijndaelManaged();
                try
                {
                    byte[] message = Convert.FromBase64String(cypher);
                    rj.Key = Key;
                    rj.IV = IV;
                    MemoryStream ms = new MemoryStream(message);
    
                    using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Read))
                    {
                        using (StreamReader sr = new StreamReader(cs))
                        {
                            sRet = sr.ReadToEnd();
                        }
                    }
    
                }
                finally
                {
                    rj.Clear();
                }
    
                return sRet;
    
            }

    Key and IV are static byte[] contained in the class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to encrypt a string in Java on Android, and then decrypt it
I'm trying to encrypt a string, save it in a file and then later
Have been trying to encrypt an xml file to a string so that I
I'm trying to figure out how to encrypt / decrypt a string in VB.Net.
I'm trying to encrypt a text file using Perl and then decrypt it using
Hello I am trying to encrypt / decrypt a string via Rijaendal. I simply
I'm trying to encrypt some data from python (Google App Engine) and then decrypt
I'm trying to encrypt some (cookie) data in C# and then decrypt it in
I am trying to encrypt data by the cipher DES-EDE3-CBC in Ruby, then decrypt
I am trying to encrypt a string but often only part of the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.