Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function TElgamalCipher.CheckNumbers(p, x, k: Integer): Integer;
- begin
- if not IsPrime(P) or (P < 256) then
- Result := 1
- else
- if (x >= p - 1) or (x <= 1) then
- Result := 2
- else
- if (GetGCD(k, p - 1) <> 1) or (k >= p - 1) or (k <= 1) then
- Result := 3
- else
- Result := 0;
- end;
- procedure TElgamalCipher.EncryptFile(const InputFileName, OutputFileName: string;
- p, g, y, k: Integer);
- var
- InputBuff: ByteBuff;
- OutputBuff: IntBuff;
- InputFile, OutputFile: TFileStream;
- i, ByteCounter: Integer;
- ACipher, BCipher: Int64;
- NotInformationShown: Boolean;
- begin
- InputFile := TFileStream.Create(InputFilename, fmOpenRead);
- OutputFile := TFileStream.Create(OutputFileName, fmCreate);
- ByteCounter := 0;
- NotInformationShown := True;
- ACipher := FastModPower(g, k, p);
- repeat
- ByteCounter := InputFile.Read(InputBuff, BytesToUpdate);
- for i := 0 to ByteCounter - 1 do
- begin
- BCipher := FastModPower(y, k, p);
- BCipher := (BCipher * InputBuff[i]) mod p;
- OutputBuff[i * 2] := ACipher;
- OutputBuff[i * 2 + 1] := BCipher;
- end;
- OutputFile.Write(OutputBuff, ByteCounter * 8);
- if NotInformationShown then
- begin
- MainForm.FillMemo(OutputBuff, ByteCounter * 2);
- NotInformationShown := False;
- end;
- until ByteCounter < BytesToUpdate;
- InputFile.Free;
- OutputFile.Free;
- end;
- procedure TElgamalCipher.DecryptFile(const InputFileName, OutputFileName: string;
- x, p: Int64);
- var
- InputBuff: IntBuff;
- OutputBuff: ByteBuff;
- InputFile, OutputFile: TFileStream;
- i, ByteCounter: Integer;
- Decipher: Integer;
- NotInformationShown: Boolean;
- TempVal: Int64;
- begin
- InputFile := TFileStream.Create(InputFilename, fmOpenRead);
- OutputFile := TFileStream.Create(OutputFileName, fmCreate);
- ByteCounter := 0;
- NotInformationShown := True;
- repeat
- ByteCounter := InputFile.Read(InputBuff, BytesToUpdate * 2 * 4);
- for i := 0 to (ByteCounter div 8) - 1 do
- begin
- TempVal := InputBuff[i * 2 + 1];
- TempVal := (FastModPower(InputBuff[i * 2], (p - 2) * x, p) * TempVal) mod p;
- OutputBuff[i] := TempVal;
- end;
- OutputFile.Write(OutputBuff, ByteCounter div 8);
- if NotInformationShown then
- begin
- MainForm.FillMemo(OutputBuff, ByteCounter div 8);
- NotInformationShown := False;
- end;
- until ByteCounter < BytesToUpdate;
- InputFile.Free;
- OutputFile.Free;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement