与AES-256-CFB的实现不同的输出

时间:2018-07-06 10:12:20

标签: c# .net encryption aes

我必须解密已经加密的值。我知道使用的设置,键和iv,但始终会得到错误的结果。我知道加密的方式与此在线工具https://appzaza.com/encrypt-text相同,该工具可以提供正确的结果,但似乎无法找到我的输出不同的原因。 enter image description here

设置: 模式:CFB 密钥大小:256 编码:UTF-8

我的代码:

            static void Main()
            {
                string plaintext = "1515151515115129";
                string pass = "ncJ9JhWrXa9DqN1GzBCNlBoKs289Q1dD";
                string iveee = "jHGasOT60bJXcHdd";
                string message = "sdH9HhaLd8K8f0jBdOP/2g==";

                byte[] passBytes = (Encoding.UTF8.GetBytes(pass));

                // Create secret IV
                byte[] ivBytes = Encoding.UTF8.GetBytes(iveee);

                string decrypted = DecryptString(message, passBytes, ivBytes);
            }

            public static string DecryptString(string cipherText, byte[] key, byte[] iv)
            {
                // Instantiate a new Aes object to perform string symmetric encryption
                //Aes encryptor = Aes.Create();
                using (var encryptor = Aes.Create())
                {
                    encryptor.Mode = CipherMode.CFB;
                    encryptor.KeySize = 256;
                    encryptor.Padding = PaddingMode.Zeros;

                    // Set key and IV
                    encryptor.Key = key;
                    encryptor.IV = iv;

                    byte[] cipherBytes = Convert.FromBase64String(cipherText);

                    // Instantiate a new MemoryStream object to contain the encrypted bytes
                    using (MemoryStream memoryStream = new MemoryStream(cipherBytes))
                    {

                        // Instantiate a new encryptor from our Aes object
                        using (ICryptoTransform aesDecryptor = encryptor.CreateDecryptor(key, iv))
                        {
                            string plainText = "";
                            // Instantiate a new CryptoStream object to process the data and write it to the 
                            // memory stream
                            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Read))
                            {
                                using (StreamReader sr = new StreamReader(cryptoStream))
                                {
                                    plainText = sr.ReadToEnd();
                                }
                            }
                            return plainText;
                        }
                    }
                }
            }

任何想法可能是什么原因?

输入用于测试目的的加密代码:

        public static string EncryptString(string plainText, byte[] key, byte[] iv)
        {
            // Instantiate a new Aes object to perform string symmetric encryption
            Aes encryptor = Aes.Create();

            encryptor.Mode = CipherMode.CFB;
            encryptor.KeySize = 256;
            encryptor.Padding = PaddingMode.Zeros;

            // Set key and IV
            encryptor.Key = key;
            encryptor.IV = iv;

            // Instantiate a new MemoryStream object to contain the encrypted bytes
            MemoryStream memoryStream = new MemoryStream();

            // Instantiate a new encryptor from our Aes object
            ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();

            // Instantiate a new CryptoStream object to process the data and write it to the 
            // memory stream
            CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);

            // Convert the plainText string into a byte array
            byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);

            // Encrypt the input plaintext string
            cryptoStream.Write(plainBytes, 0, plainBytes.Length);

            // Complete the encryption process
            cryptoStream.FlushFinalBlock();

            // Convert the encrypted data from a MemoryStream to a byte array
            byte[] cipherBytes = memoryStream.ToArray();

            // Close both the MemoryStream and the CryptoStream
            memoryStream.Close();
            cryptoStream.Close();

            // Convert the encrypted byte array to a base64 encoded string
            string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);

            // Return the encrypted data as a string
            return cipherText;
        }

我尝试了所有不同的填充模式,但没有任何结果

0 个答案:

没有答案