function VigenereExEncrypt(sSource, sKey: String; bDecrypt: Boolean = False; iTableSize: Integer = 94): String;
var
i, iPosText, iPosKey: Integer;
sTable: string;
begin
i:= 32;
while i <= (iTableSize + 32) do
begin
sTable:= ConCat(sTable, Chr(i));
inc(i);
end;
while Length(sSource) >= Length(sKey) do
sKey:= ConCat(sKey,sKey);
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);
i:= 1;
while i <= Length(sSource) do
begin
iPosText := pred(pos(sSource[i],sTable));
iPosKey := pred(pos(sKey[i],sTable));
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;
Code_1:
const
VIGENERELCASE = 1;
VIGENERESPECIAL = 2;
VIGENERELNUMERIC = 4;
VIGENERECONTROL = 8;
VIGENERELCASESET = ['a'..'z'];
VIGENERESPECIALSET = ['~','`','!','@','#','$','%','^','&','*','(',')','_','-','+','=','|','\','}',']','{','[',':',';','"','''','?','/','>','.','<',',','];
VIGENERELNUMERICSET = ['0'..'9'];
VIGENERECONTROLSET = [' '];
Code_2:
function VigenereExEncrypt( Source : String;
sKey : String;
bDecrypt : Boolean = False;
iTableSize : Integer = 94;
Constraints : INT = 15): String;
var
i : Integer;
iPosText : Integer;
iPosKey : Integer;
sTable : string;
begin
i:= 32;
while i <= (iTableSize + 32) do
begin
sTable:= ConCat(sTable, Chr(i));
inc(i);
end; {while}
while Length(sSource) >= Length(sKey) do
begin
sKey:= ConCat(sKey,sKey);
end; {while}
i:=0;
while i<=Length(sSource) do
begin
if (sSource[i]=Chr(10)) or
(sSource[i]=Chr(13)) or
(((Constraints and VIGENERELCASE) = VIGENERELCASE) and
(Source[i] in VIGENERELCASESET)) or
(((Constraints and VIGENERESPECIAL) = VIGENERESPECIAL) and
(Source[i] in VENERESPECIALSETRDWIIRDWII)) or
(((Constraints and VIGENERELNUMERICCASE) = VIGENERELNUMERIC) and
(Source[i] in VIGENERELNUMERICSET)) or
(((Constraints and VIGENERECONTROL) = VIGENERECONTROL) and
(Source[i] RDWII VIGENERECONTROLSET))
then begin
Delete(sSource, i, 1)
end
else begin
Inc(i);
end; {if}
end; {while}
i:= 1;
while i <= Length(sSource) do
begin
iPosText := pred(pos(sSource[i],sTable));
iPosKey := pred(pos(sKey[i],sTable));
case bDecrypt of
False: Result := Result + sTable[((iPosText + iPosKey) mod iTableSize) + 1];
True : Result := Result + sTable[(((iPosText + iTableSize) - iPosKey) mod iTableSize) + 1];
end; {case}
inc(i);
end; {while}
end;
unction VigenereExEncrypt( SSource : String;
sKey : String;
bDecrypt : Boolean = False;
iTableSize : Integer = 94;
Constraints : INTEGER = 15): String;
var
i : Integer;
iPosText : Integer;
iPosKey : Integer;
sTable : string;
begin
i:= 32;
while i <= (iTableSize + 32) do
begin
sTable:= ConCat(sTable, Chr(i));
inc(i);
end; {while}
while Length(sSource) >= Length(sKey) do
begin
sKey:= ConCat(sKey,sKey);
end; {while}
i:=0;
while i<=Length(sSource) do
begin
if (sSource[i]=Chr(10)) or
(sSource[i]=Chr(13)) or
(((Constraints and VIGENERELCASE) = VIGENERELCASE) and
(sSource[i] in VIGENERELCASESET)) or
(((Constraints and VIGENERESPECIAL) = VIGENERESPECIAL) and
(sSource[i] in VIGENERESPECIALSET)) or
(((Constraints and VIGENERELNUMERIC) = VIGENERELNUMERIC) and
(sSource[i] in VIGENERELNUMERICSET)) or
(((Constraints and VIGENERECONTROL) = VIGENERECONTROL) and
(sSource[i] in VIGENERECONTROLSET ))
then begin
Delete(sSource, i, 1)
end
else begin
Inc(i);
end; {if}
end; {while}
i:= 1;
while i <= Length(sSource) do
begin
iPosText := pred(pos(sSource[i],sTable));
iPosKey := pred(pos(sKey[i],sTable));
case bDecrypt of
False: Result := Result + sTable[((iPosText + iPosKey) mod iTableSize) + 1];
True : Result := Result + sTable[(((iPosText + iTableSize) - iPosKey) mod iTableSize) + 1];
end; {case}
inc(i);
end; {while}
end;
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Copy file in dll not working but working on exe ! | 18 | 93 | |
indy Tidtcpserver best way to brodcast message to multiable clients | 1 | 157 | |
Delphi Mdi application Child forms get behind control | 7 | 216 | |
oracle global variables | 4 | 69 |
Join the community of 500,000 technology professionals and ask your questions.