tags:

views:

26

answers:

1

Hi,

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  A: 

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.

David Burhans
@David Burhans: Thanks very much, worked a dream!
arbme