Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. function TElgamalCipher.CheckNumbers(p, x, k: Integer): Integer;
  2. begin
  3. if not IsPrime(P) or (P < 256) then
  4. Result := 1
  5. else
  6. if (x >= p - 1) or (x <= 1) then
  7. Result := 2
  8. else
  9. if (GetGCD(k, p - 1) <> 1) or (k >= p - 1) or (k <= 1) then
  10. Result := 3
  11. else
  12. Result := 0;
  13. end;
  14.  
  15. procedure TElgamalCipher.EncryptFile(const InputFileName, OutputFileName: string;
  16. p, g, y, k: Integer);
  17. var
  18. InputBuff: ByteBuff;
  19. OutputBuff: IntBuff;
  20. InputFile, OutputFile: TFileStream;
  21. i, ByteCounter: Integer;
  22. ACipher, BCipher: Int64;
  23. NotInformationShown: Boolean;
  24. begin
  25. InputFile := TFileStream.Create(InputFilename, fmOpenRead);
  26. OutputFile := TFileStream.Create(OutputFileName, fmCreate);
  27. ByteCounter := 0;
  28. NotInformationShown := True;
  29. ACipher := FastModPower(g, k, p);
  30. repeat
  31. ByteCounter := InputFile.Read(InputBuff, BytesToUpdate);
  32. for i := 0 to ByteCounter - 1 do
  33. begin
  34. BCipher := FastModPower(y, k, p);
  35. BCipher := (BCipher * InputBuff[i]) mod p;
  36. OutputBuff[i * 2] := ACipher;
  37. OutputBuff[i * 2 + 1] := BCipher;
  38. end;
  39. OutputFile.Write(OutputBuff, ByteCounter * 8);
  40. if NotInformationShown then
  41. begin
  42. MainForm.FillMemo(OutputBuff, ByteCounter * 2);
  43. NotInformationShown := False;
  44. end;
  45. until ByteCounter < BytesToUpdate;
  46. InputFile.Free;
  47. OutputFile.Free;
  48. end;
  49.  
  50. procedure TElgamalCipher.DecryptFile(const InputFileName, OutputFileName: string;
  51. x, p: Int64);
  52. var
  53. InputBuff: IntBuff;
  54. OutputBuff: ByteBuff;
  55. InputFile, OutputFile: TFileStream;
  56. i, ByteCounter: Integer;
  57. Decipher: Integer;
  58. NotInformationShown: Boolean;
  59. TempVal: Int64;
  60. begin
  61. InputFile := TFileStream.Create(InputFilename, fmOpenRead);
  62. OutputFile := TFileStream.Create(OutputFileName, fmCreate);
  63. ByteCounter := 0;
  64. NotInformationShown := True;
  65. repeat
  66. ByteCounter := InputFile.Read(InputBuff, BytesToUpdate * 2 * 4);
  67. for i := 0 to (ByteCounter div 8) - 1 do
  68. begin
  69. TempVal := InputBuff[i * 2 + 1];
  70. TempVal := (FastModPower(InputBuff[i * 2], (p - 2) * x, p) * TempVal) mod p;
  71. OutputBuff[i] := TempVal;
  72. end;
  73. OutputFile.Write(OutputBuff, ByteCounter div 8);
  74. if NotInformationShown then
  75. begin
  76. MainForm.FillMemo(OutputBuff, ByteCounter div 8);
  77. NotInformationShown := False;
  78. end;
  79. until ByteCounter < BytesToUpdate;
  80. InputFile.Free;
  81. OutputFile.Free;
  82. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement