MyAES.pas 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. unit MyAES;
  2. {
  3. author:ChenJingTao.
  4. create date:2006,7,8.
  5. 说明:本文件参考ELAES的代码和例子,把流加密解密用指针实现,使效率成倍提高.
  6. 注意:本单元只实现128位加密,192和256之类还有扩展密钥没有做.如果需要移植,
  7. 切记仔细看ELAES的详细代码.切记!
  8. }
  9. interface
  10. function CalculateAESEncryptBuffer128Size(const InSize: Longint): Longint;//计算加密后的大小
  11. function AESEncryptBuffer128(dest: PChar;
  12. destLen: PLongint;
  13. const source: PChar;
  14. const sourceLen: Longint;
  15. const strKey: string): integer;//加密Buff,成功返回0,失败返回-1
  16. function AESDecryptBuffer128(dest: Pchar;
  17. destLen: PLongint;
  18. const source: Pchar;
  19. const sourceLen: Longint;
  20. const strKey: string): integer;//解密Buff,成功返回0,失败返回-1
  21. function AESEncryptStr128(const strIn, strKey: string): string;
  22. function AESDecryptStr128(const strIn, strKey: string): string;
  23. implementation
  24. function Min(A, B: integer): integer;
  25. begin
  26. if A < B then
  27. Result := A
  28. else
  29. Result := B;
  30. end;
  31. type
  32. TAESBuffer = array [0..15] of byte;
  33. PAESBuffer=^TAESBuffer;
  34. TAESKey128 = array [0..15] of byte;
  35. TAESExpandedKey128 = array [0..43] of longword;
  36. const
  37. Rcon: array [1..30] of longword = (
  38. $00000001, $00000002, $00000004, $00000008, $00000010, $00000020,
  39. $00000040, $00000080, $0000001B, $00000036, $0000006C, $000000D8,
  40. $000000AB, $0000004D, $0000009A, $0000002F, $0000005E, $000000BC,
  41. $00000063, $000000C6, $00000097, $00000035, $0000006A, $000000D4,
  42. $000000B3, $0000007D, $000000FA, $000000EF, $000000C5, $00000091
  43. );
  44. ForwardTable: array [0..255] of longword = (
  45. $A56363C6, $847C7CF8, $997777EE, $8D7B7BF6, $0DF2F2FF, $BD6B6BD6, $B16F6FDE, $54C5C591,
  46. $50303060, $03010102, $A96767CE, $7D2B2B56, $19FEFEE7, $62D7D7B5, $E6ABAB4D, $9A7676EC,
  47. $45CACA8F, $9D82821F, $40C9C989, $877D7DFA, $15FAFAEF, $EB5959B2, $C947478E, $0BF0F0FB,
  48. $ECADAD41, $67D4D4B3, $FDA2A25F, $EAAFAF45, $BF9C9C23, $F7A4A453, $967272E4, $5BC0C09B,
  49. $C2B7B775, $1CFDFDE1, $AE93933D, $6A26264C, $5A36366C, $413F3F7E, $02F7F7F5, $4FCCCC83,
  50. $5C343468, $F4A5A551, $34E5E5D1, $08F1F1F9, $937171E2, $73D8D8AB, $53313162, $3F15152A,
  51. $0C040408, $52C7C795, $65232346, $5EC3C39D, $28181830, $A1969637, $0F05050A, $B59A9A2F,
  52. $0907070E, $36121224, $9B80801B, $3DE2E2DF, $26EBEBCD, $6927274E, $CDB2B27F, $9F7575EA,
  53. $1B090912, $9E83831D, $742C2C58, $2E1A1A34, $2D1B1B36, $B26E6EDC, $EE5A5AB4, $FBA0A05B,
  54. $F65252A4, $4D3B3B76, $61D6D6B7, $CEB3B37D, $7B292952, $3EE3E3DD, $712F2F5E, $97848413,
  55. $F55353A6, $68D1D1B9, $00000000, $2CEDEDC1, $60202040, $1FFCFCE3, $C8B1B179, $ED5B5BB6,
  56. $BE6A6AD4, $46CBCB8D, $D9BEBE67, $4B393972, $DE4A4A94, $D44C4C98, $E85858B0, $4ACFCF85,
  57. $6BD0D0BB, $2AEFEFC5, $E5AAAA4F, $16FBFBED, $C5434386, $D74D4D9A, $55333366, $94858511,
  58. $CF45458A, $10F9F9E9, $06020204, $817F7FFE, $F05050A0, $443C3C78, $BA9F9F25, $E3A8A84B,
  59. $F35151A2, $FEA3A35D, $C0404080, $8A8F8F05, $AD92923F, $BC9D9D21, $48383870, $04F5F5F1,
  60. $DFBCBC63, $C1B6B677, $75DADAAF, $63212142, $30101020, $1AFFFFE5, $0EF3F3FD, $6DD2D2BF,
  61. $4CCDCD81, $140C0C18, $35131326, $2FECECC3, $E15F5FBE, $A2979735, $CC444488, $3917172E,
  62. $57C4C493, $F2A7A755, $827E7EFC, $473D3D7A, $AC6464C8, $E75D5DBA, $2B191932, $957373E6,
  63. $A06060C0, $98818119, $D14F4F9E, $7FDCDCA3, $66222244, $7E2A2A54, $AB90903B, $8388880B,
  64. $CA46468C, $29EEEEC7, $D3B8B86B, $3C141428, $79DEDEA7, $E25E5EBC, $1D0B0B16, $76DBDBAD,
  65. $3BE0E0DB, $56323264, $4E3A3A74, $1E0A0A14, $DB494992, $0A06060C, $6C242448, $E45C5CB8,
  66. $5DC2C29F, $6ED3D3BD, $EFACAC43, $A66262C4, $A8919139, $A4959531, $37E4E4D3, $8B7979F2,
  67. $32E7E7D5, $43C8C88B, $5937376E, $B76D6DDA, $8C8D8D01, $64D5D5B1, $D24E4E9C, $E0A9A949,
  68. $B46C6CD8, $FA5656AC, $07F4F4F3, $25EAEACF, $AF6565CA, $8E7A7AF4, $E9AEAE47, $18080810,
  69. $D5BABA6F, $887878F0, $6F25254A, $722E2E5C, $241C1C38, $F1A6A657, $C7B4B473, $51C6C697,
  70. $23E8E8CB, $7CDDDDA1, $9C7474E8, $211F1F3E, $DD4B4B96, $DCBDBD61, $868B8B0D, $858A8A0F,
  71. $907070E0, $423E3E7C, $C4B5B571, $AA6666CC, $D8484890, $05030306, $01F6F6F7, $120E0E1C,
  72. $A36161C2, $5F35356A, $F95757AE, $D0B9B969, $91868617, $58C1C199, $271D1D3A, $B99E9E27,
  73. $38E1E1D9, $13F8F8EB, $B398982B, $33111122, $BB6969D2, $70D9D9A9, $898E8E07, $A7949433,
  74. $B69B9B2D, $221E1E3C, $92878715, $20E9E9C9, $49CECE87, $FF5555AA, $78282850, $7ADFDFA5,
  75. $8F8C8C03, $F8A1A159, $80898909, $170D0D1A, $DABFBF65, $31E6E6D7, $C6424284, $B86868D0,
  76. $C3414182, $B0999929, $772D2D5A, $110F0F1E, $CBB0B07B, $FC5454A8, $D6BBBB6D, $3A16162C
  77. );
  78. LastForwardTable: array [0..255] of longword = (
  79. $00000063, $0000007C, $00000077, $0000007B, $000000F2, $0000006B, $0000006F, $000000C5,
  80. $00000030, $00000001, $00000067, $0000002B, $000000FE, $000000D7, $000000AB, $00000076,
  81. $000000CA, $00000082, $000000C9, $0000007D, $000000FA, $00000059, $00000047, $000000F0,
  82. $000000AD, $000000D4, $000000A2, $000000AF, $0000009C, $000000A4, $00000072, $000000C0,
  83. $000000B7, $000000FD, $00000093, $00000026, $00000036, $0000003F, $000000F7, $000000CC,
  84. $00000034, $000000A5, $000000E5, $000000F1, $00000071, $000000D8, $00000031, $00000015,
  85. $00000004, $000000C7, $00000023, $000000C3, $00000018, $00000096, $00000005, $0000009A,
  86. $00000007, $00000012, $00000080, $000000E2, $000000EB, $00000027, $000000B2, $00000075,
  87. $00000009, $00000083, $0000002C, $0000001A, $0000001B, $0000006E, $0000005A, $000000A0,
  88. $00000052, $0000003B, $000000D6, $000000B3, $00000029, $000000E3, $0000002F, $00000084,
  89. $00000053, $000000D1, $00000000, $000000ED, $00000020, $000000FC, $000000B1, $0000005B,
  90. $0000006A, $000000CB, $000000BE, $00000039, $0000004A, $0000004C, $00000058, $000000CF,
  91. $000000D0, $000000EF, $000000AA, $000000FB, $00000043, $0000004D, $00000033, $00000085,
  92. $00000045, $000000F9, $00000002, $0000007F, $00000050, $0000003C, $0000009F, $000000A8,
  93. $00000051, $000000A3, $00000040, $0000008F, $00000092, $0000009D, $00000038, $000000F5,
  94. $000000BC, $000000B6, $000000DA, $00000021, $00000010, $000000FF, $000000F3, $000000D2,
  95. $000000CD, $0000000C, $00000013, $000000EC, $0000005F, $00000097, $00000044, $00000017,
  96. $000000C4, $000000A7, $0000007E, $0000003D, $00000064, $0000005D, $00000019, $00000073,
  97. $00000060, $00000081, $0000004F, $000000DC, $00000022, $0000002A, $00000090, $00000088,
  98. $00000046, $000000EE, $000000B8, $00000014, $000000DE, $0000005E, $0000000B, $000000DB,
  99. $000000E0, $00000032, $0000003A, $0000000A, $00000049, $00000006, $00000024, $0000005C,
  100. $000000C2, $000000D3, $000000AC, $00000062, $00000091, $00000095, $000000E4, $00000079,
  101. $000000E7, $000000C8, $00000037, $0000006D, $0000008D, $000000D5, $0000004E, $000000A9,
  102. $0000006C, $00000056, $000000F4, $000000EA, $00000065, $0000007A, $000000AE, $00000008,
  103. $000000BA, $00000078, $00000025, $0000002E, $0000001C, $000000A6, $000000B4, $000000C6,
  104. $000000E8, $000000DD, $00000074, $0000001F, $0000004B, $000000BD, $0000008B, $0000008A,
  105. $00000070, $0000003E, $000000B5, $00000066, $00000048, $00000003, $000000F6, $0000000E,
  106. $00000061, $00000035, $00000057, $000000B9, $00000086, $000000C1, $0000001D, $0000009E,
  107. $000000E1, $000000F8, $00000098, $00000011, $00000069, $000000D9, $0000008E, $00000094,
  108. $0000009B, $0000001E, $00000087, $000000E9, $000000CE, $00000055, $00000028, $000000DF,
  109. $0000008C, $000000A1, $00000089, $0000000D, $000000BF, $000000E6, $00000042, $00000068,
  110. $00000041, $00000099, $0000002D, $0000000F, $000000B0, $00000054, $000000BB, $00000016
  111. );
  112. InverseTable: array [0..255] of longword = (
  113. $50A7F451, $5365417E, $C3A4171A, $965E273A, $CB6BAB3B, $F1459D1F, $AB58FAAC, $9303E34B,
  114. $55FA3020, $F66D76AD, $9176CC88, $254C02F5, $FCD7E54F, $D7CB2AC5, $80443526, $8FA362B5,
  115. $495AB1DE, $671BBA25, $980EEA45, $E1C0FE5D, $02752FC3, $12F04C81, $A397468D, $C6F9D36B,
  116. $E75F8F03, $959C9215, $EB7A6DBF, $DA595295, $2D83BED4, $D3217458, $2969E049, $44C8C98E,
  117. $6A89C275, $78798EF4, $6B3E5899, $DD71B927, $B64FE1BE, $17AD88F0, $66AC20C9, $B43ACE7D,
  118. $184ADF63, $82311AE5, $60335197, $457F5362, $E07764B1, $84AE6BBB, $1CA081FE, $942B08F9,
  119. $58684870, $19FD458F, $876CDE94, $B7F87B52, $23D373AB, $E2024B72, $578F1FE3, $2AAB5566,
  120. $0728EBB2, $03C2B52F, $9A7BC586, $A50837D3, $F2872830, $B2A5BF23, $BA6A0302, $5C8216ED,
  121. $2B1CCF8A, $92B479A7, $F0F207F3, $A1E2694E, $CDF4DA65, $D5BE0506, $1F6234D1, $8AFEA6C4,
  122. $9D532E34, $A055F3A2, $32E18A05, $75EBF6A4, $39EC830B, $AAEF6040, $069F715E, $51106EBD,
  123. $F98A213E, $3D06DD96, $AE053EDD, $46BDE64D, $B58D5491, $055DC471, $6FD40604, $FF155060,
  124. $24FB9819, $97E9BDD6, $CC434089, $779ED967, $BD42E8B0, $888B8907, $385B19E7, $DBEEC879,
  125. $470A7CA1, $E90F427C, $C91E84F8, $00000000, $83868009, $48ED2B32, $AC70111E, $4E725A6C,
  126. $FBFF0EFD, $5638850F, $1ED5AE3D, $27392D36, $64D90F0A, $21A65C68, $D1545B9B, $3A2E3624,
  127. $B1670A0C, $0FE75793, $D296EEB4, $9E919B1B, $4FC5C080, $A220DC61, $694B775A, $161A121C,
  128. $0ABA93E2, $E52AA0C0, $43E0223C, $1D171B12, $0B0D090E, $ADC78BF2, $B9A8B62D, $C8A91E14,
  129. $8519F157, $4C0775AF, $BBDD99EE, $FD607FA3, $9F2601F7, $BCF5725C, $C53B6644, $347EFB5B,
  130. $7629438B, $DCC623CB, $68FCEDB6, $63F1E4B8, $CADC31D7, $10856342, $40229713, $2011C684,
  131. $7D244A85, $F83DBBD2, $1132F9AE, $6DA129C7, $4B2F9E1D, $F330B2DC, $EC52860D, $D0E3C177,
  132. $6C16B32B, $99B970A9, $FA489411, $2264E947, $C48CFCA8, $1A3FF0A0, $D82C7D56, $EF903322,
  133. $C74E4987, $C1D138D9, $FEA2CA8C, $360BD498, $CF81F5A6, $28DE7AA5, $268EB7DA, $A4BFAD3F,
  134. $E49D3A2C, $0D927850, $9BCC5F6A, $62467E54, $C2138DF6, $E8B8D890, $5EF7392E, $F5AFC382,
  135. $BE805D9F, $7C93D069, $A92DD56F, $B31225CF, $3B99ACC8, $A77D1810, $6E639CE8, $7BBB3BDB,
  136. $097826CD, $F418596E, $01B79AEC, $A89A4F83, $656E95E6, $7EE6FFAA, $08CFBC21, $E6E815EF,
  137. $D99BE7BA, $CE366F4A, $D4099FEA, $D67CB029, $AFB2A431, $31233F2A, $3094A5C6, $C066A235,
  138. $37BC4E74, $A6CA82FC, $B0D090E0, $15D8A733, $4A9804F1, $F7DAEC41, $0E50CD7F, $2FF69117,
  139. $8DD64D76, $4DB0EF43, $544DAACC, $DF0496E4, $E3B5D19E, $1B886A4C, $B81F2CC1, $7F516546,
  140. $04EA5E9D, $5D358C01, $737487FA, $2E410BFB, $5A1D67B3, $52D2DB92, $335610E9, $1347D66D,
  141. $8C61D79A, $7A0CA137, $8E14F859, $893C13EB, $EE27A9CE, $35C961B7, $EDE51CE1, $3CB1477A,
  142. $59DFD29C, $3F73F255, $79CE1418, $BF37C773, $EACDF753, $5BAAFD5F, $146F3DDF, $86DB4478,
  143. $81F3AFCA, $3EC468B9, $2C342438, $5F40A3C2, $72C31D16, $0C25E2BC, $8B493C28, $41950DFF,
  144. $7101A839, $DEB30C08, $9CE4B4D8, $90C15664, $6184CB7B, $70B632D5, $745C6C48, $4257B8D0
  145. );
  146. LastInverseTable: array [0..255] of longword = (
  147. $00000052, $00000009, $0000006A, $000000D5, $00000030, $00000036, $000000A5, $00000038,
  148. $000000BF, $00000040, $000000A3, $0000009E, $00000081, $000000F3, $000000D7, $000000FB,
  149. $0000007C, $000000E3, $00000039, $00000082, $0000009B, $0000002F, $000000FF, $00000087,
  150. $00000034, $0000008E, $00000043, $00000044, $000000C4, $000000DE, $000000E9, $000000CB,
  151. $00000054, $0000007B, $00000094, $00000032, $000000A6, $000000C2, $00000023, $0000003D,
  152. $000000EE, $0000004C, $00000095, $0000000B, $00000042, $000000FA, $000000C3, $0000004E,
  153. $00000008, $0000002E, $000000A1, $00000066, $00000028, $000000D9, $00000024, $000000B2,
  154. $00000076, $0000005B, $000000A2, $00000049, $0000006D, $0000008B, $000000D1, $00000025,
  155. $00000072, $000000F8, $000000F6, $00000064, $00000086, $00000068, $00000098, $00000016,
  156. $000000D4, $000000A4, $0000005C, $000000CC, $0000005D, $00000065, $000000B6, $00000092,
  157. $0000006C, $00000070, $00000048, $00000050, $000000FD, $000000ED, $000000B9, $000000DA,
  158. $0000005E, $00000015, $00000046, $00000057, $000000A7, $0000008D, $0000009D, $00000084,
  159. $00000090, $000000D8, $000000AB, $00000000, $0000008C, $000000BC, $000000D3, $0000000A,
  160. $000000F7, $000000E4, $00000058, $00000005, $000000B8, $000000B3, $00000045, $00000006,
  161. $000000D0, $0000002C, $0000001E, $0000008F, $000000CA, $0000003F, $0000000F, $00000002,
  162. $000000C1, $000000AF, $000000BD, $00000003, $00000001, $00000013, $0000008A, $0000006B,
  163. $0000003A, $00000091, $00000011, $00000041, $0000004F, $00000067, $000000DC, $000000EA,
  164. $00000097, $000000F2, $000000CF, $000000CE, $000000F0, $000000B4, $000000E6, $00000073,
  165. $00000096, $000000AC, $00000074, $00000022, $000000E7, $000000AD, $00000035, $00000085,
  166. $000000E2, $000000F9, $00000037, $000000E8, $0000001C, $00000075, $000000DF, $0000006E,
  167. $00000047, $000000F1, $0000001A, $00000071, $0000001D, $00000029, $000000C5, $00000089,
  168. $0000006F, $000000B7, $00000062, $0000000E, $000000AA, $00000018, $000000BE, $0000001B,
  169. $000000FC, $00000056, $0000003E, $0000004B, $000000C6, $000000D2, $00000079, $00000020,
  170. $0000009A, $000000DB, $000000C0, $000000FE, $00000078, $000000CD, $0000005A, $000000F4,
  171. $0000001F, $000000DD, $000000A8, $00000033, $00000088, $00000007, $000000C7, $00000031,
  172. $000000B1, $00000012, $00000010, $00000059, $00000027, $00000080, $000000EC, $0000005F,
  173. $00000060, $00000051, $0000007F, $000000A9, $00000019, $000000B5, $0000004A, $0000000D,
  174. $0000002D, $000000E5, $0000007A, $0000009F, $00000093, $000000C9, $0000009C, $000000EF,
  175. $000000A0, $000000E0, $0000003B, $0000004D, $000000AE, $0000002A, $000000F5, $000000B0,
  176. $000000C8, $000000EB, $000000BB, $0000003C, $00000083, $00000053, $00000099, $00000061,
  177. $00000017, $0000002B, $00000004, $0000007E, $000000BA, $00000077, $000000D6, $00000026,
  178. $000000E1, $00000069, $00000014, $00000063, $00000055, $00000021, $0000000C, $0000007D
  179. );
  180. procedure ExpandAESKeyForEncryption(const Key: TAESKey128; var ExpandedKey: TAESExpandedKey128);
  181. var
  182. I, J: integer;
  183. T: longword;
  184. W0, W1, W2, W3: longword;
  185. begin
  186. ExpandedKey[0] := PLongWord(@Key[0])^;
  187. ExpandedKey[1] := PLongWord(@Key[4])^;
  188. ExpandedKey[2] := PLongWord(@Key[8])^;
  189. ExpandedKey[3] := PLongWord(@Key[12])^;
  190. I := 0; J := 1;
  191. repeat
  192. T := (ExpandedKey[I + 3] shl 24) or (ExpandedKey[I + 3] shr 8);
  193. W0 := LastForwardTable[Byte(T)]; W1 := LastForwardTable[Byte(T shr 8)];
  194. W2 := LastForwardTable[Byte(T shr 16)]; W3 := LastForwardTable[Byte(T shr 24)];
  195. ExpandedKey[I + 4] := ExpandedKey[I] xor
  196. (W0 xor ((W1 shl 8) or (W1 shr 24)) xor
  197. ((W2 shl 16) or (W2 shr 16)) xor ((W3 shl 24) or (W3 shr 8))) xor Rcon[J];
  198. Inc(J);
  199. ExpandedKey[I + 5] := ExpandedKey[I + 1] xor ExpandedKey[I + 4];
  200. ExpandedKey[I + 6] := ExpandedKey[I + 2] xor ExpandedKey[I + 5];
  201. ExpandedKey[I + 7] := ExpandedKey[I + 3] xor ExpandedKey[I + 6];
  202. Inc(I, 4);
  203. until I >= 40;
  204. end;
  205. procedure EncryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey128;
  206. var OutBuf: TAESBuffer);
  207. var
  208. T0, T1: array [0..3] of longword;
  209. W0, W1, W2, W3: longword;
  210. begin
  211. // initializing
  212. T0[0] := PLongWord(@InBuf[0])^ xor Key[0];
  213. T0[1] := PLongWord(@InBuf[4])^ xor Key[1];
  214. T0[2] := PLongWord(@InBuf[8])^ xor Key[2];
  215. T0[3] := PLongWord(@InBuf[12])^ xor Key[3];
  216. // performing transformation 9 times
  217. // round 1
  218. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  219. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  220. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  221. xor ((W3 shl 24) or (W3 shr 8))) xor Key[4];
  222. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  223. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  224. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  225. xor ((W3 shl 24) or (W3 shr 8))) xor Key[5];
  226. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  227. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  228. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  229. xor ((W3 shl 24) or (W3 shr 8))) xor Key[6];
  230. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  231. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  232. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  233. xor ((W3 shl 24) or (W3 shr 8))) xor Key[7];
  234. // round 2
  235. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  236. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  237. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  238. xor ((W3 shl 24) or (W3 shr 8))) xor Key[8];
  239. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  240. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  241. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  242. xor ((W3 shl 24) or (W3 shr 8))) xor Key[9];
  243. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  244. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  245. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  246. xor ((W3 shl 24) or (W3 shr 8))) xor Key[10];
  247. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  248. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  249. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  250. xor ((W3 shl 24) or (W3 shr 8))) xor Key[11];
  251. // round 3
  252. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  253. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  254. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  255. xor ((W3 shl 24) or (W3 shr 8))) xor Key[12];
  256. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  257. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  258. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  259. xor ((W3 shl 24) or (W3 shr 8))) xor Key[13];
  260. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  261. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  262. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  263. xor ((W3 shl 24) or (W3 shr 8))) xor Key[14];
  264. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  265. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  266. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  267. xor ((W3 shl 24) or (W3 shr 8))) xor Key[15];
  268. // round 4
  269. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  270. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  271. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  272. xor ((W3 shl 24) or (W3 shr 8))) xor Key[16];
  273. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  274. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  275. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  276. xor ((W3 shl 24) or (W3 shr 8))) xor Key[17];
  277. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  278. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  279. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  280. xor ((W3 shl 24) or (W3 shr 8))) xor Key[18];
  281. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  282. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  283. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  284. xor ((W3 shl 24) or (W3 shr 8))) xor Key[19];
  285. // round 5
  286. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  287. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  288. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  289. xor ((W3 shl 24) or (W3 shr 8))) xor Key[20];
  290. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  291. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  292. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  293. xor ((W3 shl 24) or (W3 shr 8))) xor Key[21];
  294. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  295. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  296. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  297. xor ((W3 shl 24) or (W3 shr 8))) xor Key[22];
  298. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  299. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  300. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  301. xor ((W3 shl 24) or (W3 shr 8))) xor Key[23];
  302. // round 6
  303. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  304. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  305. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  306. xor ((W3 shl 24) or (W3 shr 8))) xor Key[24];
  307. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  308. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  309. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  310. xor ((W3 shl 24) or (W3 shr 8))) xor Key[25];
  311. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  312. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  313. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  314. xor ((W3 shl 24) or (W3 shr 8))) xor Key[26];
  315. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  316. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  317. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  318. xor ((W3 shl 24) or (W3 shr 8))) xor Key[27];
  319. // round 7
  320. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  321. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  322. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  323. xor ((W3 shl 24) or (W3 shr 8))) xor Key[28];
  324. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  325. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  326. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  327. xor ((W3 shl 24) or (W3 shr 8))) xor Key[29];
  328. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  329. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  330. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  331. xor ((W3 shl 24) or (W3 shr 8))) xor Key[30];
  332. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  333. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  334. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  335. xor ((W3 shl 24) or (W3 shr 8))) xor Key[31];
  336. // round 8
  337. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  338. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  339. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  340. xor ((W3 shl 24) or (W3 shr 8))) xor Key[32];
  341. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  342. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  343. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  344. xor ((W3 shl 24) or (W3 shr 8))) xor Key[33];
  345. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  346. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  347. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  348. xor ((W3 shl 24) or (W3 shr 8))) xor Key[34];
  349. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  350. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  351. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  352. xor ((W3 shl 24) or (W3 shr 8))) xor Key[35];
  353. // round 9
  354. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  355. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  356. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  357. xor ((W3 shl 24) or (W3 shr 8))) xor Key[36];
  358. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  359. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  360. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  361. xor ((W3 shl 24) or (W3 shr 8))) xor Key[37];
  362. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  363. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  364. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  365. xor ((W3 shl 24) or (W3 shr 8))) xor Key[38];
  366. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  367. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  368. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  369. xor ((W3 shl 24) or (W3 shr 8))) xor Key[39];
  370. // last round of transformations
  371. W0 := LastForwardTable[Byte(T1[0])]; W1 := LastForwardTable[Byte(T1[1] shr 8)];
  372. W2 := LastForwardTable[Byte(T1[2] shr 16)]; W3 := LastForwardTable[Byte(T1[3] shr 24)];
  373. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  374. xor ((W3 shl 24) or (W3 shr 8))) xor Key[40];
  375. W0 := LastForwardTable[Byte(T1[1])]; W1 := LastForwardTable[Byte(T1[2] shr 8)];
  376. W2 := LastForwardTable[Byte(T1[3] shr 16)]; W3 := LastForwardTable[Byte(T1[0] shr 24)];
  377. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  378. xor ((W3 shl 24) or (W3 shr 8))) xor Key[41];
  379. W0 := LastForwardTable[Byte(T1[2])]; W1 := LastForwardTable[Byte(T1[3] shr 8)];
  380. W2 := LastForwardTable[Byte(T1[0] shr 16)]; W3 := LastForwardTable[Byte(T1[1] shr 24)];
  381. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  382. xor ((W3 shl 24) or (W3 shr 8))) xor Key[42];
  383. W0 := LastForwardTable[Byte(T1[3])]; W1 := LastForwardTable[Byte(T1[0] shr 8)];
  384. W2 := LastForwardTable[Byte(T1[1] shr 16)]; W3 := LastForwardTable[Byte(T1[2] shr 24)];
  385. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  386. xor ((W3 shl 24) or (W3 shr 8))) xor Key[43];
  387. // finalizing
  388. PLongWord(@OutBuf[0])^ := T0[0]; PLongWord(@OutBuf[4])^ := T0[1];
  389. PLongWord(@OutBuf[8])^ := T0[2]; PLongWord(@OutBuf[12])^ := T0[3];
  390. end;
  391. procedure ExpandAESKeyForDecryption(var ExpandedKey: TAESExpandedKey128);
  392. var
  393. I: integer;
  394. U, F2, F4, F8, F9: longword;
  395. begin
  396. for I := 1 to 9 do
  397. begin
  398. F9 := ExpandedKey[I * 4];
  399. U := F9 and $80808080;
  400. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  401. U := F2 and $80808080;
  402. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  403. U := F4 and $80808080;
  404. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  405. F9 := F9 xor F8;
  406. ExpandedKey[I * 4] := F2 xor F4 xor F8 xor
  407. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  408. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  409. F9 := ExpandedKey[I * 4 + 1];
  410. U := F9 and $80808080;
  411. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  412. U := F2 and $80808080;
  413. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  414. U := F4 and $80808080;
  415. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  416. F9 := F9 xor F8;
  417. ExpandedKey[I * 4 + 1] := F2 xor F4 xor F8 xor
  418. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  419. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  420. F9 := ExpandedKey[I * 4 + 2];
  421. U := F9 and $80808080;
  422. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  423. U := F2 and $80808080;
  424. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  425. U := F4 and $80808080;
  426. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  427. F9 := F9 xor F8;
  428. ExpandedKey[I * 4 + 2] := F2 xor F4 xor F8 xor
  429. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  430. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  431. F9 := ExpandedKey[I * 4 + 3];
  432. U := F9 and $80808080;
  433. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  434. U := F2 and $80808080;
  435. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  436. U := F4 and $80808080;
  437. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  438. F9 := F9 xor F8;
  439. ExpandedKey[I * 4 + 3] := F2 xor F4 xor F8 xor
  440. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  441. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  442. end;
  443. end;
  444. procedure DecryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey128;
  445. var OutBuf: TAESBuffer);
  446. var
  447. T0, T1: array [0..3] of longword;
  448. W0, W1, W2, W3: longword;
  449. begin
  450. // initializing
  451. T0[0] := PLongWord(@InBuf[0])^ xor Key[40];
  452. T0[1] := PLongWord(@InBuf[4])^ xor Key[41];
  453. T0[2] := PLongWord(@InBuf[8])^ xor Key[42];
  454. T0[3] := PLongWord(@InBuf[12])^ xor Key[43];
  455. // performing transformations 9 times
  456. // round 1
  457. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  458. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  459. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  460. xor ((W3 shl 24) or (W3 shr 8))) xor Key[36];
  461. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  462. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  463. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  464. xor ((W3 shl 24) or (W3 shr 8))) xor Key[37];
  465. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  466. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  467. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  468. xor ((W3 shl 24) or (W3 shr 8))) xor Key[38];
  469. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  470. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  471. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  472. xor ((W3 shl 24) or (W3 shr 8))) xor Key[39];
  473. // round 2
  474. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  475. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  476. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  477. xor ((W3 shl 24) or (W3 shr 8))) xor Key[32];
  478. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  479. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  480. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  481. xor ((W3 shl 24) or (W3 shr 8))) xor Key[33];
  482. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  483. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  484. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  485. xor ((W3 shl 24) or (W3 shr 8))) xor Key[34];
  486. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  487. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  488. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  489. xor ((W3 shl 24) or (W3 shr 8))) xor Key[35];
  490. // round 3
  491. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  492. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  493. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  494. xor ((W3 shl 24) or (W3 shr 8))) xor Key[28];
  495. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  496. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  497. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  498. xor ((W3 shl 24) or (W3 shr 8))) xor Key[29];
  499. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  500. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  501. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  502. xor ((W3 shl 24) or (W3 shr 8))) xor Key[30];
  503. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  504. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  505. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  506. xor ((W3 shl 24) or (W3 shr 8))) xor Key[31];
  507. // round 4
  508. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  509. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  510. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  511. xor ((W3 shl 24) or (W3 shr 8))) xor Key[24];
  512. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  513. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  514. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  515. xor ((W3 shl 24) or (W3 shr 8))) xor Key[25];
  516. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  517. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  518. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  519. xor ((W3 shl 24) or (W3 shr 8))) xor Key[26];
  520. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  521. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  522. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  523. xor ((W3 shl 24) or (W3 shr 8))) xor Key[27];
  524. // round 5
  525. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  526. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  527. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  528. xor ((W3 shl 24) or (W3 shr 8))) xor Key[20];
  529. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  530. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  531. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  532. xor ((W3 shl 24) or (W3 shr 8))) xor Key[21];
  533. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  534. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  535. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  536. xor ((W3 shl 24) or (W3 shr 8))) xor Key[22];
  537. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  538. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  539. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  540. xor ((W3 shl 24) or (W3 shr 8))) xor Key[23];
  541. // round 6
  542. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  543. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  544. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  545. xor ((W3 shl 24) or (W3 shr 8))) xor Key[16];
  546. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  547. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  548. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  549. xor ((W3 shl 24) or (W3 shr 8))) xor Key[17];
  550. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  551. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  552. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  553. xor ((W3 shl 24) or (W3 shr 8))) xor Key[18];
  554. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  555. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  556. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  557. xor ((W3 shl 24) or (W3 shr 8))) xor Key[19];
  558. // round 7
  559. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  560. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  561. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  562. xor ((W3 shl 24) or (W3 shr 8))) xor Key[12];
  563. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  564. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  565. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  566. xor ((W3 shl 24) or (W3 shr 8))) xor Key[13];
  567. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  568. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  569. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  570. xor ((W3 shl 24) or (W3 shr 8))) xor Key[14];
  571. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  572. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  573. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  574. xor ((W3 shl 24) or (W3 shr 8))) xor Key[15];
  575. // round 8
  576. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  577. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  578. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  579. xor ((W3 shl 24) or (W3 shr 8))) xor Key[8];
  580. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  581. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  582. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  583. xor ((W3 shl 24) or (W3 shr 8))) xor Key[9];
  584. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  585. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  586. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  587. xor ((W3 shl 24) or (W3 shr 8))) xor Key[10];
  588. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  589. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  590. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  591. xor ((W3 shl 24) or (W3 shr 8))) xor Key[11];
  592. // round 9
  593. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  594. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  595. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  596. xor ((W3 shl 24) or (W3 shr 8))) xor Key[4];
  597. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  598. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  599. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  600. xor ((W3 shl 24) or (W3 shr 8))) xor Key[5];
  601. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  602. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  603. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  604. xor ((W3 shl 24) or (W3 shr 8))) xor Key[6];
  605. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  606. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  607. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  608. xor ((W3 shl 24) or (W3 shr 8))) xor Key[7];
  609. // last round of transformations
  610. W0 := LastInverseTable[Byte(T1[0])]; W1 := LastInverseTable[Byte(T1[3] shr 8)];
  611. W2 := LastInverseTable[Byte(T1[2] shr 16)]; W3 := LastInverseTable[Byte(T1[1] shr 24)];
  612. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  613. xor ((W3 shl 24) or (W3 shr 8))) xor Key[0];
  614. W0 := LastInverseTable[Byte(T1[1])]; W1 := LastInverseTable[Byte(T1[0] shr 8)];
  615. W2 := LastInverseTable[Byte(T1[3] shr 16)]; W3 := LastInverseTable[Byte(T1[2] shr 24)];
  616. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  617. xor ((W3 shl 24) or (W3 shr 8))) xor Key[1];
  618. W0 := LastInverseTable[Byte(T1[2])]; W1 := LastInverseTable[Byte(T1[1] shr 8)];
  619. W2 := LastInverseTable[Byte(T1[0] shr 16)]; W3 := LastInverseTable[Byte(T1[3] shr 24)];
  620. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  621. xor ((W3 shl 24) or (W3 shr 8))) xor Key[2];
  622. W0 := LastInverseTable[Byte(T1[3])]; W1 := LastInverseTable[Byte(T1[2] shr 8)];
  623. W2 := LastInverseTable[Byte(T1[1] shr 16)]; W3 := LastInverseTable[Byte(T1[0] shr 24)];
  624. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  625. xor ((W3 shl 24) or (W3 shr 8))) xor Key[3];
  626. // finalizing
  627. PLongWord(@OutBuf[0])^ := T0[0]; PLongWord(@OutBuf[4])^ := T0[1];
  628. PLongWord(@OutBuf[8])^ := T0[2]; PLongWord(@OutBuf[12])^ := T0[3];
  629. end;
  630. function CalculateAESEncryptBuffer128Size(const InSize: Longint): Longint;
  631. var
  632. iPack, iMore: Longint;
  633. begin
  634. Result:=SizeOf(integer);
  635. iPack := InSize div sizeof(TAESBuffer); //TAESBuffer = array [0..15] of byte;
  636. iMore := InSize mod sizeof(TAESBuffer);
  637. if iMore = 0 then Result := Result+InSize
  638. else Result := Result+(iPack + 1) * sizeof(TAESBuffer);
  639. end;
  640. function AESEncryptBuffer128(dest: PChar;
  641. destLen: PLongint;
  642. const source: PChar;
  643. const sourceLen: Longint;
  644. const strKey: string): integer;
  645. var
  646. Key: TAESKey128;
  647. ExpandedKey: TAESExpandedKey128;
  648. iSizeIn: integer;
  649. TempIn, TempOut: TAESBuffer;
  650. Postion: Cardinal;
  651. begin
  652. Result:=-1;
  653. //1:源数据长度
  654. iSizeIn := sourceLen;
  655. if iSizeIn = 0 then exit;
  656. //2:strKey--->TAESKey128--->TAESExpandedKey128
  657. // Prepare key...
  658. FillChar(Key, SizeOf(Key), 0);
  659. Move(PChar(strKey)^, Key, Min(SizeOf(Key), Length(strKey)));
  660. ExpandAESKeyForEncryption(Key, ExpandedKey);
  661. //3:把源数据长度写进目标缓冲头部
  662. Move(iSizeIn, Dest^, SizeOf(iSizeIn));
  663. Postion := 0;
  664. //4:如果源数据长度大于一个缓冲区,那么循环按照每缓冲区大小把它写进临时缓冲区,
  665. //加密后写到目标缓冲区(注意:不能覆盖之前的源字符大小)
  666. while iSizeIn >= SizeOf(TAESBuffer) do
  667. begin
  668. {
  669. Move(Pchar(Source + Postion)^, TempIn, SizeOf(TempIn));
  670. EncryptAES(TempIn, ExpandedKey, TempOut);
  671. Move(TempOut, Pchar(Dest + SizeOf(iSizeIn) + Postion)^, SizeOf(TempOut));
  672. }
  673. EncryptAES(PAESBuffer(Pchar(Source + Postion))^,ExpandedKey,PAESBuffer(Pchar(Dest + SizeOf(iSizeIn) + Postion))^);
  674. Dec(iSizeIn, SizeOf(TAESBuffer));
  675. Inc(Postion, SizeOf(TAESBuffer));
  676. end;
  677. //5:假设还有剩余,或者本身不足够一个临时缓冲区,那么,把它们放到临时缓冲区,不足部分填充0,
  678. //使其刚好足够一个缓冲区
  679. if iSizeIn > 0 then
  680. begin
  681. Move(pchar(Source + Postion)^, TempIn, iSizeIn);
  682. FillChar(TempIn[iSizeIn], SizeOf(TempIn) - iSizeIn, 0);
  683. EncryptAES(TempIn, ExpandedKey, TempOut);
  684. Move(TempOut, pchar(Dest + SizeOf(iSizeIn) + Postion)^, SizeOf(TempOut));
  685. Inc(Postion, SizeOf(TempOut));
  686. end;
  687. //6:注意:到最后,目的缓冲区大小必须加SizeOf(Size)
  688. destLen^:=SizeOf(iSizeIn)+Postion;
  689. Result:=0;
  690. end;
  691. function AESDecryptBuffer128(dest: Pchar;
  692. destLen: PLongint;
  693. const source: Pchar;
  694. const sourceLen: Longint;
  695. const strKey: string): integer;
  696. var
  697. Key: TAESKey128;
  698. ExpandedKey: TAESExpandedKey128;
  699. iSizeIn: integer;
  700. //TempIn, TempOut: TAESBuffer;
  701. Postion: Cardinal;
  702. iOriginSize: integer;
  703. begin
  704. Result:=-1;
  705. //1:计算源数据长度
  706. iSizeIn := sourceLen;
  707. //2:去掉头部原来写入的数据长度
  708. iSizeIn:=iSizeIn-sizeof(iSizeIn);
  709. if iSizeIn = 0 then exit;
  710. if (iSizeIn mod SizeOf(TAESBuffer)) > 0 then Exit;
  711. //3:获取原始数据大小
  712. iOriginSize:=pInteger(source)^; //这里是否应该判断溢出(可能是非加密Buff)?
  713. destLen^:=iOriginSize;
  714. //ShowMessage(IntToStr(iOriginSize));
  715. //4:strKey--->TAESExpandedKey128--->TAESExpandedKey128
  716. // Prepare key...
  717. FillChar(Key, SizeOf(Key), 0);
  718. Move(PChar(strKey)^, Key, Min(SizeOf(Key), Length(strKey)));
  719. ExpandAESKeyForEncryption(Key, ExpandedKey);
  720. ExpandAESKeyForDecryption(ExpandedKey);
  721. Postion :=0;
  722. //5:循环解密
  723. while iSizeIn >= SizeOf(TAESBuffer) do
  724. begin
  725. {
  726. Move(Pchar(Source +sizeof(iSizeIn)+ Postion)^, TempIn, SizeOf(TempIn));
  727. DecryptAES(TempIn, ExpandedKey, TempOut);
  728. Move(TempOut, Pchar(Dest + Postion)^, SizeOf(TempOut));
  729. }
  730. DecryptAES(PAESBuffer(Pchar(Source +sizeof(iSizeIn)+ Postion))^, ExpandedKey, PAESBuffer(Pchar(Dest + Postion))^);
  731. Dec(iSizeIn, SizeOf(TAESBuffer));
  732. Inc(Postion, SizeOf(TAESBuffer));
  733. end;
  734. Result:=0;
  735. end;
  736. procedure CvtInt;
  737. { IN:
  738. EAX: The integer value to be converted to text
  739. ESI: Ptr to the right-hand side of the output buffer: LEA ESI, StrBuf[16]
  740. ECX: Base for conversion: 0 for signed decimal, 10 or 16 for unsigned
  741. EDX: Precision: zero padded minimum field width
  742. OUT:
  743. ESI: Ptr to start of converted text (not start of buffer)
  744. ECX: Length of converted text
  745. }
  746. asm
  747. OR CL,CL
  748. JNZ @CvtLoop
  749. @C1: OR EAX,EAX
  750. JNS @C2
  751. NEG EAX
  752. CALL @C2
  753. MOV AL,'-'
  754. INC ECX
  755. DEC ESI
  756. MOV [ESI],AL
  757. RET
  758. @C2: MOV ECX,10
  759. @CvtLoop:
  760. PUSH EDX
  761. PUSH ESI
  762. @D1: XOR EDX,EDX
  763. DIV ECX
  764. DEC ESI
  765. ADD DL,'0'
  766. CMP DL,'0'+10
  767. JB @D2
  768. ADD DL,('A'-'0')-10
  769. @D2: MOV [ESI],DL
  770. OR EAX,EAX
  771. JNE @D1
  772. POP ECX
  773. POP EDX
  774. SUB ECX,ESI
  775. SUB EDX,ECX
  776. JBE @D5
  777. ADD ECX,EDX
  778. MOV AL,'0'
  779. SUB ESI,EDX
  780. JMP @z
  781. @zloop: MOV [ESI+EDX],AL
  782. @z: DEC EDX
  783. JNZ @zloop
  784. MOV [ESI],AL
  785. @D5:
  786. end;
  787. function IntToHex(Value: Integer; Digits: Integer): string;
  788. // FmtStr(Result, '%.*x', [Digits, Value]);
  789. asm
  790. CMP EDX, 32 // Digits < buffer length?
  791. JBE @A1
  792. XOR EDX, EDX
  793. @A1: PUSH ESI
  794. MOV ESI, ESP
  795. SUB ESP, 32
  796. PUSH ECX // result ptr
  797. MOV ECX, 16 // base 16 EDX = Digits = field width
  798. CALL CvtInt
  799. MOV EDX, ESI
  800. POP EAX // result ptr
  801. CALL System.@LStrFromPCharLen
  802. ADD ESP, 32
  803. POP ESI
  804. end;
  805. function StringToHex(S: string): string;
  806. var
  807. i: integer;
  808. begin
  809. Result := '';
  810. // Go throught every single characters, and convert them
  811. // to hexadecimal...
  812. for i := 1 to Length(S) do
  813. Result := Result + IntToHex(Ord(S[i]), 2);
  814. end;
  815. function AESEncryptStr128(const strIn, strKey: string): string;
  816. var
  817. Key: TAESKey128;
  818. ExpandedKey: TAESExpandedKey128;
  819. Source, Dest: Pchar;
  820. iSizeIn, iSizeOut: integer;
  821. TempIn, TempOut: TAESBuffer;
  822. Postion: Cardinal;
  823. // iPack, iMore: integer;
  824. strTemp: string;
  825. begin
  826. Result:='';
  827. //1:计算源字符长度
  828. iSizeIn := Length(strIn);
  829. if iSizeIn = 0 then exit;
  830. (*
  831. //2:通过源字符长度,计算出需要分配的源内存大小和目的内存大小
  832. iPack := iSizeIn div sizeof(TAESBuffer);
  833. iMore := iSizeIn mod sizeof(TAESBuffer);
  834. iSizeOut := iPack * sizeof(TAESBuffer);
  835. if iMore <> 0 then iSizeOut := iSizeOut + sizeof(TAESBuffer); //有余数,需要把余下的作为一个区
  836. iSizeOut := SizeOf(iSizeIn) + iSizeOut; //头部有源字符长度,所以得加上其占用的字节数
  837. *)
  838. iSizeOut:=CalculateAESEncryptBuffer128Size(iSizeIn);
  839. //3:strKey--->TAESKey128--->TAESExpandedKey128
  840. // Prepare key...
  841. FillChar(Key, SizeOf(Key), 0);
  842. Move(PChar(strKey)^, Key, Min(SizeOf(Key), Length(strKey)));
  843. ExpandAESKeyForEncryption(Key, ExpandedKey);
  844. //4:分配内存
  845. GetMem(Source, iSizeIn);
  846. GetMem(Dest, iSizeOut);
  847. //5:把源字符长度写进目标缓冲头部
  848. Move(iSizeIn, Dest^, SizeOf(iSizeIn));
  849. //6:把源字符写进源缓冲区
  850. Move(PChar(strIn)^, Source^, iSizeIn);
  851. Postion := 0;
  852. //7:如果源字符大小大于一个缓冲区,那么循环把它写进临时缓冲区,加密后写到目标缓冲区(注意:不能覆盖之前的源字符大小)
  853. while iSizeIn >= SizeOf(TAESBuffer) do
  854. begin
  855. {
  856. Move(Pchar(Source + Postion)^, TempIn, SizeOf(TempIn));
  857. EncryptAES(TempIn, ExpandedKey, TempOut);
  858. Move(TempOut, Pchar(Dest + SizeOf(iSizeIn) + Postion)^, SizeOf(TempOut));
  859. }
  860. EncryptAES(PAESBuffer(Pchar(Source + Postion))^, ExpandedKey, PAESBuffer(Pchar(Dest + SizeOf(iSizeIn) + Postion))^);
  861. Dec(iSizeIn, SizeOf(TAESBuffer));
  862. Inc(Postion, SizeOf(TAESBuffer));
  863. end;
  864. //8:假设还有剩余,或者本身不足够一个临时缓冲区,那么,把它们放到临时缓冲区,不足部分填充0,使其刚好足够一个缓冲区
  865. if iSizeIn > 0 then
  866. begin
  867. Move(pchar(Source + Postion)^, TempIn, iSizeIn);
  868. FillChar(TempIn[iSizeIn], SizeOf(TempIn) - iSizeIn, 0);
  869. EncryptAES(TempIn, ExpandedKey, TempOut);
  870. Move(TempOut, pchar(Dest + SizeOf(iSizeIn) + Postion)^, SizeOf(TempOut));
  871. //Inc(Postion, SizeOf(TempOut));
  872. end;
  873. //9:注意:到最后,目的缓冲区大小等于整数乘以一个缓冲区大小加SizeOf(Size)
  874. SetLength(strTemp, iSizeOut);
  875. Move(Dest^, strTemp[1], iSizeOut);
  876. //10:释放内存.并把结果以16进制字符形式返回
  877. FreeMem(Source);
  878. FreeMem(Dest);
  879. Result := StringToHex(strTemp);
  880. end;
  881. function StrToInt(const S: string): Integer;
  882. var
  883. E: Integer;
  884. begin
  885. Val(S, Result, E);
  886. if E <> 0 then Result:=E;
  887. end;
  888. function HexToString(S: string): string;
  889. var
  890. i: integer;
  891. begin
  892. Result := '';
  893. // Go throught every single hexadecimal characters, and convert
  894. // them to ASCII characters...
  895. for i := 1 to Length( S ) do
  896. begin
  897. // Only process chunk of 2 digit Hexadecimal...
  898. if ((i mod 2) = 1) then
  899. Result := Result + Chr( StrToInt( '0x' + Copy( S, i, 2 )));
  900. end;
  901. end;
  902. function StrPas(const Str: PChar): string;
  903. begin
  904. Result := Str;
  905. end;
  906. function AESDecryptStr128(const strIn, strKey: string): string;
  907. var
  908. Key: TAESKey128;
  909. ExpandedKey: TAESExpandedKey128;
  910. Source, Dest: Pchar;
  911. iSizeIn, iSizeOut: integer;
  912. //TempIn, TempOut: TAESBuffer;
  913. Postion: Cardinal;
  914. //此函数没有用到,注释掉 iOriginSize: integer;
  915. strTemp: string;
  916. begin
  917. Result:='';
  918. //1:先还原字符
  919. strTemp:=HexToString(strIn);
  920. //2:计算源字符长度
  921. iSizeIn := Length(strTemp);
  922. //3:去掉头部原来写入的字符长度
  923. iSizeIn:=iSizeIn-sizeof(iSizeIn);
  924. if iSizeIn = 0 then exit;
  925. if (iSizeIn mod SizeOf(TAESBuffer)) > 0 then Exit;
  926. //此函数没有用到,注释掉 iOriginSize:=pInteger(Pchar(strTemp))^; //原始数据大小
  927. //ShowMessage(IntToStr(iOriginSize));
  928. //4:计算出需要分配的源内存大小和目的内存大小
  929. iSizeOut := iSizeIn;
  930. //3:strKey--->TAESExpandedKey128--->TAESExpandedKey128
  931. // Prepare key...
  932. FillChar(Key, SizeOf(Key), 0);
  933. Move(PChar(strKey)^, Key, Min(SizeOf(Key), Length(strKey)));
  934. ExpandAESKeyForEncryption(Key, ExpandedKey);
  935. ExpandAESKeyForDecryption(ExpandedKey);
  936. //4:分配内存
  937. GetMem(Source, iSizeIn);
  938. GetMem(Dest, iSizeOut);
  939. //6:把源字符写进源缓冲区.注意:只写头后面的数据.
  940. Move(PChar(Pchar(strTemp)+sizeof(iSizeIn))^, Source^, iSizeIn);
  941. Postion :=0;
  942. //7:循环解密
  943. while iSizeIn >= SizeOf(TAESBuffer) do
  944. begin
  945. {
  946. Move(Pchar(Source + Postion)^, TempIn, SizeOf(TempIn));
  947. DecryptAES(TempIn, ExpandedKey, TempOut);
  948. Move(TempOut, Pchar(Dest + Postion)^, SizeOf(TempOut));
  949. }
  950. DecryptAES(PAESBuffer(Pchar(Source + Postion))^,ExpandedKey,PAESBuffer(Pchar(Dest + Postion))^);
  951. Dec(iSizeIn, SizeOf(TAESBuffer));
  952. Inc(Postion, SizeOf(TAESBuffer));
  953. end;
  954. //9:注意:到最后,目的缓冲区大小等于整数乘以一个缓冲区大小加SizeOf(Size)
  955. //SetLength(strTemp, iSizeOut);
  956. //Move(Dest^, strTemp[1], iSizeOut);
  957. //10:释放内存.并把结果以16进制字符形式返回
  958. Result := StrPas(Dest);
  959. FreeMem(Source);
  960. FreeMem(Dest);
  961. end;
  962. end.