首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在c#和在线工具中使用AES加密时获得不同的输出

在c#和在线工具中使用AES加密时获得不同的输出
EN

Stack Overflow用户
提问于 2016-08-10 21:54:24
回答 2查看 1.1K关注 0票数 0
代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
using System.ComponentModel;


namespace DecryptionToEncryption
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create byte arrays to hold original, encrypted, and decrypted data.
            **byte[] encryptedstring = {0x7B,0x35,0x30,0x36,0x30,0x32,0x36,0x30,0x34,0x7C,0x55,0x38,0x30,0x30,0x45,0x44,0x45,0x37,0x33,0x46,0x32,0x34,0x31,0x41,0x43,0x32,0x45,0x35,0x38,0x41,0x37,0x44,0x37,0x34,0x43,0x38,0x37,0x39,0x44,0x31,0x44,0x37,0x37,0x7C,0x34,0x44,0x42,0x36,0x43,0x34,0x7D};**
            string data = Encoding.UTF7.GetString(encryptedstring);

            byte[] key = new byte[16];
            for (int i = 0; i < 16; ++i)
            {
                key[i] = 1;
            }

            byte[] iv = new byte[16];
            for (int i = 0; i < 16; ++i)
            {
                iv[i] = 1;
            }


            RijndaelManaged myRijndael = new RijndaelManaged();

            myRijndael.Key = key;
            myRijndael.IV = iv;
            byte[] encrypted = encryptStringToBytes_AES(data, myRijndael.Key, myRijndael.IV);
            string str =Encoding.UTF7.GetString(encrypted);
            char[] charValues = str.ToCharArray();
            string hexOutput = "";
            foreach (char _eachChar in charValues)
            {
                // Get the integral value of the character.
                int value = Convert.ToInt32(_eachChar);
                // Convert the decimal value to a hexadecimal value in string form.
                hexOutput += String.Format("{0,10:X}", value);
                // to make output as your eg 
                //  hexOutput +=" "+ String.Format("{0:X}", value);

            }


            Console.WriteLine(hexOutput);


            Console.ReadLine();

            // sends the byte array via active tcp connection
          //  _transport.SendEncryptedData(encrypted);
        }



        static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the stream used to encrypt to an in memory
            // array of bytes.
            MemoryStream msEncrypt = null;

            // Declare the RijndaelManaged object
            // used to encrypt the data.
            RijndaelManaged aesAlg = null;

            try
            {
                // Create a RijndaelManaged object
                // with the specified key and IV.
                aesAlg = new RijndaelManaged();
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create an encrypto to perform the stream transform.
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for encryption.
                msEncrypt = new MemoryStream();
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Write all data to the stream.
                        swEncrypt.Write(plainText);

                    }
                }
            }

            finally
            {
                // Clear the RijndaelManaged object.
                if (aesAlg != null)
                    aesAlg.Clear();enter code here
            }

            // Return the encrypted bytes from the memory stream.
            return msEncrypt.ToArray();
        }
    }
}

我正在运行这段c#代码,以便使用AES/CBC进行加密。我的输入是below.and我的输出是:36 E7 78 F8 B8 97 15 6C C3 73 EA A8 1B 12 71 C2 A0 5A F2 40 60 89 6B 8 70 90 C9 B6 75 57 F7 22 73 3D 15 AB B1 D5 E5 73 85 E A5 7E A9 D2 7C F2 48 C9 2D DF 6A 4E CA CB 31 AC D1 2 B2 C3 DB 89

但在在线工具中,它显示了一些不同的...(最后一行与我的输出不同):36 e7 78 f8 b8 97 15 6c c3 73 ea a8 1b 12 71 c2 a0 5a f2 40 60 89 6b 08 70 90 c9 b6 75 57 f7 22 73 3d 15 ab b1 d5 e5 73 85 0e a5 7e a9 d2 7c f2 ff aa 96 0e 3e f6 aa 0c 7e c5 15 2e 97 2f fd be

请帮助我获得相同的输出,如在线工具所示。

EN

Stack Overflow用户

发布于 2016-08-11 21:16:12

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
using System.ComponentModel;

namespace final_encryption
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                string original = "{50602604|U800EDE73F241AC2E58A7D74C879D1D77|4DB6C4}"; //"Here is some data to encrypt!";
                Console.WriteLine("Original:   " + original);
                Console.ReadLine();

                // Create a new instance of the RijndaelManaged
                // class.  This generates a new key and initialization 
                // vector (IV).
                byte[] key = new byte[16];
                for (int i = 0; i < 16; ++i)
                {
                    key[i] = 1;
                }

                byte[] iv = new byte[16];
                for (int i = 0; i < 16; ++i)
                {
                    iv[i] = 1;
                }
                using (RijndaelManaged myRijndael = new RijndaelManaged())
                {
                    // myRijndael.GenerateKey();
                    //myRijndael.GenerateIV();
                    myRijndael.Key = key;
                    myRijndael.IV = iv;
                    // Encrypt the string to an array of bytes.
                    byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);

                    StringBuilder s = new StringBuilder();
                    foreach (byte item in encrypted)
                    {
                        s.Append(item.ToString("X2") + " ");
                    }
                    Console.WriteLine("Encrypted:   " + s);
                    Console.ReadLine();

                    // Decrypt the bytes to a string.
                    string decrypted = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

                    //Display the original data and the decrypted data.
                    Console.WriteLine("Decrypted:    " + decrypted);
                    Console.ReadLine();
                }

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

            }
        }
        static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");
            byte[] encrypted;
            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;
                rijAlg.Mode = CipherMode.CBC;
                rijAlg.Padding = PaddingMode.Zeros;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {



                            //Write all data to the stream.

                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }


            // Return the encrypted bytes from the memory stream.
            return encrypted;

        }

        static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;
                rijAlg.Mode = CipherMode.CBC;
                rijAlg.Padding = PaddingMode.Zeros;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {

                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;
        }
    }
}

我解决了这个问题,现在用在线工具显示了正确的答案。

我的输出:36 E7 78 F8 B8 97 15 6C C3 73 EA A8 1B 12 71 C2 A0 5A F2 40 60 89 6B 08 70 90 C9 B6 75 57 F7 22 73 3D 15 AB B1 D5 E5 73 85 0E A5 7E A9 D2 7C F2 FF AA 96 0E 3E AA 0C 7E #EN20# 15 2E 97 2F FD BE

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38875447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档