在c#和在线工具中使用AES加密时获得不同的输出
本文关键字:输出 加密 AES 在线 工具 | 更新日期: 2023-09-27 18:01:58
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进行加密。我的输入如下。我的输出如下:
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
请帮我获得在线工具中显示的相同输出。
加密字符串长度为51个字符。
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};
这不是CBC模式的可能长度。
AES是一种分组密码,在包括CBC在内的大多数模式下都能处理数据块。
对解密的输入过短将导致解密代码用一些东西填充丢失的字节,可能是后面的垃圾。由于这是最后一个块,对于不同的实现,解密可能会有所不同。
但将数据视为UTF-8字符串是:
"{50602604|U800EDE73F241AC2E58A7D74C879D1D77|4DB6C4}"
这不是人们对加密数据的期望,加密数据应该没有图案并且看起来像随机字节。
它看起来像是由三个组件组成的格式化数据:
50602604
U800EDE73F241AC2E58A7D74C879D1D77
4DB6C4
第二个组成部分的第一个字符是唯一的非十六进制字符,因此它可以是剩余32个字符的指示符。
难怪它不能正确解密。
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;
}
}
}
我用在线工具解决了这个问题,现在显示出了正确的答案。
MY输出: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