[英]C# AES Encryption Byte Array
我想加密字節數組。 所以首先我在這個站點上嘗試一下。
計算這個
然后,我使用System.Security.Cryptography庫進行計算。 但這給了我不同的結果。 在這種情況下,您能幫我嗎?
碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
namespace DesfireCalculation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
byte key_no = 0x00;
byte[] key = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte[] IV = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte[] rndB = new byte[16] { 0x1E,0xA0,0x35,0x3A,0x7D,0x29,0x47,0xD8,0xBB,0xC6,0xAD,0x6F,0xB5,0x2F,0xCA,0x84 };
private void Form1_Load(object sender, EventArgs e)
{
try
{
byte[] res=EncryptStringToBytes_Aes(BitConverter.ToString(rndB), key, IV);
string res_txt = BitConverter.ToString(res);
Console.WriteLine(res_txt);
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
}
static byte[] EncryptStringToBytes_Aes(byte[] Data, byte[] Key, byte[] IV)
{
// Check arguments.
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
aesAlg.Mode = CipherMode.CBC;
aesAlg.BlockSize = 128;
aesAlg.FeedbackSize = 128;
aesAlg.KeySize = 128;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.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(Data);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
}
}
該網站指出:
Input Data (It will be padded with zeroes if necessary.)
填充在加密中非常重要。
因此,請確保您使用的是: aes.Padding = PaddingMode.Zeros;
如果沒有它,您將獲得更長的填充字節結果。
編輯:對於實際情況,您可能應該保留默認值:PKCS#7。 @WimCoenen有一個很好的理由。 檢查評論。
代碼的另一個問題是: 在設置鍵和IV的大小之前,先設置它們。
這是錯誤的 :
aesAlg.Key = Key;
aesAlg.IV = IV;
aesAlg.Mode = CipherMode.CBC;
aesAlg.BlockSize = 128;
aesAlg.FeedbackSize = 128;
aesAlg.KeySize = 128;
這是正確的順序:
aesAlg.Mode = CipherMode.CBC;
aesAlg.KeySize = 128;
aesAlg.BlockSize = 128;
aesAlg.FeedbackSize = 128;
aesAlg.Padding = PaddingMode.Zeros;
aesAlg.Key = key;
aesAlg.IV = iv;
代碼的另一個問題是您正在使用StreamWriter寫入加密流:
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(Data);
}
StreamWriter可能會弄亂一切。 它設計用於編寫具有特定編碼的文本。
在下面的代碼中查看適用於您的情況的我的實現。
public class AesCryptographyService
{
public byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
{
using (var aes = Aes.Create())
{
aes.KeySize = 128;
aes.BlockSize = 128;
aes.Padding = PaddingMode.Zeros;
aes.Key = key;
aes.IV = iv;
using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
{
return PerformCryptography(data, encryptor);
}
}
}
public byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
{
using (var aes = Aes.Create())
{
aes.KeySize = 128;
aes.BlockSize = 128;
aes.Padding = PaddingMode.Zeros;
aes.Key = key;
aes.IV = iv;
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
{
return PerformCryptography(data, decryptor);
}
}
}
private byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)
{
using (var ms = new MemoryStream())
using (var cryptoStream = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))
{
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
return ms.ToArray();
}
}
}
var key = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
var iv = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
var input = new byte[16] { 0x1E,0xA0,0x35,0x3A,0x7D,0x29,0x47,0xD8,0xBB,0xC6,0xAD,0x6F,0xB5,0x2F,0xCA,0x84 };
var crypto = new AesCryptographyService();
var encrypted = crypto.Encrypt(input, key, iv);
var str = BitConverter.ToString(encrypted).Replace("-", "");
Console.WriteLine(str);
它將輸出結果:
C5537C8EFFFCC7E152C27831AFD383BA
與您所引用的網站上的相同:
編輯:
我更改了您的功能,因此它將輸出正確的結果:
static byte[] EncryptStringToBytes_Aes(byte[] data, byte[] key, byte[] iv)
{
// Check arguments.
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("iv");
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Mode = CipherMode.CBC;
aesAlg.KeySize = 128;
aesAlg.BlockSize = 128;
aesAlg.FeedbackSize = 128;
aesAlg.Padding = PaddingMode.Zeros;
aesAlg.Key = key;
aesAlg.IV = iv;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(data, 0, data.Length);
csEncrypt.FlushFinalBlock();
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
輸入數據不同,因此結果也不同。 在站點上,您的純文本為“ C5537C8EFFFCC7E152C27831AFD383BA
”,代碼為“ B969FDFE56FD91FC9DE6F6F213B8FD1E
”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.