Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#和在线工具中使用AES加密时获得不同的输出 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Security.Cryptography; 使用System.IO; 使用系统组件模型; 命名空间解密到加密 { 班级计划 { 静态void Main(字符串[]参数) { //创建字节数组以保存原始、加密和解密的数据。 **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,0x37,0x34,0x43,0x38,0x37,0x37,0x44,0x37,0x37,0x44,0x37,0x37,0x34,0x42,0x34,0x7D}** 字符串数据=Encoding.UTF7.GetString(encryptedstring); 字节[]键=新字节[16]; 对于(int i=0;i_C# - Fatal编程技术网

在c#和在线工具中使用AES加密时获得不同的输出 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Security.Cryptography; 使用System.IO; 使用系统组件模型; 命名空间解密到加密 { 班级计划 { 静态void Main(字符串[]参数) { //创建字节数组以保存原始、加密和解密的数据。 **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,0x37,0x34,0x43,0x38,0x37,0x37,0x44,0x37,0x37,0x44,0x37,0x37,0x34,0x42,0x34,0x7D}** 字符串数据=Encoding.UTF7.GetString(encryptedstring); 字节[]键=新字节[16]; 对于(int i=0;i

在c#和在线工具中使用AES加密时获得不同的输出 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Security.Cryptography; 使用System.IO; 使用系统组件模型; 命名空间解密到加密 { 班级计划 { 静态void Main(字符串[]参数) { //创建字节数组以保存原始、加密和解密的数据。 **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,0x37,0x34,0x43,0x38,0x37,0x37,0x44,0x37,0x37,0x44,0x37,0x37,0x34,0x42,0x34,0x7D}** 字符串数据=Encoding.UTF7.GetString(encryptedstring); 字节[]键=新字节[16]; 对于(int i=0;i,c#,C#,则加密字符串的长度为51个字符 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 {

则加密字符串的长度为51个字符

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();
        }
    }
}
这不是CBC模式的可能长度

AES是一种分组密码,可在包括CBC在内的大多数模式下处理数据块

对解密的输入(短)将导致解密代码在丢失的字节中填充一些内容,可能是垃圾。因为这是最后一个块,所以对于不同的实现,解密可能是不同的

但将数据视为UTF-8字符串是:

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};
这并不是人们对加密数据的期望,加密数据应该没有模式,看起来像随机字节

它看起来像包含三个组件的格式化数据:

"{50602604|U800EDE73F241AC2E58A7D74C879D1D77|4DB6C4}"
第二个组件的第一个字符是唯一的非十六进制字符,因此它可能是剩余32个字符的指示符


难怪它不能正确解密。

加密的字符串长度为51个字符

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();
        }
    }
}
50602604
U800EDE73F241AC2E58A7D74C879D1D77
4DB6C4
这不是CBC模式的可能长度

AES是一种分组密码,可在包括CBC在内的大多数模式下处理数据块

对解密的输入(短)将导致解密代码在丢失的字节中填充一些内容,可能是垃圾。因为这是最后一个块,所以对于不同的实现,解密可能是不同的

但将数据视为UTF-8字符串是:

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};
这并不是人们对加密数据的期望,加密数据应该没有模式,看起来像随机字节

它看起来像包含三个组件的格式化数据:

"{50602604|U800EDE73F241AC2E58A7D74C879D1D77|4DB6C4}"
第二个组件的第一个字符是唯一的非十六进制字符,因此它可能是剩余32个字符的指示符

难怪它不能正确解密。

使用系统;
50602604
U800EDE73F241AC2E58A7D74C879D1D77
4DB6C4
使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Security.Cryptography; 使用System.IO; 使用系统组件模型; 命名空间最终加密 { 班级计划 { 静态void Main(字符串[]参数) { 尝试 { 字符串original=“{50602604 | U800EDE73F241AC2E58A7D74C879D1D77 | 4DB6C4}”/“这里有一些要加密的数据!”; Console.WriteLine(“原件:+原件”); Console.ReadLine(); //创建RijndaelManaged的新实例 //这将生成一个新的密钥和初始化 //载体(IV)。 字节[]键=新字节[16]; 对于(int i=0;i<16;++i) { 键[i]=1; } 字节[]iv=新字节[16]; 对于(int i=0;i<16;++i) { iv[i]=1; } 使用(RijndaelManaged myRijndael=new RijndaelManaged()) { //myRijndael.GenerateKey(); //myRijndael.GenerateIV(); myRijndael.Key=Key; myRijndael.IV=IV; //将字符串加密为字节数组。 byte[]encrypted=EncryptStringToBytes(原始,myRijndael.Key,myRijndael.IV); StringBuilder s=新的StringBuilder(); foreach(加密中的字节项) { s、 追加(item.ToString(“X2”)+”); } Console.WriteLine(“加密:+s”); Console.ReadLine(); //将字节解密为字符串。 斯特林