Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, November 21, 2016 10:37 PM
I have a couple questions with regards to AES encryption in C# (.NET 4.5.2). I've implemented decryption using AES-CBC 256-bit. However, the system that's going to be encrypting the data and sending it to me is only providing a hexadecimal string as a key. My .NET code is expecting a byte array and I'm not entirely sure what the right way to coerce a hex->byte[] would be when it comes to an encryption key.
They're also saying that their code does not implement an initialization vector. I've read that it's not possible to implement AES-CBC without an IV in .NET, but I wanted to verify that as well. It will apparently take a lot of effort to make the change to use an IV so I want to be sure before I ask for that to be done.
Example of their key:
1B2CACE7C347B723B0D202D3A0EBA2E1A6A8B4EFA38224E9121CFAD8A0882C99
Example of my key:
103,104,67,140,151,68,73,127,23,241,254,108,59,85,54,1,148,56,247,208,177,233,139,193,5,207,174,223,97,32,44,3
Any help would be greatly appreciated!
Nathon Dalton
Sr. Lead Developer
Blog: http://www.nathondalton.com
All replies (2)
Tuesday, November 22, 2016 9:45 AM âś…Answered
Hi Nathon Dalton,
Thank you for posting here.
***>> coerce a hex->byte[] ***
Please try the following code.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hex_to_Byte
{
class Program
{
static void Main(string[] args)
{
string input = "1B2CACE7C347B723B0D202D3A0EBA2E1A6A8B4EFA38224E9121CFAD8A0882C99";
byte[] b = HexadecimalStringToByteArray(input);
foreach (var item in b)
{
Console.Write(item + ",");
}
Console.ReadKey();
}
public static byte[] HexadecimalStringToByteArray(string input)
{
var outputLength = input.Length / 2;
var output = new byte[outputLength];
using (var sr = new StringReader(input))
{
for (var i = 0; i < outputLength; i++)
output[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
}
return output;
}
}
}
Here is the output.

>> For IV, please refer to the links.
Good AES Initialization Vector practice.
I hope this would be helpful to you.
If you have something else, please feel free to contact us.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Tuesday, November 22, 2016 5:10 PM
I've put the code in place. It's going to take me a while to validate that it works with the AES key. In the mean time, thank you for the help!
Nathon Dalton
Sr. Lead Developer
Blog: http://www.nathondalton.com