混合密码学。要解密的数据长度无效

Hybrid cryptography. Length of the data to decrypt is invalid

我在混合加密实施过程中遇到上述错误。

根据https://en.wikipedia.org/wiki/Hybrid_cryptosystem

我卡在了最后一步

我的密码是

 private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                CspParameters cspParams = new CspParameters { ProviderType = 1 };
                RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(2048, cspParams);
                string publicKey =lblPublicKey.Text = Convert.ToBase64String(rsaProvider.ExportCspBlob(false));
                string privateKey = lblPrivateKey.Text= Convert.ToBase64String(rsaProvider.ExportCspBlob(true));
                string symmericKey = txtBoxSymmetricKey.Text = "Kamran12";
                txtEncryptedData.Text = EncryptData(txtInputData.Text, symmericKey);
                txtBoxEncryptedSymmetricKey.Text = RSA_Encrypt(symmericKey, publicKey);



                txtBoxDescryptedSymmetricKey.Text = RSA_Decrypt(txtBoxEncryptedSymmetricKey.Text, privateKey);
                txtDecryptedData.Text = DecryptData(txtEncryptedData.Text, txtBoxDescryptedSymmetricKey.Text); //getting error length of the data to decrypt is invalid
            }
            catch (Exception exc)
            {

            }

        }

        public static string RSA_Decrypt(string encryptedText, string privateKey)
        {
            CspParameters cspParams = new CspParameters { ProviderType = 1 };
            RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(cspParams);
            rsaProvider.ImportCspBlob(Convert.FromBase64String(privateKey));
            var buffer = Convert.FromBase64String(encryptedText);
            byte[] plainBytes = rsaProvider.Decrypt(buffer, false);
            string plainText = Encoding.UTF8.GetString(plainBytes, 0, plainBytes.Length);
            return plainText;
        }

        public static string RSA_Encrypt(string data, string publicKey)
        {
            CspParameters cspParams = new CspParameters { ProviderType = 1 };
            RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(cspParams);
            rsaProvider.ImportCspBlob(Convert.FromBase64String(publicKey));
            byte[] plainBytes = Encoding.UTF8.GetBytes(data);
            byte[] encryptedBytes = rsaProvider.Encrypt(plainBytes, false);
            return Convert.ToBase64String(encryptedBytes);
        }



        public string EncryptData(string data, string key)
        {
            string encryptedData = null;
            byte[] buffer = Encoding.UTF8.GetBytes(data);
            DESCryptoServiceProvider desCryptSrvckey = new DESCryptoServiceProvider
            {
                Key = new UTF8Encoding().GetBytes(key)
            };
            desCryptSrvckey.IV = desCryptSrvckey.Key;
            using (MemoryStream stmCipherText = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(stmCipherText, desCryptSrvckey.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(buffer, 0, buffer.Length);
                    cs.FlushFinalBlock();


                    encryptedData = Encoding.UTF8.GetString(stmCipherText.ToArray());
                }
            }
            return encryptedData;
        }

        public string DecryptData(string data, string key)
        {

            byte[] encryptedMessageBytes = Encoding.UTF8.GetBytes(data);
            string decryptedData = null;
            DESCryptoServiceProvider desCryptSrvckey = new DESCryptoServiceProvider
            {
                Key = new UTF8Encoding().GetBytes(key)
            };
            desCryptSrvckey.IV = desCryptSrvckey.Key;
            using (MemoryStream encryptedStream = new MemoryStream(encryptedMessageBytes))
            {
                using (
                    CryptoStream cs = new CryptoStream(encryptedStream, desCryptSrvckey.CreateDecryptor(),
                        CryptoStreamMode.Read))
                {
                    using (StreamReader sr = new StreamReader(cs))
                    {
                        decryptedData = sr.ReadToEnd();
                    }
                }
            }
            return decryptedData;
        }

您将 encryptedData 声明为字符串。这是不正确的。您的加密数据是字节,而不是字符串。尝试将原始字节转换为 UTF-8 文本,如 encryptedData = Encoding.UTF8.GetString(stmCipherText.ToArray()); 中那样不会产生 UTF-8 文本,但会产生垃圾并可能丢失数据。

如果您希望加密的输出为文本,则获取密文字节并使用 Convert.ToBase64String() 将它们转换为文本字符串。

解密时,将Base64字符串转回字节,再解密