AES Encryption using RijndaelManged Class: Baes64编码解码aes加密字符串

AES Encryption using RijndaelManged Class: Baes64 encoding and decoding aes encryption string

所以,我在解密解码的 base64 aes 字符串时遇到问题。这可能吗?我写了一个小的控制台程序来解决这个问题,但没有成功。这是我的例子:

如图所示,我已经成功地将 base64 转换回 aes 加密字符串,但是当我尝试解密它时,我得到了更多垃圾。如果需要代码片段,请告诉我。感谢大家的帮助 :) 更新:解密方法的代码片段

static void Main(string[] args)
    {
        string plainText;
        string decrypted;
        string decryptedFromB64EncodedDecoded;
        string fromBase64ToEncryptedText;
        string encryptedText;
        string encryptedTextBase64;
        byte[] encryptedBytes;

        byte[] encryptedBytes2;
        byte[] encryptedBytesBase64;

        RijndaelManaged crypto = new RijndaelManaged();

        UTF8Encoding UTF = new UTF8Encoding();

        Console.WriteLine("Please put in the text to be encrypted.");
        plainText = Console.ReadLine();

        try
        {
            encryptedBytes = encrypt(plainText, crypto.Key, crypto.IV);
            encryptedText = Encoding.ASCII.GetString(encryptedBytes);
            //encryptedBytes2 = Encoding.ASCII.GetBytes(encryptedText);

            encryptedTextBase64 = toBase64String(encryptedText);
            encryptedBytesBase64 = fromBase64String(encryptedTextBase64);

            fromBase64ToEncryptedText = Encoding.ASCII.GetString(encryptedBytesBase64);

            encryptedBytes2 = Encoding.ASCII.GetBytes(fromBase64ToEncryptedText);

            decrypted = decrypt(encryptedBytes, crypto.Key, crypto.IV);
            decryptedFromB64EncodedDecoded = decrypt(encryptedBytes2, crypto.Key, crypto.IV);

            Console.WriteLine("Start: {0}", plainText);
            Console.WriteLine("Encrypted: {0}", encryptedText);
            Console.WriteLine("Encrypted Base64: {0}", encryptedTextBase64);
            Console.WriteLine("From Base64 To AES Encypted Text: {0}", fromBase64ToEncryptedText);
            Console.WriteLine("Decrypted: {0}", decrypted);

            Console.WriteLine("Decrypted From Encode and then Decode Base64 Text: {0}", decryptedFromB64EncodedDecoded);

        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: {0}", ex.Message);
        }

        Console.ReadLine();
    }

public static string decrypt (byte[] textToDecrypt, byte[] key, byte[] IV)
    {
        RijndaelManaged crypto = new RijndaelManaged();
        MemoryStream stream = new MemoryStream(textToDecrypt) ;
        ICryptoTransform decryptor = null;
        CryptoStream cryptoStream = null;
        StreamReader readStream = null;

        string text = string.Empty;

        try
        {
            crypto.Key = key;
            crypto.IV = IV;
            crypto.Padding = PaddingMode.None;

            decryptor = crypto.CreateDecryptor(crypto.Key, crypto.IV);
            cryptoStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read);
            //cryptoStream.Read(textToDecrypt, 0, textToDecrypt.Length);
            readStream = new StreamReader(cryptoStream);
            text = readStream.ReadToEnd();
            cryptoStream.Close();

            byte[] decodedValue = stream.ToArray();

            return text;
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if (crypto != null)
            {
                crypto.Clear();
            }
            stream.Flush();
            stream.Close();
        }
    }

public static byte[] encrypt(string text, byte[] key, byte[] IV)
    {
        RijndaelManaged crypto = null;
        MemoryStream stream = null;

        //ICryptoTransform is used to perform the actual decryption vs encryption, hash function are a version crypto transforms
        ICryptoTransform encryptor = null;
        //CryptoStream allows for encrption in memory
        CryptoStream cryptoStream = null;

        UTF8Encoding byteTransform = new UTF8Encoding();

        byte[] bytes = byteTransform.GetBytes(text);

        try
        {
            crypto = new RijndaelManaged();
            crypto.Key = key;
            crypto.IV = IV;

            stream = new MemoryStream();

            encryptor = crypto.CreateEncryptor(crypto.Key, crypto.IV);
            cryptoStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(bytes, 0, bytes.Length);

        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            if (crypto != null)
            {
                crypto.Clear();
            }
            cryptoStream.Close();
        }

        return stream.ToArray();
    }

 public static string toBase64String(string value)
    {
        UTF8Encoding UTF = new UTF8Encoding();
        byte[] myarray =  UTF.GetBytes(value);
        return Convert.ToBase64String(myarray);
    }

    public static byte[] fromBase64String(string mystring)
    {
        //UTF8Encoding UTF = new UTF8Encoding();
        //byte[] myarray = UTF.GetBytes(value);
        return Convert.FromBase64String(mystring);
    }

我不知道你是如何解密的,但在你解密之前,你应该将 base 64 字符串转换为字节数组,然后再将其发送到解密中。

byte[] encryptedStringAsBytes = Convert.FromBase64String(base64EncodedEncryptedValue);

然后使用字节数组,您可以通过 MemoryStream 传递给 CryptoStream。

更新

我认为问题在于您设置流的方式

            using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
            {
                rijndaelManaged.Padding = paddingMode;
                rijndaelManaged.Key     = key;
                rijndaelManaged.IV      = initVector;

                MemoryStream memoryStream = null;
                try
                {
                    memoryStream = new MemoryStream(valueToDecrypt);
                    using (ICryptoTransform cryptoTransform = rijndaelManaged.CreateDecryptor())
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Read))
                        {
                            using (StreamReader streamReader = new StreamReader(cryptoStream))
                            {
                                return streamReader.ReadToEnd();                       
                            }
                        }
                    }
                }
                finally
                {
                    if (memoryStream != null)
                        memoryStream.Dispose();
                }
            }    

更新 2

这就是执行这些步骤的基本方式。

加密

  1. 使用 Encoding.GetBytes(stringToEncrypt)
  2. 对您的纯文本字符串进行编码
  3. 将字节[]传递到加密API(通过内存流等)
  4. 从加密流中获取字节并将结果编码为 Base64

解密(反向操作)

  1. 使用 Convert.FromBase64String(base64EncodedEncryptedValue) 将 base64 编码的字符串转换为字节
  2. 将该字节数组传递到上面的解密函数中

尝试:

encryptedBytes2 = Encoding.ASCII.GetBytes(encryptedText);

根据您的评论。字节就是那个字节,所以为了解密密文,你需要撤销你已经完成的任何编码或一系列编码。

如果你真的想从加密字节 -> Base64String -> ASCII 字符串 -> 然后解密那个 ASCII 字符串?您需要对 ascii 字符串进行 base64 解码,然后使用

将该字符串转换为字节
Encoding.ASCII.GetBytes(yourdecodedstring); 

请注意,base 64 解码与使用 Convert.FromBase84String 不同。