AES Encrypt
The Advanced Encryption Standard (AES) also known as Rijndael is a specification for the encryption of electronic data.
For AES Decrypt algorithm click here.
Private Shared aes_sbox As Byte(,) = {{&H63, &H7C, &H77, &H7B, &HF2, &H6B,
&H6F, &HC5, &H30, &H1, &H67, &H2B,
&HFE, &HD7, &HAB, &H76}, {&HCA, &H82, &HC9, &H7D, &HFA, &H59,
&H47, &HF0, &HAD, &HD4, &HA2, &HAF,
&H9C, &HA4, &H72, &HC0}, {&HB7, &HFD, &H93, &H26, &H36, &H3F,
&HF7, &HCC, &H34, &HA5, &HE5, &HF1,
&H71, &HD8, &H31, &H15}, {&H4, &HC7, &H23, &HC3, &H18, &H96,
&H5, &H9A, &H7, &H12, &H80, &HE2,
&HEB, &H27, &HB2, &H75}, {&H9, &H83, &H2C, &H1A, &H1B, &H6E,
&H5A, &HA0, &H52, &H3B, &HD6, &HB3,
&H29, &HE3, &H2F, &H84}, {&H53, &HD1, &H0, &HED, &H20, &HFC,
&HB1, &H5B, &H6A, &HCB, &HBE, &H39,
&H4A, &H4C, &H58, &HCF},
{&HD0, &HEF, &HAA, &HFB, &H43, &H4D,
&H33, &H85, &H45, &HF9, &H2, &H7F,
&H50, &H3C, &H9F, &HA8}, {&H51, &HA3, &H40, &H8F, &H92, &H9D,
&H38, &HF5, &HBC, &HB6, &HDA, &H21,
&H10, &HFF, &HF3, &HD2}, {&HCD, &HC, &H13, &HEC, &H5F, &H97,
&H44, &H17, &HC4, &HA7, &H7E, &H3D,
&H64, &H5D, &H19, &H73}, {&H60, &H81, &H4F, &HDC, &H22, &H2A,
&H90, &H88, &H46, &HEE, &HB8, &H14,
&HDE, &H5E, &HB, &HDB}, {&HE0, &H32, &H3A, &HA, &H49, &H6,
&H24, &H5C, &HC2, &HD3, &HAC, &H62,
&H91, &H95, &HE4, &H79}, {&HE7, &HC8, &H37, &H6D, &H8D, &HD5,
&H4E, &HA9, &H6C, &H56, &HF4, &HEA,
&H65, &H7A, &HAE, &H8},
{&HBA, &H78, &H25, &H2E, &H1C, &HA6,
&HB4, &HC6, &HE8, &HDD, &H74, &H1F,
&H4B, &HBD, &H8B, &H8A}, {&H70, &H3E, &HB5, &H66, &H48, &H3,
&HF6, &HE, &H61, &H35, &H57, &HB9,
&H86, &HC1, &H1D, &H9E}, {&HE1, &HF8, &H98, &H11, &H69, &HD9,
&H8E, &H94, &H9B, &H1E, &H87, &HE9,
&HCE, &H55, &H28, &HDF}, {&H8C, &HA1, &H89, &HD, &HBF, &HE6,
&H42, &H68, &H41, &H99, &H2D, &HF,
&HB0, &H54, &HBB, &H16}}
Private Shared gf_mul As Byte(,) = {{&H0, &H0, &H0, &H0, &H0, &H0}, {&H2, &H3, &H9, &HB, &HD, &HE}, {&H4, &H6, &H12, &H16, &H1A, &H1C}, {&H6, &H5, &H1B, &H1D, &H17, &H12}, {&H8, &HC, &H24, &H2C, &H34, &H38}, {&HA, &HF, &H2D, &H27, &H39, &H36},
{&HC, &HA, &H36, &H3A, &H2E, &H24}, {&HE, &H9, &H3F, &H31, &H23, &H2A}, {&H10, &H18, &H48, &H58, &H68, &H70}, {&H12, &H1B, &H41, &H53, &H65, &H7E}, {&H14, &H1E, &H5A, &H4E, &H72, &H6C}, {&H16, &H1D, &H53, &H45, &H7F, &H62},
{&H18, &H14, &H6C, &H74, &H5C, &H48}, {&H1A, &H17, &H65, &H7F, &H51, &H46}, {&H1C, &H12, &H7E, &H62, &H46, &H54}, {&H1E, &H11, &H77, &H69, &H4B, &H5A}, {&H20, &H30, &H90, &HB0, &HD0, &HE0}, {&H22, &H33, &H99, &HBB, &HDD, &HEE},
{&H24, &H36, &H82, &HA6, &HCA, &HFC}, {&H26, &H35, &H8B, &HAD, &HC7, &HF2}, {&H28, &H3C, &HB4, &H9C, &HE4, &HD8}, {&H2A, &H3F, &HBD, &H97, &HE9, &HD6}, {&H2C, &H3A, &HA6, &H8A, &HFE, &HC4}, {&H2E, &H39, &HAF, &H81, &HF3, &HCA},
{&H30, &H28, &HD8, &HE8, &HB8, &H90}, {&H32, &H2B, &HD1, &HE3, &HB5, &H9E}, {&H34, &H2E, &HCA, &HFE, &HA2, &H8C}, {&H36, &H2D, &HC3, &HF5, &HAF, &H82}, {&H38, &H24, &HFC, &HC4, &H8C, &HA8}, {&H3A, &H27, &HF5, &HCF, &H81, &HA6},
{&H3C, &H22, &HEE, &HD2, &H96, &HB4}, {&H3E, &H21, &HE7, &HD9, &H9B, &HBA}, {&H40, &H60, &H3B, &H7B, &HBB, &HDB}, {&H42, &H63, &H32, &H70, &HB6, &HD5}, {&H44, &H66, &H29, &H6D, &HA1, &HC7}, {&H46, &H65, &H20, &H66, &HAC, &HC9},
{&H48, &H6C, &H1F, &H57, &H8F, &HE3}, {&H4A, &H6F, &H16, &H5C, &H82, &HED}, {&H4C, &H6A, &HD, &H41, &H95, &HFF}, {&H4E, &H69, &H4, &H4A, &H98, &HF1}, {&H50, &H78, &H73, &H23, &HD3, &HAB}, {&H52, &H7B, &H7A, &H28, &HDE, &HA5},
{&H54, &H7E, &H61, &H35, &HC9, &HB7}, {&H56, &H7D, &H68, &H3E, &HC4, &HB9}, {&H58, &H74, &H57, &HF, &HE7, &H93}, {&H5A, &H77, &H5E, &H4, &HEA, &H9D}, {&H5C, &H72, &H45, &H19, &HFD, &H8F}, {&H5E, &H71, &H4C, &H12, &HF0, &H81},
{&H60, &H50, &HAB, &HCB, &H6B, &H3B}, {&H62, &H53, &HA2, &HC0, &H66, &H35}, {&H64, &H56, &HB9, &HDD, &H71, &H27}, {&H66, &H55, &HB0, &HD6, &H7C, &H29}, {&H68, &H5C, &H8F, &HE7, &H5F, &H3}, {&H6A, &H5F, &H86, &HEC, &H52, &HD},
{&H6C, &H5A, &H9D, &HF1, &H45, &H1F}, {&H6E, &H59, &H94, &HFA, &H48, &H11}, {&H70, &H48, &HE3, &H93, &H3, &H4B}, {&H72, &H4B, &HEA, &H98, &HE, &H45}, {&H74, &H4E, &HF1, &H85, &H19, &H57}, {&H76, &H4D, &HF8, &H8E, &H14, &H59},
{&H78, &H44, &HC7, &HBF, &H37, &H73}, {&H7A, &H47, &HCE, &HB4, &H3A, &H7D}, {&H7C, &H42, &HD5, &HA9, &H2D, &H6F}, {&H7E, &H41, &HDC, &HA2, &H20, &H61}, {&H80, &HC0, &H76, &HF6, &H6D, &HAD}, {&H82, &HC3, &H7F, &HFD, &H60, &HA3},
{&H84, &HC6, &H64, &HE0, &H77, &HB1}, {&H86, &HC5, &H6D, &HEB, &H7A, &HBF}, {&H88, &HCC, &H52, &HDA, &H59, &H95}, {&H8A, &HCF, &H5B, &HD1, &H54, &H9B}, {&H8C, &HCA, &H40, &HCC, &H43, &H89}, {&H8E, &HC9, &H49, &HC7, &H4E, &H87},
{&H90, &HD8, &H3E, &HAE, &H5, &HDD}, {&H92, &HDB, &H37, &HA5, &H8, &HD3}, {&H94, &HDE, &H2C, &HB8, &H1F, &HC1}, {&H96, &HDD, &H25, &HB3, &H12, &HCF}, {&H98, &HD4, &H1A, &H82, &H31, &HE5}, {&H9A, &HD7, &H13, &H89, &H3C, &HEB},
{&H9C, &HD2, &H8, &H94, &H2B, &HF9}, {&H9E, &HD1, &H1, &H9F, &H26, &HF7}, {&HA0, &HF0, &HE6, &H46, &HBD, &H4D}, {&HA2, &HF3, &HEF, &H4D, &HB0, &H43}, {&HA4, &HF6, &HF4, &H50, &HA7, &H51}, {&HA6, &HF5, &HFD, &H5B, &HAA, &H5F},
{&HA8, &HFC, &HC2, &H6A, &H89, &H75}, {&HAA, &HFF, &HCB, &H61, &H84, &H7B}, {&HAC, &HFA, &HD0, &H7C, &H93, &H69}, {&HAE, &HF9, &HD9, &H77, &H9E, &H67}, {&HB0, &HE8, &HAE, &H1E, &HD5, &H3D}, {&HB2, &HEB, &HA7, &H15, &HD8, &H33},
{&HB4, &HEE, &HBC, &H8, &HCF, &H21}, {&HB6, &HED, &HB5, &H3, &HC2, &H2F}, {&HB8, &HE4, &H8A, &H32, &HE1, &H5}, {&HBA, &HE7, &H83, &H39, &HEC, &HB}, {&HBC, &HE2, &H98, &H24, &HFB, &H19}, {&HBE, &HE1, &H91, &H2F, &HF6, &H17},
{&HC0, &HA0, &H4D, &H8D, &HD6, &H76}, {&HC2, &HA3, &H44, &H86, &HDB, &H78}, {&HC4, &HA6, &H5F, &H9B, &HCC, &H6A}, {&HC6, &HA5, &H56, &H90, &HC1, &H64}, {&HC8, &HAC, &H69, &HA1, &HE2, &H4E}, {&HCA, &HAF, &H60, &HAA, &HEF, &H40},
{&HCC, &HAA, &H7B, &HB7, &HF8, &H52}, {&HCE, &HA9, &H72, &HBC, &HF5, &H5C}, {&HD0, &HB8, &H5, &HD5, &HBE, &H6}, {&HD2, &HBB, &HC, &HDE, &HB3, &H8}, {&HD4, &HBE, &H17, &HC3, &HA4, &H1A}, {&HD6, &HBD, &H1E, &HC8, &HA9, &H14},
{&HD8, &HB4, &H21, &HF9, &H8A, &H3E}, {&HDA, &HB7, &H28, &HF2, &H87, &H30}, {&HDC, &HB2, &H33, &HEF, &H90, &H22}, {&HDE, &HB1, &H3A, &HE4, &H9D, &H2C}, {&HE0, &H90, &HDD, &H3D, &H6, &H96}, {&HE2, &H93, &HD4, &H36, &HB, &H98},
{&HE4, &H96, &HCF, &H2B, &H1C, &H8A}, {&HE6, &H95, &HC6, &H20, &H11, &H84}, {&HE8, &H9C, &HF9, &H11, &H32, &HAE}, {&HEA, &H9F, &HF0, &H1A, &H3F, &HA0}, {&HEC, &H9A, &HEB, &H7, &H28, &HB2}, {&HEE, &H99, &HE2, &HC, &H25, &HBC},
{&HF0, &H88, &H95, &H65, &H6E, &HE6}, {&HF2, &H8B, &H9C, &H6E, &H63, &HE8}, {&HF4, &H8E, &H87, &H73, &H74, &HFA}, {&HF6, &H8D, &H8E, &H78, &H79, &HF4}, {&HF8, &H84, &HB1, &H49, &H5A, &HDE}, {&HFA, &H87, &HB8, &H42, &H57, &HD0},
{&HFC, &H82, &HA3, &H5F, &H40, &HC2}, {&HFE, &H81, &HAA, &H54, &H4D, &HCC}, {&H1B, &H9B, &HEC, &HF7, &HDA, &H41}, {&H19, &H98, &HE5, &HFC, &HD7, &H4F}, {&H1F, &H9D, &HFE, &HE1, &HC0, &H5D}, {&H1D, &H9E, &HF7, &HEA, &HCD, &H53},
{&H13, &H97, &HC8, &HDB, &HEE, &H79}, {&H11, &H94, &HC1, &HD0, &HE3, &H77}, {&H17, &H91, &HDA, &HCD, &HF4, &H65}, {&H15, &H92, &HD3, &HC6, &HF9, &H6B}, {&HB, &H83, &HA4, &HAF, &HB2, &H31}, {&H9, &H80, &HAD, &HA4, &HBF, &H3F},
{&HF, &H85, &HB6, &HB9, &HA8, &H2D}, {&HD, &H86, &HBF, &HB2, &HA5, &H23}, {&H3, &H8F, &H80, &H83, &H86, &H9}, {&H1, &H8C, &H89, &H88, &H8B, &H7}, {&H7, &H89, &H92, &H95, &H9C, &H15}, {&H5, &H8A, &H9B, &H9E, &H91, &H1B},
{&H3B, &HAB, &H7C, &H47, &HA, &HA1}, {&H39, &HA8, &H75, &H4C, &H7, &HAF}, {&H3F, &HAD, &H6E, &H51, &H10, &HBD}, {&H3D, &HAE, &H67, &H5A, &H1D, &HB3}, {&H33, &HA7, &H58, &H6B, &H3E, &H99}, {&H31, &HA4, &H51, &H60, &H33, &H97},
{&H37, &HA1, &H4A, &H7D, &H24, &H85}, {&H35, &HA2, &H43, &H76, &H29, &H8B}, {&H2B, &HB3, &H34, &H1F, &H62, &HD1}, {&H29, &HB0, &H3D, &H14, &H6F, &HDF}, {&H2F, &HB5, &H26, &H9, &H78, &HCD}, {&H2D, &HB6, &H2F, &H2, &H75, &HC3},
{&H23, &HBF, &H10, &H33, &H56, &HE9}, {&H21, &HBC, &H19, &H38, &H5B, &HE7}, {&H27, &HB9, &H2, &H25, &H4C, &HF5}, {&H25, &HBA, &HB, &H2E, &H41, &HFB}, {&H5B, &HFB, &HD7, &H8C, &H61, &H9A}, {&H59, &HF8, &HDE, &H87, &H6C, &H94},
{&H5F, &HFD, &HC5, &H9A, &H7B, &H86}, {&H5D, &HFE, &HCC, &H91, &H76, &H88}, {&H53, &HF7, &HF3, &HA0, &H55, &HA2}, {&H51, &HF4, &HFA, &HAB, &H58, &HAC}, {&H57, &HF1, &HE1, &HB6, &H4F, &HBE}, {&H55, &HF2, &HE8, &HBD, &H42, &HB0},
{&H4B, &HE3, &H9F, &HD4, &H9, &HEA}, {&H49, &HE0, &H96, &HDF, &H4, &HE4}, {&H4F, &HE5, &H8D, &HC2, &H13, &HF6}, {&H4D, &HE6, &H84, &HC9, &H1E, &HF8}, {&H43, &HEF, &HBB, &HF8, &H3D, &HD2}, {&H41, &HEC, &HB2, &HF3, &H30, &HDC},
{&H47, &HE9, &HA9, &HEE, &H27, &HCE}, {&H45, &HEA, &HA0, &HE5, &H2A, &HC0}, {&H7B, &HCB, &H47, &H3C, &HB1, &H7A}, {&H79, &HC8, &H4E, &H37, &HBC, &H74}, {&H7F, &HCD, &H55, &H2A, &HAB, &H66}, {&H7D, &HCE, &H5C, &H21, &HA6, &H68},
{&H73, &HC7, &H63, &H10, &H85, &H42}, {&H71, &HC4, &H6A, &H1B, &H88, &H4C}, {&H77, &HC1, &H71, &H6, &H9F, &H5E}, {&H75, &HC2, &H78, &HD, &H92, &H50}, {&H6B, &HD3, &HF, &H64, &HD9, &HA}, {&H69, &HD0, &H6, &H6F, &HD4, &H4},
{&H6F, &HD5, &H1D, &H72, &HC3, &H16}, {&H6D, &HD6, &H14, &H79, &HCE, &H18}, {&H63, &HDF, &H2B, &H48, &HED, &H32}, {&H61, &HDC, &H22, &H43, &HE0, &H3C}, {&H67, &HD9, &H39, &H5E, &HF7, &H2E}, {&H65, &HDA, &H30, &H55, &HFA, &H20},
{&H9B, &H5B, &H9A, &H1, &HB7, &HEC}, {&H99, &H58, &H93, &HA, &HBA, &HE2}, {&H9F, &H5D, &H88, &H17, &HAD, &HF0}, {&H9D, &H5E, &H81, &H1C, &HA0, &HFE}, {&H93, &H57, &HBE, &H2D, &H83, &HD4}, {&H91, &H54, &HB7, &H26, &H8E, &HDA},
{&H97, &H51, &HAC, &H3B, &H99, &HC8}, {&H95, &H52, &HA5, &H30, &H94, &HC6}, {&H8B, &H43, &HD2, &H59, &HDF, &H9C}, {&H89, &H40, &HDB, &H52, &HD2, &H92}, {&H8F, &H45, &HC0, &H4F, &HC5, &H80}, {&H8D, &H46, &HC9, &H44, &HC8, &H8E},
{&H83, &H4F, &HF6, &H75, &HEB, &HA4}, {&H81, &H4C, &HFF, &H7E, &HE6, &HAA}, {&H87, &H49, &HE4, &H63, &HF1, &HB8}, {&H85, &H4A, &HED, &H68, &HFC, &HB6}, {&HBB, &H6B, &HA, &HB1, &H67, &HC}, {&HB9, &H68, &H3, &HBA, &H6A, &H2},
{&HBF, &H6D, &H18, &HA7, &H7D, &H10}, {&HBD, &H6E, &H11, &HAC, &H70, &H1E}, {&HB3, &H67, &H2E, &H9D, &H53, &H34}, {&HB1, &H64, &H27, &H96, &H5E, &H3A}, {&HB7, &H61, &H3C, &H8B, &H49, &H28}, {&HB5, &H62, &H35, &H80, &H44, &H26},
{&HAB, &H73, &H42, &HE9, &HF, &H7C}, {&HA9, &H70, &H4B, &HE2, &H2, &H72}, {&HAF, &H75, &H50, &HFF, &H15, &H60}, {&HAD, &H76, &H59, &HF4, &H18, &H6E}, {&HA3, &H7F, &H66, &HC5, &H3B, &H44}, {&HA1, &H7C, &H6F, &HCE, &H36, &H4A},
{&HA7, &H79, &H74, &HD3, &H21, &H58}, {&HA5, &H7A, &H7D, &HD8, &H2C, &H56}, {&HDB, &H3B, &HA1, &H7A, &HC, &H37}, {&HD9, &H38, &HA8, &H71, &H1, &H39}, {&HDF, &H3D, &HB3, &H6C, &H16, &H2B}, {&HDD, &H3E, &HBA, &H67, &H1B, &H25},
{&HD3, &H37, &H85, &H56, &H38, &HF}, {&HD1, &H34, &H8C, &H5D, &H35, &H1}, {&HD7, &H31, &H97, &H40, &H22, &H13}, {&HD5, &H32, &H9E, &H4B, &H2F, &H1D}, {&HCB, &H23, &HE9, &H22, &H64, &H47}, {&HC9, &H20, &HE0, &H29, &H69, &H49},
{&HCF, &H25, &HFB, &H34, &H7E, &H5B}, {&HCD, &H26, &HF2, &H3F, &H73, &H55}, {&HC3, &H2F, &HCD, &HE, &H50, &H7F}, {&HC1, &H2C, &HC4, &H5, &H5D, &H71}, {&HC7, &H29, &HDF, &H18, &H4A, &H63}, {&HC5, &H2A, &HD6, &H13, &H47, &H6D},
{&HFB, &HB, &H31, &HCA, &HDC, &HD7}, {&HF9, &H8, &H38, &HC1, &HD1, &HD9}, {&HFF, &HD, &H23, &HDC, &HC6, &HCB}, {&HFD, &HE, &H2A, &HD7, &HCB, &HC5}, {&HF3, &H7, &H15, &HE6, &HE8, &HEF}, {&HF1, &H4, &H1C, &HED, &HE5, &HE1},
{&HF7, &H1, &H7, &HF0, &HF2, &HF3}, {&HF5, &H2, &HE, &HFB, &HFF, &HFD}, {&HEB, &H13, &H79, &H92, &HB4, &HA7}, {&HE9, &H10, &H70, &H99, &HB9, &HA9}, {&HEF, &H15, &H6B, &H84, &HAE, &HBB}, {&HED, &H16, &H62, &H8F, &HA3, &HB5},
{&HE3, &H1F, &H5D, &HBE, &H80, &H9F}, {&HE1, &H1C, &H54, &HB5, &H8D, &H91}, {&HE7, &H19, &H4F, &HA8, &H9A, &H83}, {&HE5, &H1A, &H46, &HA3, &H97, &H8D}}
Private Shared Function SubArray(Of T)(data As T(), index As Integer, Optional length As Integer = -1) As T()
If length = -1 Then
length = data.Length - index
End If
Dim result As T() = New T(length - 1) {}
Array.Copy(data, index, result, 0, length)
Return result
End Function
Private Shared Sub AddRoundKey(state As Byte(,), w As UInteger())
Dim subkey As Byte() = New Byte(3) {}
subkey(0) = CLng(w(0) >> 24) And Byte.MaxValue
subkey(1) = CLng(w(0) >> 16) And Byte.MaxValue
subkey(2) = CLng(w(0) >> 8) And Byte.MaxValue
subkey(3) = CLng(w(0)) And Byte.MaxValue
state(0, 0) = state(0, 0) Xor subkey(0)
state(1, 0) = state(1, 0) Xor subkey(1)
state(2, 0) = state(2, 0) Xor subkey(2)
state(3, 0) = state(3, 0) Xor subkey(3)
subkey(0) = CLng(w(1) >> 24) And Byte.MaxValue
subkey(1) = CLng(w(1) >> 16) And Byte.MaxValue
subkey(2) = CLng(w(1) >> 8) And Byte.MaxValue
subkey(3) = CLng(w(1)) And Byte.MaxValue
state(0, 1) = state(0, 1) Xor subkey(0)
state(1, 1) = state(1, 1) Xor subkey(1)
state(2, 1) = state(2, 1) Xor subkey(2)
state(3, 1) = state(3, 1) Xor subkey(3)
subkey(0) = CLng(w(2) >> 24) And Byte.MaxValue
subkey(1) = CLng(w(2) >> 16) And Byte.MaxValue
subkey(2) = CLng(w(2) >> 8) And Byte.MaxValue
subkey(3) = CLng(w(2)) And Byte.MaxValue
state(0, 2) = state(0, 2) Xor subkey(0)
state(1, 2) = state(1, 2) Xor subkey(1)
state(2, 2) = state(2, 2) Xor subkey(2)
state(3, 2) = state(3, 2) Xor subkey(3)
subkey(0) = CLng(w(3) >> 24) And Byte.MaxValue
subkey(1) = CLng(w(3) >> 16) And Byte.MaxValue
subkey(2) = CLng(w(3) >> 8) And Byte.MaxValue
subkey(3) = CLng(w(3)) And Byte.MaxValue
state(0, 3) = state(0, 3) Xor subkey(0)
state(1, 3) = state(1, 3) Xor subkey(1)
state(2, 3) = state(2, 3) Xor subkey(2)
state(3, 3) = state(3, 3) Xor subkey(3)
End Sub
Private Shared Sub SubBytes(state As Byte(,))
state(0, 0) = aes_sbox(state(0, 0) >> 4, state(0, 0) And &HF)
state(0, 1) = aes_sbox(state(0, 1) >> 4, state(0, 1) And &HF)
state(0, 2) = aes_sbox(state(0, 2) >> 4, state(0, 2) And &HF)
state(0, 3) = aes_sbox(state(0, 3) >> 4, state(0, 3) And &HF)
state(1, 0) = aes_sbox(state(1, 0) >> 4, state(1, 0) And &HF)
state(1, 1) = aes_sbox(state(1, 1) >> 4, state(1, 1) And &HF)
state(1, 2) = aes_sbox(state(1, 2) >> 4, state(1, 2) And &HF)
state(1, 3) = aes_sbox(state(1, 3) >> 4, state(1, 3) And &HF)
state(2, 0) = aes_sbox(state(2, 0) >> 4, state(2, 0) And &HF)
state(2, 1) = aes_sbox(state(2, 1) >> 4, state(2, 1) And &HF)
state(2, 2) = aes_sbox(state(2, 2) >> 4, state(2, 2) And &HF)
state(2, 3) = aes_sbox(state(2, 3) >> 4, state(2, 3) And &HF)
state(3, 0) = aes_sbox(state(3, 0) >> 4, state(3, 0) And &HF)
state(3, 1) = aes_sbox(state(3, 1) >> 4, state(3, 1) And &HF)
state(3, 2) = aes_sbox(state(3, 2) >> 4, state(3, 2) And &HF)
state(3, 3) = aes_sbox(state(3, 3) >> 4, state(3, 3) And &HF)
End Sub
Private Shared Sub ShiftRows(state As Byte(,))
Dim t As Byte
t = state(1, 0)
state(1, 0) = state(1, 1)
state(1, 1) = state(1, 2)
state(1, 2) = state(1, 3)
state(1, 3) = t
t = state(2, 0)
state(2, 0) = state(2, 2)
state(2, 2) = t
t = state(2, 1)
state(2, 1) = state(2, 3)
state(2, 3) = t
t = state(3, 0)
state(3, 0) = state(3, 3)
state(3, 3) = state(3, 2)
state(3, 2) = state(3, 1)
state(3, 1) = t
End Sub
Private Shared Sub MixColumns(state As Byte(,))
Dim col As Byte() = New Byte(3) {}
col(0) = state(0, 0)
col(1) = state(1, 0)
col(2) = state(2, 0)
col(3) = state(3, 0)
state(0, 0) = gf_mul(col(0), 0)
state(0, 0) = state(0, 0) Xor gf_mul(col(1), 1)
state(0, 0) = state(0, 0) Xor col(2)
state(0, 0) = state(0, 0) Xor col(3)
state(1, 0) = col(0)
state(1, 0) = state(1, 0) Xor gf_mul(col(1), 0)
state(1, 0) = state(1, 0) Xor gf_mul(col(2), 1)
state(1, 0) = state(1, 0) Xor col(3)
state(2, 0) = col(0)
state(2, 0) = state(2, 0) Xor col(1)
state(2, 0) = state(2, 0) Xor gf_mul(col(2), 0)
state(2, 0) = state(2, 0) Xor gf_mul(col(3), 1)
state(3, 0) = gf_mul(col(0), 1)
state(3, 0) = state(3, 0) Xor col(1)
state(3, 0) = state(3, 0) Xor col(2)
state(3, 0) = state(3, 0) Xor gf_mul(col(3), 0)
col(0) = state(0, 1)
col(1) = state(1, 1)
col(2) = state(2, 1)
col(3) = state(3, 1)
state(0, 1) = gf_mul(col(0), 0)
state(0, 1) = state(0, 1) Xor gf_mul(col(1), 1)
state(0, 1) = state(0, 1) Xor col(2)
state(0, 1) = state(0, 1) Xor col(3)
state(1, 1) = col(0)
state(1, 1) = state(1, 1) Xor gf_mul(col(1), 0)
state(1, 1) = state(1, 1) Xor gf_mul(col(2), 1)
state(1, 1) = state(1, 1) Xor col(3)
state(2, 1) = col(0)
state(2, 1) = state(2, 1) Xor col(1)
state(2, 1) = state(2, 1) Xor gf_mul(col(2), 0)
state(2, 1) = state(2, 1) Xor gf_mul(col(3), 1)
state(3, 1) = gf_mul(col(0), 1)
state(3, 1) = state(3, 1) Xor col(1)
state(3, 1) = state(3, 1) Xor col(2)
state(3, 1) = state(3, 1) Xor gf_mul(col(3), 0)
col(0) = state(0, 2)
col(1) = state(1, 2)
col(2) = state(2, 2)
col(3) = state(3, 2)
state(0, 2) = gf_mul(col(0), 0)
state(0, 2) = state(0, 2) Xor gf_mul(col(1), 1)
state(0, 2) = state(0, 2) Xor col(2)
state(0, 2) = state(0, 2) Xor col(3)
state(1, 2) = col(0)
state(1, 2) = state(1, 2) Xor gf_mul(col(1), 0)
state(1, 2) = state(1, 2) Xor gf_mul(col(2), 1)
state(1, 2) = state(1, 2) Xor col(3)
state(2, 2) = col(0)
state(2, 2) = state(2, 2) Xor col(1)
state(2, 2) = state(2, 2) Xor gf_mul(col(2), 0)
state(2, 2) = state(2, 2) Xor gf_mul(col(3), 1)
state(3, 2) = gf_mul(col(0), 1)
state(3, 2) = state(3, 2) Xor col(1)
state(3, 2) = state(3, 2) Xor col(2)
state(3, 2) = state(3, 2) Xor gf_mul(col(3), 0)
col(0) = state(0, 3)
col(1) = state(1, 3)
col(2) = state(2, 3)
col(3) = state(3, 3)
state(0, 3) = gf_mul(col(0), 0)
state(0, 3) = state(0, 3) Xor gf_mul(col(1), 1)
state(0, 3) = state(0, 3) Xor col(2)
state(0, 3) = state(0, 3) Xor col(3)
state(1, 3) = col(0)
state(1, 3) = state(1, 3) Xor gf_mul(col(1), 0)
state(1, 3) = state(1, 3) Xor gf_mul(col(2), 1)
state(1, 3) = state(1, 3) Xor col(3)
state(2, 3) = col(0)
state(2, 3) = state(2, 3) Xor col(1)
state(2, 3) = state(2, 3) Xor gf_mul(col(2), 0)
state(2, 3) = state(2, 3) Xor gf_mul(col(3), 1)
state(3, 3) = gf_mul(col(0), 1)
state(3, 3) = state(3, 3) Xor col(1)
state(3, 3) = state(3, 3) Xor col(2)
state(3, 3) = state(3, 3) Xor gf_mul(col(3), 0)
End Sub
Private Shared Function SubWord(word As UInteger) As UInteger
Dim result As UInteger
result = aes_sbox((word >> 4) And &HF, word And &HF)
result += CUInt(aes_sbox((word >> 12) And &HF, (word >> 8) And &HF)) << 8
result += CUInt(aes_sbox((word >> 20) And &HF, (word >> 16) And &HF)) << 16
result += CUInt(aes_sbox((word >> 28) And &HF, (word >> 24) And &HF)) << 24
Return (result)
End Function
Private Shared Function KE_ROTWORD(x As UInteger) As UInteger
Return (((x) << 8) Or ((x) >> 24))
End Function
Public Shared Sub KeyExpansion(key As Byte(), w As UInteger(), keysize As Integer)
Dim Nb As Integer = 4, Nr As Integer, Nk As Integer, idx As Integer
Dim temp As UInteger
Dim Rcon As UInteger() = {&H1000000, &H2000000, &H4000000, &H8000000, &H10000000, &H20000000,
&H40000000, &H80000000UI, &H1B000000, &H36000000, &H6C000000, &HD8000000UI,
&HAB000000UI, &H4D000000, &H9A000000UI}
Select Case keysize
Case 128
Nr = 10
Nk = 4
Exit Select
Case 192
Nr = 12
Nk = 6
Exit Select
Case 256
Nr = 14
Nk = 8
Exit Select
Case Else
Return
End Select
For idx = 0 To Nk - 1
w(idx) = (CInt(key(4 * idx)) << 24) Or (CInt(key(4 * idx + 1)) << 16) Or
(CInt(key(4 * idx + 2)) << 8) Or (key(4 * idx + 3))
Next
For idx = Nk To Nb * (Nr + 1) - 1
temp = w(idx - 1)
If (idx Mod Nk) = 0 Then
temp = SubWord(KE_ROTWORD(temp)) Xor Rcon((idx - 1) \ Nk)
ElseIf Nk > 6 AndAlso (idx Mod Nk) = 4 Then
temp = SubWord(temp)
End If
w(idx) = w(idx - Nk) Xor temp
Next
End Sub
Public Shared Sub Encrypt(input As Byte(), output As Byte(), key As UInteger(), keysize As Integer)
Dim state As Byte(,) = New Byte(3, 3) {}
state(0, 0) = input(0)
state(1, 0) = input(1)
state(2, 0) = input(2)
state(3, 0) = input(3)
state(0, 1) = input(4)
state(1, 1) = input(5)
state(2, 1) = input(6)
state(3, 1) = input(7)
state(0, 2) = input(8)
state(1, 2) = input(9)
state(2, 2) = input(10)
state(3, 2) = input(11)
state(0, 3) = input(12)
state(1, 3) = input(13)
state(2, 3) = input(14)
state(3, 3) = input(15)
AddRoundKey(state, key)
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 4))
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 8))
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 12))
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 16))
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 20))
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 24))
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 28))
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 32))
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 36))
If keysize <> 128 Then
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 40))
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 44))
If keysize <> 192 Then
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 48))
SubBytes(state)
ShiftRows(state)
MixColumns(state)
AddRoundKey(state, SubArray(key, 52))
SubBytes(state)
ShiftRows(state)
AddRoundKey(state, SubArray(key, 56))
Else
SubBytes(state)
ShiftRows(state)
AddRoundKey(state, SubArray(key, 48))
End If
Else
SubBytes(state)
ShiftRows(state)
AddRoundKey(state, SubArray(key, 40))
End If
output(0) = state(0, 0)
output(1) = state(1, 0)
output(2) = state(2, 0)
output(3) = state(3, 0)
output(4) = state(0, 1)
output(5) = state(1, 1)
output(6) = state(2, 1)
output(7) = state(3, 1)
output(8) = state(0, 2)
output(9) = state(1, 2)
output(10) = state(2, 2)
output(11) = state(3, 2)
output(12) = state(0, 3)
output(13) = state(1, 3)
output(14) = state(2, 3)
output(15) = state(3, 3)
End Sub
Example
Dim plaintext As Byte() = {&H0, &H11, &H22, &H33, &H44, &H55,
&H66, &H77, &H88, &H99, &HAA, &HBB,
&HCC, &HDD, &HEE, &HFF}
Dim key As Byte() = {&H0, &H1, &H2, &H3, &H4, &H5,
&H6, &H7, &H8, &H9, &HA, &HB,
&HC, &HD, &HE, &HF, &H10, &H11,
&H12, &H13, &H14, &H15, &H16, &H17,
&H18, &H19, &H1A, &H1B, &H1C, &H1D,
&H1E, &H1F}
Dim ciphertext(15) As Byte
Dim key_schedule As UInteger() = New UInteger(59) {}
KeyExpansion(key, key_schedule, 256)
Encrypt(plaintext, ciphertext, key_schedule, 256)
For idx As Integer = 0 To 15
Console.Write("{0:x2}", ciphertext(idx))
Next
Output
8ea2b7ca516745bfeafc49904b496089