Function VigenereExEncrypt(sSource, sKey: String; bDecrypt: Boolean = False; iTableSize: Integer = 94): String;
var
i, iPosText, iPosKey: Integer;
sTable: string;
begin
//Create our Cipher Table
i:= 32;
While i <= (iTableSize + 32) do
Begin
sTable:= ConCat(sTable, Chr(i));
inc(i);
end;
//Make the key the same size or greater than the Source
while Length(sSource) >= Length(sKey) do
sKey:= ConCat(sKey,sKey);
//Remove Line Feed & Carrage Returns from Cipher
i:=0;
while i<=Length(sSource) do
if (sSource[i]=Chr(10)) or (sSource[i]=Chr(13)) then
Delete(sSource, i, 1)
else
Inc(i);
{Uncomment if you need to remove spaces
//Remove Spaces from Cipher
i:=0;
while i<=Length(sSource) do
if sSource[i]=' ' then
Delete(sSource, i, 1)
else
Inc(i);
}
//Vegenere Encryption/Decryption routine
i:= 1;
while i <= Length(sSource) do
Begin
iPosText := pred(pos(sSource[i],sTable));
iPosKey := pred(pos(sKey[i],sTable));
//Encrypt or Decrypt(Default is Encrypt)
Case bDecrypt of
False: Result := Result + sTable[((iPosText + iPosKey) mod iTableSize) + 1];
True : Result := Result + sTable[(((iPosText + iTableSize) - iPosKey) mod iTableSize) + 1];
end;
inc(i);
end;
end;
Beispiele :
// Crypt
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Text:= VigenereExEncrypt(Trim(Memo1.Text), Edit1.Text, True);
end;
// Decrypt
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text:= VigenereExEncrypt(Trim(Memo1.Text), Edit1.Text);
end;
Keine Kommentare:
Kommentar veröffentlichen