comment
IRC Chat
play_arrow
Este sitio utiliza cookies propias y de terceros. Si continúa navegando consideramos que acepta el uso de cookies. OK Más Información.

[UNIT] BlowFish Encription

  • 0 Respuestas
  • 1902 Vistas

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Expermicid

  • *
  • Underc0der
  • Mensajes: 457
  • Actividad:
    0%
  • Reputación 0
  • _-Expermicid-_
    • Ver Perfil
  • Skype: expermicid.xd
« en: Junio 11, 2013, 09:39:39 pm »
Código: Delphi
  1. unit Blowfish;
  2. {
  3.   (c) 1998, 1999, 2000 Budi Sukmawan
  4.   mailto:budskman [at] yahoo [dot] com
  5.   [url]http://www.bimacipta.com/[/url]
  6.  
  7.   1998.04.17 written by Budi Sukmawan <budskman@hotmail.com>
  8.   1999.03.25 Add ECB, CBC, CTS, CFB, OFB routine.
  9.   1999.11.06 Change Setup key routine.
  10.              Fix CBC, CBC CTS, CFB, OFB routine.
  11.   1999.12.03 change type casting at endian conversion
  12.              small performance adv. [-12 clock/round)
  13.   2000.04.21 Simplify routine for publication
  14.              CBC mode only
  15. }
  16. interface
  17.  
  18. uses Windows, SysUtils;
  19.  
  20. const
  21.   BF_BLOCKSIZE = 8;    { Blowfish block size }
  22.   BF_SBOX_SIZE = 256;  { Number of S-box entries }
  23.   BF_SBOX_SIZE_BYTES = ( BF_SBOX_SIZE * 4 );
  24.   BF_PARRAY_SIZE = 18; { Number of P-array entries }
  25.   BF_NO_ROUNDS = 16;
  26.   IVSIZE = BF_BLOCKSIZE;
  27.  
  28. type
  29. {$ifndef VER80}{$ifndef VER90}{$ifndef VER93}
  30. {$ifndef VER100}{$ifndef VER110}
  31.  {$define D4UP}
  32. {$endif}{$endif}{$endif}{$endif}{$endif}
  33.  
  34. {$ifndef D4UP}
  35.   LongWord = LongInt;
  36. {$endif}
  37.  
  38.   TSBox = array [0..BF_SBOX_SIZE-1] of LongWord;
  39.   TPArr = array [0..BF_PARRAY_SIZE-1] of LongWord;
  40.   TBFBlock = array [0..BF_BLOCKSIZE-1] of Byte;
  41.   TIV = array [0..IVSIZE-1] of Byte;
  42.   PBA = PByteArray;
  43.   PLA = ^TLongArray;
  44.   TLongArray = array[0..(4*1024)-1] of LongWord;
  45.  
  46. {$I BF_Init.Inc}
  47.  
  48. type
  49.   PBlowFishKey = ^TBlowFishKey;
  50.   TBlowFishKey = record
  51.     P: TPArr;                { P-array }
  52.     S1, S2, S3, S4: TSBox;   { S-boxes }
  53.   end;
  54.  
  55.   TCryptCtx = record
  56.     { User encryption key }
  57.     key: TBlowFishKey; { kunci Blowfish }
  58.     IV,                { Initial IV }
  59.     currentIV: TIV;    { IV }
  60.   end;
  61.  
  62.   procedure bfEncrypt(const key: TBlowFishKey;const inBlock;var outBlock);
  63.   procedure bfDecrypt(const key: TBlowFishKey;const inBlock;var outBlock);
  64.   procedure bfKeySetup(var key: TBlowFishKey;const pUserKey;
  65.                        userKeyLen: Integer );
  66.   function bfSelfTest: Integer;
  67.   procedure bfEncryptCBC(var cryptCtx: TCryptCtx; var pBuffer; bufLen: Integer);
  68.   procedure bfDecryptCBC(var cryptCtx: TCryptCtx; var pBuffer; bufLen: Integer);
  69.   procedure bfResetContext(var cryptCtx: TCryptCtx);
  70.   function bfModeSelfTest: Integer;
  71.  
  72. implementation
  73.  
  74. procedure bfEncrypt(const key: TBlowFishKey;const inBlock;var outBlock);
  75. var
  76.   L, R, i: LongWord;
  77.  
  78. begin
  79.   { get data  }
  80.   L := (LongWord(TByteArray(inBlock)[0]) shl 24) or
  81.        (LongWord(TByteArray(inBlock)[1]) shl 16) or
  82.        (LongWord(TByteArray(inBlock)[2]) shl 8) or
  83.         LongWord(TByteArray(inBlock)[3]);
  84.   R := (LongWord(TByteArray(inBlock)[4]) shl 24) or
  85.        (LongWord(TByteArray(inBlock)[5]) shl 16) or
  86.        (LongWord(TByteArray(inBlock)[6]) shl 8) or
  87.         LongWord(TByteArray(inBlock)[7]);
  88.   { Perform 16 rounds of encryption }
  89.   with key do
  90.   begin
  91.     i := 1;
  92.     L := L xor P[0];
  93.     while i <= BF_NO_ROUNDS do
  94.     begin
  95.       R := R xor P[i] xor
  96.            (((S1[L shr 24] + S2[Byte(L shr 16)]) xor
  97.               S3[Byte(L shr 8)]) + S4[Byte(L)]);
  98.       inc(i);
  99.       L := L xor P[i] xor
  100.            (((S1[R shr 24] + S2[Byte(R shr 16)]) xor
  101.               S3[Byte(R shr 8)]) + S4[Byte(R)]);
  102.       inc(i);
  103.     end;
  104.     R := R xor P[BF_NO_ROUNDS + 1];
  105.   end;
  106.   { put data back }
  107.   TByteArray(outBlock)[0] := R shr 24;
  108.   TByteArray(outBlock)[1] := Byte(R shr 16);
  109.   TByteArray(outBlock)[2] := Byte(R shr  8);
  110.   TByteArray(outBlock)[3] := Byte(R);
  111.   TByteArray(outBlock)[4] := L shr 24;
  112.   TByteArray(outBlock)[5] := Byte(L shr 16);
  113.   TByteArray(outBlock)[6] := Byte(L shr  8);
  114.   TByteArray(outBlock)[7] := Byte(L);
  115. end;
  116.  
  117. procedure bfDecrypt(const key: TBlowFishKey;const inBlock;var outBlock);
  118. var
  119.   L, R, i: LongWord;
  120.  
  121. begin
  122.   { get data }
  123.   R := (LongWord(TByteArray(inBlock)[0]) shl 24) or
  124.        (LongWord(TByteArray(inBlock)[1]) shl 16) or
  125.        (LongWord(TByteArray(inBlock)[2]) shl 8) or
  126.         LongWord(TByteArray(inBlock)[3]);
  127.   L := (LongWord(TByteArray(inBlock)[4]) shl 24) or
  128.        (LongWord(TByteArray(inBlock)[5]) shl 16) or
  129.        (LongWord(TByteArray(inBlock)[6]) shl 8) or
  130.         LongWord(TByteArray(inBlock)[7]);
  131.   { Perform 16 rounds of decryption }
  132.   with key do
  133.   begin
  134.     i := BF_NO_ROUNDS;
  135.     R := R xor P[BF_NO_ROUNDS + 1];
  136.     while i >= 1 do
  137.     begin
  138.       L := L xor P[i] xor
  139.            (((S1[R shr 24] + S2[Byte(R shr 16)]) xor
  140.               S3[Byte(R shr 8)]) + S4[Byte(R)]);
  141.       dec(i);
  142.       R := R xor P[i] xor
  143.            (((S1[L shr 24] + S2[Byte(L shr 16)]) xor
  144.               S3[Byte(L shr 8)]) + S4[Byte(L)]);
  145.       dec(i);
  146.     end;
  147.     L := L xor P[0];
  148.   end;
  149.   { put data back }
  150.   TByteArray(outBlock)[0] := L shr 24;
  151.   TByteArray(outBlock)[1] := Byte(L shr 16);
  152.   TByteArray(outBlock)[2] := Byte(L shr  8);
  153.   TByteArray(outBlock)[3] := Byte(L);
  154.   TByteArray(outBlock)[4] := R shr 24;
  155.   TByteArray(outBlock)[5] := Byte(R shr 16);
  156.   TByteArray(outBlock)[6] := Byte(R shr  8);
  157.   TByteArray(outBlock)[7] := Byte(R);
  158. end;
  159.  
  160. {These only for key setup, no endian conversion needed}
  161. procedure bfInitEncrypt(const key: TBlowFishKey;var data1, data2: LongWord);
  162. var
  163.   L, R, i: LongWord;
  164. begin
  165.   L := data1;
  166.   R := data2;
  167.   with key do
  168.   begin
  169.     i := 1;
  170.     L := L xor P[0];
  171.     while i <= BF_NO_ROUNDS do
  172.     begin
  173.       R := R xor P[i] xor
  174.            (((S1[L shr 24] + S2[Byte(L shr 16)]) xor
  175.               S3[Byte(L shr 8)]) + S4[Byte(L)]);
  176.       inc(i);
  177.       L := L xor P[i] xor
  178.            (((S1[R shr 24] + S2[Byte(R shr 16)]) xor
  179.               S3[Byte(R shr 8)]) + S4[Byte(R)]);
  180.       inc(i);
  181.     end;
  182.     R := R xor P[BF_NO_ROUNDS + 1];
  183.   end;
  184.   data1 := R;
  185.   data2 := L;
  186. end;
  187.  
  188. procedure bfKeySetup(var key: TBlowFishKey;const pUserKey; userKeyLen: Integer);
  189. var
  190.   userKey: PByte;
  191.   keyIndex, i, j, byteIndex: Integer;
  192.   data1, data2: LongWord;
  193.   sBox: PLA;
  194. begin
  195.   userKey := @pUserKey;
  196.   for i := 0 to (BF_NO_ROUNDS + 2) - 1 do
  197.     key.P[i] := initialParray[i];
  198.   for i := 0 to BF_SBOX_SIZE-1 do
  199.   begin
  200.     key.S1[i] := initialSbox1[i];
  201.     key.S2[i] := initialSbox2[i];
  202.     key.S3[i] := initialSbox3[i];
  203.     key.S4[i] := initialSbox4[i];
  204.   end;
  205.   keyIndex := 0;
  206.   for i := 0 to (BF_NO_ROUNDS + 2) - 1 do
  207.   begin
  208.     data1 := 0;
  209.     for byteIndex := 0 to 3 do
  210.     begin
  211.       data1 := data1 shl 8;
  212.       data1 := data1 or LongWord(PByteArray(userKey)[keyIndex]);
  213.       inc(keyIndex);
  214.       keyIndex := keyIndex mod userKeyLen;
  215.     end;
  216.     key.P[i] := key.P[i] xor data1;
  217.   end;
  218.   data1 := 0;
  219.   data2 := 0;
  220.   i := 0 ;
  221.   while i < (BF_NO_ROUNDS + 2) do
  222.   begin
  223.     bfInitEncrypt(key, data1, data2);
  224.     key.P[i] := data1;
  225.     key.P[i+1] := data2;
  226.     inc(i,2);
  227.   end;
  228.   sBox := nil;
  229.   for j := 0 to 3 do
  230.   begin
  231.     case j of
  232.      0: sBox := @key.S1;
  233.      1: sBox := @key.S2;
  234.      2: sBox := @key.S3;
  235.      3: sBox := @key.S4;
  236.     end;
  237.     i := 0;
  238.     while i < BF_SBOX_SIZE do
  239.     begin
  240.       bfInitEncrypt(key, data1, data2);
  241.       sBox[i] := data1;
  242.       sBox[i+1] := data2;
  243.       Inc(i, 2);
  244.     end;
  245.   end;
  246. end;
  247.  
  248. (***************************************************************
  249.  *                 Blowfish Mode of Operation                  *
  250.  ***************************************************************)
  251.  
  252. procedure bfResetContext(var cryptCtx: TCryptCtx);
  253. begin
  254.   Move(cryptCtx.IV, cryptCtx.currentIV, IVSIZE);
  255. end;
  256.  
  257. { Encrypt data in CBC (Cipher Block Chaining) Mode}
  258. procedure bfEncryptCBC(var cryptCtx: TCryptCtx; var pBuffer; bufLen: Integer);
  259. var
  260.   buffer, currentIV: PByte;
  261. begin
  262.   assert( ( bufLen mod BF_BLOCKSIZE ) = 0,
  263.          'Data length is must be multiple of the block size');
  264.   buffer := @pBuffer;
  265.   currentIV := @cryptCtx.currentIV;
  266.   while bufLen >= BF_BLOCKSIZE do
  267.   begin
  268.     PLA(buffer)^[0] := PLA(buffer)^[0] xor PLA(currentIV)^[0];
  269.     PLA(buffer)^[1] := PLA(buffer)^[1] xor PLA(currentIV)^[1];
  270.     bfEncrypt(cryptCtx.key, buffer^, buffer^);
  271.     currentIV := buffer;
  272.     Inc(buffer, BF_BLOCKSIZE);
  273.     Dec(bufLen, BF_BLOCKSIZE);
  274.   end;
  275.   {Move(currentIV^, cryptCtx.currentIV, BF_BLOCKSIZE);}
  276.   PLA(@cryptCtx.currentIV)^[0] := PLA(currentIV)^[0];
  277.   PLA(@cryptCtx.currentIV)^[1] := PLA(currentIV)^[1];
  278. end;
  279.  
  280. { Decrypt data in CBC mode }
  281. procedure bfDecryptCBC(var cryptCtx: TCryptCtx; var pBuffer; bufLen: Integer);
  282. var
  283.   buffer: PByte;
  284.   temp: TbfBlock;
  285. begin
  286.   assert( ( bufLen mod BF_BLOCKSIZE ) = 0,
  287.          'Data length is must be multiple of the block size');
  288.   buffer := @pBuffer;
  289.   while bufLen >= BF_BLOCKSIZE do
  290.   begin
  291.     {Move(buffer^, temp, BF_BLOCKSIZE);}
  292.     PLA(@temp)^[0] := PLA(buffer)^[0];
  293.     PLA(@temp)^[1] := PLA(buffer)^[1];
  294.     bfDecrypt(cryptCtx.key, buffer^, buffer^);
  295.     PLA(buffer)^[0] := PLA(buffer)^[0] xor PLA(@cryptCtx.currentIV)^[0];
  296.     PLA(buffer)^[1] := PLA(buffer)^[1] xor PLA(@cryptCtx.currentIV)^[1];
  297.     {Move(temp, cryptCtx.currentIV, BF_BLOCKSIZE);}
  298.     PLA(@cryptCtx.currentIV)^[0] := PLA(@temp)^[0];
  299.     PLA(@cryptCtx.currentIV)^[1] := PLA(@temp)^[1];
  300.     Inc(buffer, BF_BLOCKSIZE);
  301.     Dec(bufLen, BF_BLOCKSIZE);
  302.   end;
  303. end;
  304.  
  305. (***************************************************************
  306.  *                      Blowfish Self Test                     *
  307.  ***************************************************************)
  308. type
  309.   TBlock64 = array [0..7] of byte;
  310.   TBfTest = record
  311.     key: TBlock64;
  312.     pt: TBlock64;
  313.     ct: TBlock64;
  314.   end;
  315.  
  316. const
  317.   plain1: PChar = 'BLOWFISH';
  318.   key1: PChar = 'abcdefghijklmnopqrstuvwxyz';
  319.   cipher1: TBFBlock = ($32,$4E,$D0,$FE,$F4,$13,$A2,$03);
  320.  
  321.   plain2: TBFBlock = ($FE,$DC,$BA,$98,$76,$54,$32,$10);
  322.   key2: PChar = 'Who is John Galt?';
  323.   cipher2: TBFBlock = ($CC,$91,$73,$2B,$80,$22,$F6,$84);
  324.  
  325.   plain3: TBFBlock = ($FE, $DC, $BA, $98, $76, $54, $32, $10);
  326.   key3: TBFBlock = ($41, $79, $6E, $A0, $52, $61, $6E, $E4);
  327.   cipher3: TBFBlock = ($E1, $13, $F4, $10, $2C, $FC, $CE, $43);
  328.  
  329.   bfTest : array[0..33] of TBfTest = (
  330.     (key : ($00, $00, $00, $00, $00, $00, $00, $00);
  331.      pt  : ($00, $00, $00, $00, $00, $00, $00, $00);
  332.      ct  : ($4E, $F9, $97, $45, $61, $98, $DD, $78)),
  333.     (key : ($FF, $FF, $FF, $FF, $FF, $FF, $FF, $FF);
  334.      pt  : ($FF, $FF, $FF, $FF, $FF, $FF, $FF, $FF);
  335.      ct  : ($51, $86, $6F, $D5, $B8, $5E, $CB, $8A)),
  336.     (key : ($30, $00, $00, $00, $00, $00, $00, $00);
  337.      pt  : ($10, $00, $00, $00, $00, $00, $00, $01);
  338.      ct  : ($7D, $85, $6F, $9A, $61, $30, $63, $F2)),
  339.     (key : ($11, $11, $11, $11, $11, $11, $11, $11);
  340.      pt  : ($11, $11, $11, $11, $11, $11, $11, $11);
  341.      ct  : ($24, $66, $DD, $87, $8B, $96, $3C, $9D)),
  342.     (key : ($01, $23, $45, $67, $89, $AB, $CD, $EF);
  343.      pt  : ($11, $11, $11, $11, $11, $11, $11, $11);
  344.      ct  : ($61, $F9, $C3, $80, $22, $81, $B0, $96)),
  345.     (key : ($11, $11, $11, $11, $11, $11, $11, $11);
  346.      pt  : ($01, $23, $45, $67, $89, $AB, $CD, $EF);
  347.      ct  : ($7D, $0C, $C6, $30, $AF, $DA, $1E, $C7)),
  348.     (key : ($00, $00, $00, $00, $00, $00, $00, $00);
  349.      pt  : ($00, $00, $00, $00, $00, $00, $00, $00);
  350.      ct  : ($4E, $F9, $97, $45, $61, $98, $DD, $78)),
  351.     (key : ($FE, $DC, $BA, $98, $76, $54, $32, $10);
  352.      pt  : ($01, $23, $45, $67, $89, $AB, $CD, $EF);
  353.      ct  : ($0A, $CE, $AB, $0F, $C6, $A0, $A2, $8D)),
  354.     (key : ($7C, $A1, $10, $45, $4A, $1A, $6E, $57);
  355.      pt  : ($01, $A1, $D6, $D0, $39, $77, $67, $42);
  356.      ct  : ($59, $C6, $82, $45, $EB, $05, $28, $2B)),
  357.     (key : ($01, $31, $D9, $61, $9D, $C1, $37, $6E);
  358.      pt  : ($5C, $D5, $4C, $A8, $3D, $EF, $57, $DA);
  359.      ct  : ($B1, $B8, $CC, $0B, $25, $0F, $09, $A0)),
  360.     (key : ($07, $A1, $13, $3E, $4A, $0B, $26, $86);
  361.      pt  : ($02, $48, $D4, $38, $06, $F6, $71, $72);
  362.      ct  : ($17, $30, $E5, $77, $8B, $EA, $1D, $A4)),
  363.     (key : ($38, $49, $67, $4C, $26, $02, $31, $9E);
  364.      pt  : ($51, $45, $4B, $58, $2D, $DF, $44, $0A);
  365.      ct  : ($A2, $5E, $78, $56, $CF, $26, $51, $EB)),
  366.     (key : ($04, $B9, $15, $BA, $43, $FE, $B5, $B6);
  367.      pt  : ($42, $FD, $44, $30, $59, $57, $7F, $A2);
  368.      ct  : ($35, $38, $82, $B1, $09, $CE, $8F, $1A)),
  369.     (key : ($01, $13, $B9, $70, $FD, $34, $F2, $CE);
  370.      pt  : ($05, $9B, $5E, $08, $51, $CF, $14, $3A);
  371.      ct  : ($48, $F4, $D0, $88, $4C, $37, $99, $18)),
  372.     (key : ($01, $70, $F1, $75, $46, $8F, $B5, $E6);
  373.      pt  : ($07, $56, $D8, $E0, $77, $47, $61, $D2);
  374.      ct  : ($43, $21, $93, $B7, $89, $51, $FC, $98)),
  375.     (key : ($43, $29, $7F, $AD, $38, $E3, $73, $FE);
  376.      pt  : ($76, $25, $14, $B8, $29, $BF, $48, $6A);
  377.      ct  : ($13, $F0, $41, $54, $D6, $9D, $1A, $E5)),
  378.     (key : ($07, $A7, $13, $70, $45, $DA, $2A, $16);
  379.      pt  : ($3B, $DD, $11, $90, $49, $37, $28, $02);
  380.      ct  : ($2E, $ED, $DA, $93, $FF, $D3, $9C, $79)),
  381.     (key : ($04, $68, $91, $04, $C2, $FD, $3B, $2F);
  382.      pt  : ($26, $95, $5F, $68, $35, $AF, $60, $9A);
  383.      ct  : ($D8, $87, $E0, $39, $3C, $2D, $A6, $E3)),
  384.     (key : ($37, $D0, $6B, $B5, $16, $CB, $75, $46);
  385.      pt  : ($16, $4D, $5E, $40, $4F, $27, $52, $32);
  386.      ct  : ($5F, $99, $D0, $4F, $5B, $16, $39, $69)),
  387.     (key : ($1F, $08, $26, $0D, $1A, $C2, $46, $5E);
  388.      pt  : ($6B, $05, $6E, $18, $75, $9F, $5C, $CA);
  389.      ct  : ($4A, $05, $7A, $3B, $24, $D3, $97, $7B)),
  390.     (key : ($58, $40, $23, $64, $1A, $BA, $61, $76);
  391.      pt  : ($00, $4B, $D6, $EF, $09, $17, $60, $62);
  392.      ct  : ($45, $20, $31, $C1, $E4, $FA, $DA, $8E)),
  393.     (key : ($02, $58, $16, $16, $46, $29, $B0, $07);
  394.      pt  : ($48, $0D, $39, $00, $6E, $E7, $62, $F2);
  395.      ct  : ($75, $55, $AE, $39, $F5, $9B, $87, $BD)),
  396.     (key : ($49, $79, $3E, $BC, $79, $B3, $25, $8F);
  397.      pt  : ($43, $75, $40, $C8, $69, $8F, $3C, $FA);
  398.      ct  : ($53, $C5, $5F, $9C, $B4, $9F, $C0, $19)),
  399.     (key : ($4F, $B0, $5E, $15, $15, $AB, $73, $A7);
  400.      pt  : ($07, $2D, $43, $A0, $77, $07, $52, $92);
  401.      ct  : ($7A, $8E, $7B, $FA, $93, $7E, $89, $A3)),
  402.     (key : ($49, $E9, $5D, $6D, $4C, $A2, $29, $BF);
  403.      pt  : ($02, $FE, $55, $77, $81, $17, $F1, $2A);
  404.      ct  : ($CF, $9C, $5D, $7A, $49, $86, $AD, $B5)),
  405.     (key : ($01, $83, $10, $DC, $40, $9B, $26, $D6);
  406.      pt  : ($1D, $9D, $5C, $50, $18, $F7, $28, $C2);
  407.      ct  : ($D1, $AB, $B2, $90, $65, $8B, $C7, $78)),
  408.     (key : ($1C, $58, $7F, $1C, $13, $92, $4F, $EF);
  409.      pt  : ($30, $55, $32, $28, $6D, $6F, $29, $5A);
  410.      ct  : ($55, $CB, $37, $74, $D1, $3E, $F2, $01)),
  411.     (key : ($01, $01, $01, $01, $01, $01, $01, $01);
  412.      pt  : ($01, $23, $45, $67, $89, $AB, $CD, $EF);
  413.      ct  : ($FA, $34, $EC, $48, $47, $B2, $68, $B2)),
  414.     (key : ($1F, $1F, $1F, $1F, $0E, $0E, $0E, $0E);
  415.      pt  : ($01, $23, $45, $67, $89, $AB, $CD, $EF);
  416.      ct  : ($A7, $90, $79, $51, $08, $EA, $3C, $AE)),
  417.     (key : ($E0, $FE, $E0, $FE, $F1, $FE, $F1, $FE);
  418.      pt  : ($01, $23, $45, $67, $89, $AB, $CD, $EF);
  419.      ct  : ($C3, $9E, $07, $2D, $9F, $AC, $63, $1D)),
  420.     (key : ($00, $00, $00, $00, $00, $00, $00, $00);
  421.      pt  : ($FF, $FF, $FF, $FF, $FF, $FF, $FF, $FF);
  422.      ct  : ($01, $49, $33, $E0, $CD, $AF, $F6, $E4)),
  423.     (key : ($FF, $FF, $FF, $FF, $FF, $FF, $FF, $FF);
  424.      pt  : ($00, $00, $00, $00, $00, $00, $00, $00);
  425.      ct  : ($F2, $1E, $9A, $77, $B7, $1C, $49, $BC)),
  426.     (key : ($01, $23, $45, $67, $89, $AB, $CD, $EF);
  427.      pt  : ($00, $00, $00, $00, $00, $00, $00, $00);
  428.      ct  : ($24, $59, $46, $88, $57, $54, $36, $9A)),
  429.     (key : ($FE, $DC, $BA, $98, $76, $54, $32, $10);
  430.      pt  : ($FF, $FF, $FF, $FF, $FF, $FF, $FF, $FF);
  431.      ct  : ($6B, $5C, $5A, $9C, $5D, $9E, $0A, $5A))
  432.   );
  433.  
  434. function bfSelfTest: Integer;
  435. var
  436.   bfKey: TBlowFishKey;
  437.   buffer: TBlock64;
  438.   i: Integer;
  439. begin
  440.   { Test #1 }
  441.   Move(plain1^, buffer, 8 );
  442.   bfKeySetup(bfKey, key1^, StrLen(key1));
  443.   bfEncrypt(bfKey, buffer, buffer);
  444.   if not (CompareMem(@buffer[0], @cipher1[0], 8 )) then
  445.   begin
  446.     Result := -11;
  447.     exit;
  448.   end;
  449.   bfDecrypt(bfKey, buffer, buffer);
  450.   if not (CompareMem(@buffer[0], @plain1^, 8 )) then
  451.   begin
  452.     Result := -21;
  453.     exit;
  454.   end;
  455.   { Test #2 }
  456.   Move(plain2, buffer, 8 );
  457.   bfKeySetup(bfKey, key2^, StrLen(key2));
  458.   bfEncrypt(bfKey, buffer, buffer);
  459.   if not (CompareMem(@buffer[0], @cipher2[0], 8 )) then
  460.   begin
  461.     Result := -12;
  462.     exit;
  463.   end;
  464.   bfDecrypt(bfKey, buffer, buffer);
  465.   if not (CompareMem(@buffer[0], @plain2, 8 )) then
  466.   begin
  467.     Result := -22;
  468.     exit;
  469.   end;
  470.   { Test #3 }
  471.   Move(plain3, buffer, 8);
  472.   bfKeySetup(bfKey, key3, SizeOf(key3));
  473.   bfEncrypt(bfKey, buffer, buffer);
  474.   if not (CompareMem(@buffer[0], @cipher3[0], 8 )) then
  475.   begin
  476.     Result := -13;
  477.     exit;
  478.   end;
  479.   bfDecrypt(bfKey, buffer, buffer);
  480.   if not (CompareMem(@buffer[0], @plain3, 8 )) then
  481.   begin
  482.     Result := -23;
  483.     exit;
  484.   end;
  485.   { Test #4 }
  486.   for i := 0 to (SizeOf(bfTest) div SizeOf(TBfTest))-1 do
  487.   begin
  488.     bfKeySetup(bfKey, bfTest[i].key, SizeOf(bfTest[i].key));
  489.     Move(bfTest[i].pt, buffer, SizeOf(TBlock64));
  490.     bfEncrypt(bfKey, buffer, buffer);
  491.     if not CompareMem(@buffer, @bfTest[i].ct, SizeOf(TBlock64)) then
  492.     begin
  493.       Result := -100 - i ;
  494.       Exit;
  495.     end;
  496.     bfDecrypt(bfKey, buffer, buffer);
  497.     if not CompareMem(@buffer, @bfTest[i].pt, SizeOf(TBlock64)) then
  498.     begin
  499.       Result := -200 - i ;
  500.       Exit;
  501.     end;
  502.   end;
  503.   Result := 0;
  504. end;
  505.  
  506. function bfModeSelfTest: Integer;
  507. var
  508.   data: PByte;
  509.   retval: Integer;
  510.   CryptCtx: TCryptCtx;
  511. begin
  512.   retval := 0;
  513.   GetMem(data, SizeOf(bfTest));
  514.   try
  515.     Move(bfTest, data^, SizeOf(bfTest));
  516.     {Init.}
  517.     FillChar(CryptCtx, SizeOf(TCryptCtx), 0);
  518.     bfKeySetup(CryptCtx.key, key1^, StrLen(key1));
  519.     {Encrypt}
  520.     bfResetContext(cryptCtx);
  521.     bfEncryptCBC(cryptCtx, data^, SizeOf(bfTest));
  522.     {Decrypt}
  523.     bfResetContext(cryptCtx);
  524.     bfDecryptCBC(cryptCtx, data^, SizeOf(bfTest));
  525.     if not CompareMem(data, @bfTest, SizeOf(bfTest)) then
  526.       retval := -300
  527.   finally
  528.     FreeMem(data, SizeOf(bfTest));
  529.   end;
  530.   Result := retval;
  531. end;
  532.  
  533. end.

Saludos

 

¿Te gustó el post? COMPARTILO!



GetWriteINI [UNIT] [Fakedo0r]

Iniciado por Fakedo0r

Respuestas: 0
Vistas: 1427
Último mensaje Abril 19, 2012, 05:11:12 pm
por Fakedo0r
ReadWriteAPI [UNIT] [Fakedo0r]

Iniciado por Fakedo0r

Respuestas: 0
Vistas: 1391
Último mensaje Abril 19, 2012, 05:08:16 pm
por Fakedo0r
[Delphi] Unit DH Tools 0.2

Iniciado por BigBear

Respuestas: 0
Vistas: 1276
Último mensaje Mayo 14, 2016, 01:45:08 pm
por BigBear
EnumInstalledPrograms [UNIT] [Fakedo0r]

Iniciado por Fakedo0r

Respuestas: 1
Vistas: 1396
Último mensaje Abril 19, 2012, 05:47:47 pm
por Pr0ph3t
MeltFile [UNIT] [Fakedo0r]

Iniciado por Fakedo0r

Respuestas: 0
Vistas: 1361
Último mensaje Abril 19, 2012, 05:10:05 pm
por Fakedo0r