AESHelper.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. 
  2. /*************************************************************************************
  3. * 作 者: 季健国
  4. * 说 明:AES CBC模式 128位秘钥 16字节
  5. * QQ : 181589805
  6. *************************************************************************************/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Security.Cryptography;
  12. using System.IO;
  13. namespace Ant.Service.Utility
  14. {
  15. /// <summary>
  16. /// ASE加解密
  17. /// </summary>
  18. public class AESHelper
  19. {
  20. public enum KeySize { Bits128, Bits192, Bits256 }; // key size, in bits, for construtor
  21. private int Nb; // block size in 32-bit words. Always 4 for AES. (128 bits).
  22. private int Nk; // key size in 32-bit words. 4, 6, 8. (128, 192, 256 bits).
  23. private int Nr; // number of rounds. 10, 12, 14.
  24. private byte[] key; // the seed key. size will be 4 * keySize from ctor.
  25. private byte[,] Sbox; // Substitution box
  26. private byte[,] iSbox; // inverse Substitution box
  27. private byte[,] w; // key schedule array.
  28. private byte[,] Rcon; // Round constants.
  29. private byte[,] State; // State matrix
  30. public AESHelper(KeySize keySize, byte[] keyBytes)
  31. {
  32. SetNbNkNr(keySize);
  33. this.key = new byte[this.Nk * 4]; // 16, 24, 32 bytes
  34. //keyBytes.CopyTo(this.key, 0); //改写为讲keyBytes的前Nk*4位存入key,不足的话用0补齐
  35. for (int i = 0; i < Nk * 4; ++i)
  36. if (i < keyBytes.Length)
  37. this.key[i] = keyBytes[i];
  38. else
  39. this.key[i] = 0;
  40. BuildSbox();
  41. BuildInvSbox();
  42. BuildRcon();
  43. KeyExpansion(); // expand the seed key into a key schedule and store in w
  44. } // Aes constructor
  45. public void Cipher(byte[] input, byte[] output) // encipher 16-bit input
  46. {
  47. // state = input
  48. this.State = new byte[4, Nb]; // always [4,4]
  49. for (int i = 0; i < (4 * Nb); ++i)
  50. {
  51. this.State[i % 4, i / 4] = input[i];
  52. }
  53. AddRoundKey(0);
  54. for (int round = 1; round <= (Nr - 1); ++round) // main round loop
  55. {
  56. SubBytes();
  57. ShiftRows();
  58. MixColumns();
  59. AddRoundKey(round);
  60. } // main round loop
  61. SubBytes();
  62. ShiftRows();
  63. AddRoundKey(Nr);
  64. // output = state
  65. for (int i = 0; i < (4 * Nb); ++i)
  66. {
  67. output[i] = this.State[i % 4, i / 4];
  68. }
  69. } // Cipher() 加密 每一轮都经历了 SubBytes, ShiftRows, MixColumns 和 AddRoundKey 四个环节,但是最后一轮没有 MixColumns 环节
  70. public void InvCipher(byte[] input, byte[] output) // decipher 16-bit input
  71. {
  72. // state = input
  73. this.State = new byte[4, Nb]; // always [4,4]
  74. for (int i = 0; i < (4 * Nb); ++i)
  75. {
  76. this.State[i % 4, i / 4] = input[i];
  77. }
  78. AddRoundKey(Nr);
  79. for (int round = Nr - 1; round >= 1; --round) // main round loop
  80. {
  81. InvShiftRows();
  82. InvSubBytes();
  83. AddRoundKey(round);
  84. InvMixColumns();
  85. } // end main round loop for InvCipher
  86. InvShiftRows();
  87. InvSubBytes();
  88. AddRoundKey(0);
  89. // output = state
  90. for (int i = 0; i < (4 * Nb); ++i)
  91. {
  92. output[i] = this.State[i % 4, i / 4];
  93. }
  94. } // InvCipher() 解密 每一轮都经历了 invShiftRows, inSubBytes, AddRoundKey 和 invMixColumns 四个环节,但是最后一轮没有 invMixColumns 环节
  95. private void SetNbNkNr(KeySize keySize)
  96. {
  97. this.Nb = 4; // block size always = 4 words = 16 bytes = 128 bits for AES
  98. if (keySize == KeySize.Bits128)
  99. {
  100. this.Nk = 4; // key size = 4 words = 16 bytes = 128 bits
  101. this.Nr = 10; // rounds for algorithm = 10
  102. }
  103. else if (keySize == KeySize.Bits192)
  104. {
  105. this.Nk = 6; // 6 words = 24 bytes = 192 bits
  106. this.Nr = 12;
  107. }
  108. else if (keySize == KeySize.Bits256)
  109. {
  110. this.Nk = 8; // 8 words = 32 bytes = 256 bits
  111. this.Nr = 14;
  112. }
  113. } // SetNbNkNr()
  114. private void BuildSbox()
  115. {
  116. this.Sbox = new byte[16, 16] { // populate the Sbox matrix
  117. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  118. /*0*/ {0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76},
  119. /*1*/ {0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0},
  120. /*2*/ {0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15},
  121. /*3*/ {0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75},
  122. /*4*/ {0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84},
  123. /*5*/ {0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf},
  124. /*6*/ {0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8},
  125. /*7*/ {0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2},
  126. /*8*/ {0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73},
  127. /*9*/ {0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb},
  128. /*a*/ {0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79},
  129. /*b*/ {0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08},
  130. /*c*/ {0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a},
  131. /*d*/ {0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e},
  132. /*e*/ {0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf},
  133. /*f*/ {0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16} };
  134. } // BuildSbox() 用于加密过程中的字节代换运算
  135. private void BuildInvSbox()
  136. {
  137. this.iSbox = new byte[16, 16] { // populate the iSbox matrix
  138. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  139. /*0*/ {0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb},
  140. /*1*/ {0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb},
  141. /*2*/ {0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e},
  142. /*3*/ {0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25},
  143. /*4*/ {0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92},
  144. /*5*/ {0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84},
  145. /*6*/ {0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06},
  146. /*7*/ {0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b},
  147. /*8*/ {0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73},
  148. /*9*/ {0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e},
  149. /*a*/ {0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b},
  150. /*b*/ {0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4},
  151. /*c*/ {0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f},
  152. /*d*/ {0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef},
  153. /*e*/ {0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61},
  154. /*f*/ {0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d} };
  155. } // BuildInvSbox() 用于解密过程中的逆字节代换运算
  156. private void BuildRcon()
  157. {
  158. this.Rcon = new byte[11, 4] { {0x00, 0x00, 0x00, 0x00},
  159. {0x01, 0x00, 0x00, 0x00},
  160. {0x02, 0x00, 0x00, 0x00},
  161. {0x04, 0x00, 0x00, 0x00},
  162. {0x08, 0x00, 0x00, 0x00},
  163. {0x10, 0x00, 0x00, 0x00},
  164. {0x20, 0x00, 0x00, 0x00},
  165. {0x40, 0x00, 0x00, 0x00},
  166. {0x80, 0x00, 0x00, 0x00},
  167. {0x1b, 0x00, 0x00, 0x00},
  168. {0x36, 0x00, 0x00, 0x00} };
  169. } // BuildRcon()
  170. private void AddRoundKey(int round)
  171. {
  172. for (int r = 0; r < 4; ++r)
  173. {
  174. for (int c = 0; c < 4; ++c)
  175. {
  176. this.State[r, c] = (byte)((int)this.State[r, c] ^ (int)w[(round * 4) + c, r]);
  177. }
  178. }
  179. } // AddRoundKey() 轮密钥加法变换
  180. private void SubBytes()
  181. {
  182. for (int r = 0; r < 4; ++r)
  183. {
  184. for (int c = 0; c < 4; ++c)
  185. {
  186. this.State[r, c] = this.Sbox[(this.State[r, c] >> 4), (this.State[r, c] & 0x0f)];
  187. }
  188. }
  189. } // SubBytes 字节代替变换
  190. private void InvSubBytes()
  191. {
  192. for (int r = 0; r < 4; ++r)
  193. {
  194. for (int c = 0; c < 4; ++c)
  195. {
  196. this.State[r, c] = this.iSbox[(this.State[r, c] >> 4), (this.State[r, c] & 0x0f)];
  197. }
  198. }
  199. } // InvSubBytes 逆字节代替变换
  200. private void ShiftRows()
  201. {
  202. byte[,] temp = new byte[4, 4];
  203. for (int r = 0; r < 4; ++r) // copy State into temp[]
  204. {
  205. for (int c = 0; c < 4; ++c)
  206. {
  207. temp[r, c] = this.State[r, c];
  208. }
  209. }
  210. for (int r = 1; r < 4; ++r) // shift temp into State
  211. {
  212. for (int c = 0; c < 4; ++c)
  213. {
  214. this.State[r, c] = temp[r, (c + r) % Nb];
  215. }
  216. }
  217. } // ShiftRows() 行位移变换
  218. private void InvShiftRows()
  219. {
  220. byte[,] temp = new byte[4, 4];
  221. for (int r = 0; r < 4; ++r) // copy State into temp[]
  222. {
  223. for (int c = 0; c < 4; ++c)
  224. {
  225. temp[r, c] = this.State[r, c];
  226. }
  227. }
  228. for (int r = 1; r < 4; ++r) // shift temp into State
  229. {
  230. for (int c = 0; c < 4; ++c)
  231. {
  232. this.State[r, (c + r) % Nb] = temp[r, c];
  233. }
  234. }
  235. } // InvShiftRows() 逆行移位变换
  236. private void MixColumns()
  237. {
  238. byte[,] temp = new byte[4, 4];
  239. for (int r = 0; r < 4; ++r) // copy State into temp[]
  240. {
  241. for (int c = 0; c < 4; ++c)
  242. {
  243. temp[r, c] = this.State[r, c];
  244. }
  245. }
  246. for (int c = 0; c < 4; ++c)
  247. {
  248. this.State[0, c] = (byte)((int)gfmultby02(temp[0, c]) ^ (int)gfmultby03(temp[1, c]) ^
  249. (int)gfmultby01(temp[2, c]) ^ (int)gfmultby01(temp[3, c]));
  250. this.State[1, c] = (byte)((int)gfmultby01(temp[0, c]) ^ (int)gfmultby02(temp[1, c]) ^
  251. (int)gfmultby03(temp[2, c]) ^ (int)gfmultby01(temp[3, c]));
  252. this.State[2, c] = (byte)((int)gfmultby01(temp[0, c]) ^ (int)gfmultby01(temp[1, c]) ^
  253. (int)gfmultby02(temp[2, c]) ^ (int)gfmultby03(temp[3, c]));
  254. this.State[3, c] = (byte)((int)gfmultby03(temp[0, c]) ^ (int)gfmultby01(temp[1, c]) ^
  255. (int)gfmultby01(temp[2, c]) ^ (int)gfmultby02(temp[3, c]));
  256. }
  257. } // MixColumns() 列混合变换
  258. private void InvMixColumns()
  259. {
  260. byte[,] temp = new byte[4, 4];
  261. for (int r = 0; r < 4; ++r) // copy State into temp[]
  262. {
  263. for (int c = 0; c < 4; ++c)
  264. {
  265. temp[r, c] = this.State[r, c];
  266. }
  267. }
  268. for (int c = 0; c < 4; ++c)
  269. {
  270. this.State[0, c] = (byte)((int)gfmultby0e(temp[0, c]) ^ (int)gfmultby0b(temp[1, c]) ^
  271. (int)gfmultby0d(temp[2, c]) ^ (int)gfmultby09(temp[3, c]));
  272. this.State[1, c] = (byte)((int)gfmultby09(temp[0, c]) ^ (int)gfmultby0e(temp[1, c]) ^
  273. (int)gfmultby0b(temp[2, c]) ^ (int)gfmultby0d(temp[3, c]));
  274. this.State[2, c] = (byte)((int)gfmultby0d(temp[0, c]) ^ (int)gfmultby09(temp[1, c]) ^
  275. (int)gfmultby0e(temp[2, c]) ^ (int)gfmultby0b(temp[3, c]));
  276. this.State[3, c] = (byte)((int)gfmultby0b(temp[0, c]) ^ (int)gfmultby0d(temp[1, c]) ^
  277. (int)gfmultby09(temp[2, c]) ^ (int)gfmultby0e(temp[3, c]));
  278. }
  279. } // InvMixColumns() 逆列混合变换
  280. private static byte gfmultby01(byte b)
  281. {
  282. return b;
  283. }
  284. private static byte gfmultby02(byte b)
  285. {
  286. if (b < 0x80)
  287. return (byte)(int)(b << 1);
  288. else
  289. return (byte)((int)(b << 1) ^ (int)(0x1b));
  290. }
  291. private static byte gfmultby03(byte b)
  292. {
  293. return (byte)((int)gfmultby02(b) ^ (int)b);
  294. }
  295. private static byte gfmultby09(byte b)
  296. {
  297. return (byte)((int)gfmultby02(gfmultby02(gfmultby02(b))) ^
  298. (int)b);
  299. }
  300. private static byte gfmultby0b(byte b)
  301. {
  302. return (byte)((int)gfmultby02(gfmultby02(gfmultby02(b))) ^
  303. (int)gfmultby02(b) ^
  304. (int)b);
  305. }
  306. private static byte gfmultby0d(byte b)
  307. {
  308. return (byte)((int)gfmultby02(gfmultby02(gfmultby02(b))) ^
  309. (int)gfmultby02(gfmultby02(b)) ^
  310. (int)(b));
  311. }
  312. private static byte gfmultby0e(byte b)
  313. {
  314. return (byte)((int)gfmultby02(gfmultby02(gfmultby02(b))) ^
  315. (int)gfmultby02(gfmultby02(b)) ^
  316. (int)gfmultby02(b));
  317. }
  318. private void KeyExpansion()
  319. {
  320. this.w = new byte[Nb * (Nr + 1), 4]; // 4 columns of bytes corresponds to a word
  321. for (int row = 0; row < Nk; ++row)
  322. {
  323. this.w[row, 0] = this.key[4 * row];
  324. this.w[row, 1] = this.key[4 * row + 1];
  325. this.w[row, 2] = this.key[4 * row + 2];
  326. this.w[row, 3] = this.key[4 * row + 3];
  327. }
  328. byte[] temp = new byte[4];
  329. for (int row = Nk; row < Nb * (Nr + 1); ++row)
  330. {
  331. temp[0] = this.w[row - 1, 0]; temp[1] = this.w[row - 1, 1];
  332. temp[2] = this.w[row - 1, 2]; temp[3] = this.w[row - 1, 3];
  333. if (row % Nk == 0)
  334. {
  335. temp = SubWord(RotWord(temp));
  336. temp[0] = (byte)((int)temp[0] ^ (int)this.Rcon[row / Nk, 0]);
  337. temp[1] = (byte)((int)temp[1] ^ (int)this.Rcon[row / Nk, 1]);
  338. temp[2] = (byte)((int)temp[2] ^ (int)this.Rcon[row / Nk, 2]);
  339. temp[3] = (byte)((int)temp[3] ^ (int)this.Rcon[row / Nk, 3]);
  340. }
  341. else if (Nk > 6 && (row % Nk == 4))
  342. {
  343. temp = SubWord(temp);
  344. }
  345. // w[row] = w[row-Nk] xor temp
  346. this.w[row, 0] = (byte)((int)this.w[row - Nk, 0] ^ (int)temp[0]);
  347. this.w[row, 1] = (byte)((int)this.w[row - Nk, 1] ^ (int)temp[1]);
  348. this.w[row, 2] = (byte)((int)this.w[row - Nk, 2] ^ (int)temp[2]);
  349. this.w[row, 3] = (byte)((int)this.w[row - Nk, 3] ^ (int)temp[3]);
  350. } // for loop
  351. } // KeyExpansion() 密钥扩展
  352. private byte[] SubWord(byte[] word)
  353. {
  354. byte[] result = new byte[4];
  355. result[0] = this.Sbox[word[0] >> 4, word[0] & 0x0f];
  356. result[1] = this.Sbox[word[1] >> 4, word[1] & 0x0f];
  357. result[2] = this.Sbox[word[2] >> 4, word[2] & 0x0f];
  358. result[3] = this.Sbox[word[3] >> 4, word[3] & 0x0f];
  359. return result;
  360. }
  361. private byte[] RotWord(byte[] word)
  362. {
  363. byte[] result = new byte[4];
  364. result[0] = word[1];
  365. result[1] = word[2];
  366. result[2] = word[3];
  367. result[3] = word[0];
  368. return result;
  369. }
  370. public void Dump()
  371. {
  372. //Console.WriteLine("Nb = " + Nb + " Nk = " + Nk + " Nr = " + Nr);
  373. //Console.WriteLine("\nThe key is \n" + DumpKey() );
  374. //Console.WriteLine("\nThe Sbox is \n" + DumpTwoByTwo(Sbox));
  375. //Console.WriteLine("\nThe w array is \n" + DumpTwoByTwo(w));
  376. //Console.WriteLine("\nThe State array is \n" + DumpTwoByTwo(State));
  377. }
  378. public string DumpKey()
  379. {
  380. string s = "";
  381. for (int i = 0; i < key.Length; ++i)
  382. s += key[i].ToString("x2") + " ";
  383. return s;
  384. }
  385. public string DumpTwoByTwo(byte[,] a)
  386. {
  387. string s = "";
  388. for (int r = 0; r < a.GetLength(0); ++r)
  389. {
  390. s += "[" + r + "]" + " ";
  391. for (int c = 0; c < a.GetLength(1); ++c)
  392. {
  393. s += a[r, c].ToString("x2") + " ";
  394. }
  395. s += "\n";
  396. }
  397. return s;
  398. }
  399. } // class Aes
  400. }