C# AES Encryption Byte Array

21,589

The input data is different hence the result is different as well. On the site your plaintext is 'C5537C8EFFFCC7E152C27831AFD383BA' and in the code is 'B969FDFE56FD91FC9DE6F6F213B8FD1E'

Share:
21,589
BK52
Author by

BK52

Updated on December 07, 2020

Comments

  • BK52
    BK52 over 3 years

    I want to encrypt byte array. So first I try it in this site.

    • key = 00000000000000000000000000000000
    • IV = 00000000000000000000000000000000
    • Input Data = 1EA0353A7D2947D8BBC6AD6FB52FCA84
    • Type = CBC

    It calculated this

    • Encrypted Output = C5537C8EFFFCC7E152C27831AFD383BA

    Then I use System.Security.Cryptography library and calculate it. But it gives me different result. Could you help me for that situation?

    Code

    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;    
            }
        }
    }
    
  • BK52
    BK52 over 5 years
    thanks for interest and attention. I edited the code and example. But there is still different result for example and my code.