testbf2.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2023 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {
  11. Blowfish 2 test.
  12. Based on http://pccipher.free.fr/blowfish2/blowfish2.txt
  13. }
  14. program testbf2;
  15. uses blowfish2, blowfish, sysutils;
  16. Procedure Test1;
  17. var
  18. L,R : QWord;
  19. ctx : TBlowfish2Context;
  20. begin
  21. (* Plaintext 128-bit block :$00000000000000010000000000000002 *)
  22. L := QWord($0000000000000001);
  23. R := QWord($0000000000000002); // 64 bits L + 64 bits R = 128-bit block
  24. Writeln(format('Plaintext 128-bit block: %x %x',[L, R]));
  25. Writeln('Key: TESTKEY');
  26. Blowfish2_Init (@ctx, 'TESTKEY', 7);
  27. Blowfish2_Encrypt(@ctx, L, R);
  28. Writeln('Ciphertext 128-bit block: ');
  29. Writeln(format('%X %X',[L, R]));
  30. if (L = QWord($7B2B9DE71D1B1C62)) and (R = QWord($91C230351177BEE8)) then
  31. Writeln('Test encryption OK.')
  32. else
  33. Writeln('Test encryption failed');
  34. Blowfish2_Decrypt(@ctx, L, R);
  35. if (L = 1) and (R = 2) then
  36. Writeln('Test decryption OK.')
  37. else
  38. Writeln('Test decryption failed.');
  39. end;
  40. Procedure Test2;
  41. var
  42. L,R : QWord;
  43. ctx : TBlowfish2Context;
  44. begin
  45. (* Plaintext 128-bit block :$01020304050607080910111213141516 *)
  46. L:=QWord($0102030405060708);
  47. R:=QWord($0910111213141516);
  48. Writeln(format('Plaintext 128-bit block : %x %x',[L, R]));
  49. Writeln('Key: A');
  50. Blowfish2_Init (@ctx, 'A', 1);
  51. Blowfish2_Encrypt(@ctx, L, R);
  52. Writeln('Ciphertext 128-bit block: ');
  53. Writeln(Format('%x %x', [L, R]));
  54. if (L = QWord($CA38165603F9915C)) and (R = QWord($61F0776A0F55E807)) then
  55. Writeln('Test encryption OK.')
  56. else
  57. Writeln('Test encryption failed.');
  58. Blowfish2_Decrypt(@ctx, L,R);
  59. if (L=QWord($0102030405060708)) and (R = QWord($0910111213141516)) then
  60. Writeln('Test decryption OK.')
  61. else
  62. Writeln('Test decryption failed.');
  63. end;
  64. Procedure Test3;
  65. var
  66. L,R : QWord;
  67. ctx : TBlowfish2Context;
  68. begin
  69. (* Plaintext 128-bit block :$01020304050607080910111213141516 *)
  70. L:=QWord($0102030405060708);
  71. R:=QWord($0910111213141516);
  72. Writeln(Format('Plaintext 128-bit block: %x %x',[L, R]));
  73. Writeln('Key: B');
  74. Blowfish2_Init(@ctx, 'B', 1);
  75. Blowfish2_Encrypt(@ctx, L, R);
  76. Writeln('Ciphertext 128-bit block: ');
  77. Writeln(format('%x %x', [L, R]));
  78. if (L = QWord($D07690A78B109983)) and (R = QWord($8DDF85826F2366C2)) then
  79. Writeln('Test encryption OK.')
  80. else
  81. Writeln('Test encryption failed.');
  82. Blowfish2_Decrypt(@ctx, L, R);
  83. if (L = QWord($0102030405060708)) and (R = QWord($0910111213141516)) then
  84. Writeln('Test decryption OK.')
  85. else
  86. Writeln('Test decryption failed.');
  87. end;
  88. begin
  89. Test1;
  90. Test2;
  91. Test3;
  92. end.