为什么解密后的字符串与输入字符串不匹配?

时间:2019-05-26 15:07:00

标签: c# aes rsacryptoserviceprovider

在此代码段中,我使用了RSACryptoServiceProvider类对Rfc2898DerivedBytes类生成的密钥和IV进行加密。然后将密钥和IV解密,插入AesManaged加密器中,然后使用AesManaged加密器对字符串进行加密(转换为字节数组)。反向执行该过程以解密成功的数组,但是返回的字符串与输入的字符串不匹配。为什么会这样?

我已经尝试过更改字节数组的编码格式,但这没有用。我检查了原始字节数组,并检查了它经过加密和解密的整个过程后的样子,它们是相同的。

class EncryptionBestPracticeDemo
    {
        public static byte[] keys;
        public static AesManaged aesAlgorithm = new AesManaged();        

        public struct KeyIV
        {            
            public byte[] Key { get; private set; }
            public byte[] IV { get; private set; }
            public KeyIV(byte[] key, byte[] iV)
            {
                this.Key = key;
                this.IV = iV;
            }
        }

        public static KeyIV GetEncryptedKeyAndIV()
        {
            KeyIV keySalt;

            var password = "Pa$$w0rd";
            var salt = "S@lt";

            var rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));

            aesAlgorithm.Padding = PaddingMode.None;

            var rgbKey = rgb.GetBytes(aesAlgorithm.KeySize / 8);            

            var rgbIV = rgb.GetBytes(aesAlgorithm.BlockSize / 8);            

            //Encrypt the key and IV
            using (var rsaProvider = new RSACryptoServiceProvider())
            {
                var setOaemPadding = true;

                keys = rsaProvider.ExportCspBlob(true);

                keySalt = new KeyIV(rsaProvider.Encrypt(rgbKey, setOaemPadding), rsaProvider.Encrypt(rgbIV, setOaemPadding));
            }
            return keySalt;
        }

        public static void AesWithRSA()
        {
            //Use RSACryptoProvider to decrypt the secret key of the AesAlgorithm algorithm
            var message = "NET Cryptography";
            var messageByteArray = Encoding.UTF8.GetBytes(message);

            byte[] encryptedData;
            byte[] decryptedData;

            //Get the encrypted Key and IV
            var keySalt = GetEncryptedKeyAndIV();            

            using (var rsaProvider = new RSACryptoServiceProvider())
            {
                var setOaemPadding = true;
                rsaProvider.ImportCspBlob(keys);

                //Decrypt the key and IV
                var key = rsaProvider.Decrypt(keySalt.Key, setOaemPadding);
                var iV = rsaProvider.Decrypt(keySalt.IV, setOaemPadding);

                //Use the decrypted key and IV in the AES encryptor
                var encryptor = aesAlgorithm.CreateEncryptor(key, iV);

                using (var bufferStream = new MemoryStream())
                using (var cryptoStream = new CryptoStream(bufferStream, encryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(messageByteArray, 0, messageByteArray.Length);

                    encryptedData = bufferStream.ToArray();
                }
            }

            using (var rsaProvider = new RSACryptoServiceProvider())
            {
                var setOaemPadding = true;
                rsaProvider.ImportCspBlob(keys);

                //Decrypt the key and IV
                var key = rsaProvider.Decrypt(keySalt.Key, setOaemPadding);                
                var iV = rsaProvider.Decrypt(keySalt.IV, setOaemPadding);

                //Use the decrypted key and IV in the AES decryptor
                var decryptor = aesAlgorithm.CreateEncryptor(key, iV);

                using (var bufferStream = new MemoryStream())
                using (var cryptoStream = new CryptoStream(bufferStream, decryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(encryptedData, 0, encryptedData.Length);

                    decryptedData = bufferStream.ToArray();

                    Console.WriteLine(Encoding.UTF8.GetString(decryptedData));                                        
                }
            }
        }
    }
  • 输入字符串:“ NET密码学”。
  • 预期的输出字符串:“ NET密码学”。
  • 输出字符串:“ ???? b!?k?!??+ X”。

0 个答案:

没有答案