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

时间:2017-02-08 10:12:39

标签: c# cryptography encryption-symmetric hybrid encryption-asymmetric

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

根据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;
        }

1 个答案:

答案 0 :(得分:4)

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

如果您希望加密输出为文本,请使用密文字节并使用Convert.ToBase64String()将其转换为文本字符串。

解密时,将Base64字符串转换回字节并解密字节。