簡體   English   中英

用於加密單個塊的AES顯示錯誤

[英]AES to encrypt a single block shows error

當在程序下面運行它顯示錯誤。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Your code goes here
            Console.WriteLine("Hello, world!");

            byte[] plainText = UTF8Encoding.UTF8.GetBytes("textt");
            byte[] key = UTF8Encoding.UTF8.GetBytes("1234567890123456");
            AES_encrypt_block(plainText,key);
        }
        private static byte[] AES_encrypt_block(byte[] plainText, byte[] Key)
        {
        byte[] output_buffer = new byte[plainText.Length];

        using (AesManaged aesAlg = new AesManaged())
        {
        //If CBC, must initialize IV = O_{128}
        //aesAlg.Mode = CipherMode.CBC;
        //aesAlg.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        aesAlg.Mode = CipherMode.ECB;

        aesAlg.BlockSize = 128;
        aesAlg.KeySize = 128;
        aesAlg.Padding = PaddingMode.None;
        aesAlg.Key = Key;

        // Create a decrytor to perform the stream transform.
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
        encryptor.TransformBlock(plainText, 0, plainText.Length, output_buffer, 0);
        }

        return output_buffer;
    }
    }

}

錯誤

用戶代碼中的異常:

System.ArgumentException:值無效。 at> System.Security.Cryptography.RijndaelManagedTransform.TransformBlock(Byte []> inputBuffer,Int32 inputOffset,Int32 inputCount,Byte [] outputBuffer,Int32> outputOffset)at Rextester.Program.AES_encrypt_block(Byte [] plainText,Byte [] Key)在Rextester.Program.Main(String [] args)“

你可以這樣做(例子)。 但我建議使用CBC而不是ECB。 並且,由於填充,解密的塊將始終具有塊的大小,即使原始內容(5字節字符串)更短。 因此,在創建字符串時,請在實際內容之前插入字符串長度(第2個示例)。

加密/解密ECB:

private static AesManaged Create(byte[] key)
{
    var aes = new AesManaged();

    aes.Mode = CipherMode.ECB;
    aes.BlockSize = 128;
    aes.KeySize = 128;
    aes.Padding = PaddingMode.Zeros;
    aes.Key = key;

    return aes;
}
private static byte[] AES_encrypt_block(byte[] plainText, byte[] key)
{
    using (AesManaged aes = Create(key))
    {
        var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
        return encryptor.TransformFinalBlock(plainText, 0, plainText.Length);
    }
}
private static byte[] AES_decrypt_block(byte[] plainText, byte[] key)
{
    using (AesManaged aesAlg = Create(key))
    {
        var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
        return decryptor.TransformFinalBlock(plainText, 0, plainText.Length);
    }
}

串到字節:

private static byte[] StringToBytes(string s)
{
    var length = BitConverter.GetBytes(s.Length);
    var raw = Encoding.UTF8.GetBytes(s);
    var full = new byte[sizeof(int) + raw.Length];

    Array.Copy(length, full, sizeof(int));
    Array.Copy(raw, 0, full, sizeof(int), raw.Length);

    return full;
}
private static string BytesToString(byte[] input)
{
    var length = BitConverter.ToInt32(input, 0);

    return Encoding.UTF8.GetString(input, sizeof(int), length);
}

驗證:

var key = UTF8Encoding.UTF8.GetBytes("1234567890123456");
var plainBytes = StringToBytes("textt");
var encryptedBytes = AES_encrypt_block(plainBytes, key);
var decryptedBytes = AES_decrypt_block(encryptedBytes, key);
var decryptedString = BytesToString(decryptedBytes);

你應該改變它;

encryptor.TransformBlock(plainText, 0, plainText.Length, output_buffer, 0);

var output = encryptor.TransformFinalBlock(plainText, 0, plainText.Length);

你應該指定PaddingMode ;

aesAlg.Padding = PaddingMode.PKCS7;

整個方法看起來像;

private static byte[] AES_encrypt_block(byte[] plainText, byte[] Key)
{
    using (AesManaged aesAlg = new AesManaged())
    {
        aesAlg.Mode = CipherMode.ECB;
        aesAlg.BlockSize = 128;
        aesAlg.KeySize = 128;
        aesAlg.Padding = PaddingMode.PKCS7;
        aesAlg.Key = Key;

        // Create a decrytor to perform the stream transform.
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
        return encryptor.TransformFinalBlock(plainText, 0, plainText.Length);
    }
}

如果使用TransformBlock ,則輸入文本長度應為16 因為它改變了每個塊的加密。 因此,您應該使用TransformFinalBlock而不是它。 此外,應為較短的消息數據塊設置PaddingMode

編輯

執行解密 ;

private static byte[] AES_Decrypt_block(byte[] cipherText, byte[] Key)
{
    using (AesManaged aesAlg = new AesManaged())
    {
        aesAlg.Mode = CipherMode.ECB;
        aesAlg.BlockSize = 128;
        aesAlg.KeySize = 128;
        aesAlg.Padding = PaddingMode.PKCS7;
        aesAlg.Key = Key;
        ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
        return decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length);
    }
}

用法

byte[] plainText = UTF8Encoding.UTF8.GetBytes("textt");
byte[] key = UTF8Encoding.UTF8.GetBytes("1234567890123456");
var encrypted = AES_encrypt_block(plainText, key);
var decrypted = AES_Decrypt_block(encrypted, key);
Console.WriteLine(UTF8Encoding.UTF8.GetString(decrypted));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM