Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Aes 256 bit Padding
Message
De
24/08/2009 14:13:23
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Cryptographie
Titre:
Aes 256 bit Padding
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP SP2
Divers
Thread ID:
01420259
Message ID:
01420259
Vues:
105
I think when padding, the length of the last block should be exactly equal to the blockSize.

I have got more than 20 cases where the last block is 128 bits and yet the blockSize = 256 bits

Now - I'm currently on an old notebook - XP sp3, Net 3.5 framework ( Vista down and dell don't have the parts - haha)

Does this seem like a bug ? I think it does


Anyone wants to run it - I've put in two cases

Thanks,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Security.Cryptography;
using System.Diagnostics;


namespace BaseTest
{
	//______________________________________________________________________
	public class OneCase
	{
		public int BlockSize;
		public CipherMode Mode;
		public PaddingMode Padding;
		public byte[] Key;
		public byte[] IV;
		public byte[] Input;

	}
	//______________________________________________________________________
	class PaddingTest
	{
		//______________________________________________________________________
		static void Main()
		{
			

			List<OneCase> cases = GetCases();

			MemoryStream memOut;


			foreach( var one in cases )
				using (var algo = Rijndael.Create())
				{
					algo.BlockSize = one.BlockSize;
					algo.Mode = one.Mode;
					algo.Padding = one.Padding;
					algo.Key = one.Key;
					algo.IV = one.IV;

					memOut = new MemoryStream();

					using (memOut)
					{
						using (var cryptor = algo.CreateEncryptor())
						{
							using (var xx = new CryptoStream(memOut, cryptor, CryptoStreamMode.Write))
							{
								xx.Write(one.Input, 0, one.Input.Length);
								xx.FlushFinalBlock();

							}
						}
					}


					byte[] encryptyed = memOut.ToArray();

					if (encryptyed.Length != (algo.BlockSize >> 3))
					{
						Print(algo, one.Input, encryptyed);
					}
				}
			Console.ReadLine();
	
		}

		//______________________________________________________________________
		private static void Print(Rijndael algo, byte[] input, byte[] encryptyed)
		{
			Console.WriteLine("BlockSize: {0}", algo.BlockSize);
			Console.WriteLine("CipherMode: {0}", algo.Mode);
			Console.WriteLine("PaddingMode: {0}", algo.Padding);
			Console.WriteLine("Key: {0}", ToHex(algo.Key));
			Console.WriteLine("IV: {0}", ToHex(algo.IV));
			Console.WriteLine("Input: {0}", ToHex(input));
			Console.WriteLine("Output: {0}", ToHex(encryptyed));

			Console.WriteLine();

		}
		//______________________________________________________________________
		private static List<OneCase> GetCases()
		{
			List<OneCase> cases = new List<OneCase>();

			cases.Add(new OneCase
					{	BlockSize = 256,
						Mode = CipherMode.CFB,
						Padding = PaddingMode.PKCS7,
						Key = FromHex("75fb331b6481cc5d6585ad66842e00cd"),
						IV = FromHex("8aadbc991d7eb527d9f10f46318f22712eeec7c48ca4c3f52de45ce635521794"),
						Input = FromHex("")
					}
					);
			cases.Add(
					new OneCase
					{	BlockSize = 256,
						Mode = CipherMode.CFB,
						Padding = PaddingMode.ANSIX923,
						Key = FromHex("13e49e8a382457c87505b5505bda00c4871f079586bddfa1"),
						IV = FromHex("6bd3535c3e438489006ded73b6fead4ed1a28c4672a943bd4f170e72278d80df"),
						Input = FromHex("8c")
					}

				);

			return cases;
		}
		//______________________________________________________________________
		static byte[] FromHex(string s)
		{
			if( (s.Length & 0x01) != 0 )
				throw new ArgumentException("Length must be even");

			byte[] hex = new byte[s.Length >> 1];

			var bb = new byte[s.Length >> 1];

			for (int i = 0, j = 0; i < s.Length; i += 2, j++)
			{
				bb[j] = Byte.Parse(s.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
			}

			return bb;

		}
		//______________________________________________________________________
		static string ToHex(byte[] b)
		{
			var sb = new StringBuilder(b.Length << 1);
			var upperBound = b.GetUpperBound(0);

			for (int i = b.GetLowerBound(0); i <= upperBound; i++)
				sb.Append(b[i].ToString("x2"));

			return sb.ToString();

		}
		//______________________________________________________________________
		


	}
}
Gregory
Répondre
Fil
Voir

Click here to load this message in the networking platform