问题使用AES和base64_encode结果

时间:2010-07-22 18:46:02

标签: c# encryption aes

我正在尝试加密字符串然后将其转换为base64_encode字符串以传递给网站,以便它可以解密它。

我的下面的函数似乎没有正常工作,它不会将base64_encode字符串解密回原始字符串。

有人看到问题吗?

由于

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 个答案:

答案 0 :(得分:1)

我不确定它到底出了什么问题,但是当我这样做时,我也遇到了一些奇怪的问题。我将与您分享我编写的代码,以使其正常工作:


        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和IV是类中包含的静态byte []。