Mam taki mały problem....Potrzebuje jakiegoś algorytmu szyfrowania tekstu, który jest odwracalny....Na początku miałem XOR, ale okazało się, że dane liczbowe są źle szyfrowane...I teraz się zastanawiam, jaki algorytm użyć.....Jaki algorytm polecacie i gdzie mogę znaleźć jakieś jego zastosowanie ?? (mile widziany kod)
0
0
program cipher;
uses
SysUtils;
{$APPTYPE CONSOLE}
type
TParams = record
inputFile, outputFile : string;
codeMethode : (cmNone, cmXor, cmAdd, cmRot13);
key : byte;
end;
const
progName = 'cipher';
progVer = '1.0';
function Rot13(ch : Char) : Char;
begin
if (upcase(ch)>='A') and (upcase(ch)<='M') then result:=chr(ord(ch)+13)
else
if (upcase(ch)>='N') and (upcase(ch)<='Z') then result:=chr(ord(ch)-13)
else
result:=ch;
end;
function xorChar(ch : Char; key : byte) : Char;
begin
result := chr(ord(ch) xor key);
end;
function addChar(ch : Char; key : shortint) : Char;
begin
result := chr(ord(ch) + key);
end;
var
i : byte;
j : word;
params : TParams;
predParam, succParam, lineIn, lineOut : string;
syntaxError : boolean;
inputFile, outputFile : text;
begin
syntaxError := false;
params.codeMethode := cmNone;
if paramcount = 0 then
begin // gdy nie ma parametrów wyswietl informacje
writeLn(progName +' '+ progVer +' '+ ' - (c) Andrzej Tomicki 2003 [email protected]');
writeLn;
writeLn(' -i input_file (eg: C:\myFile.txt)');
writeLn(' -o output_file (eg: C:\myCodedFile.txt)');
writeLn(' -c code_methode ([rot13], [xor], [add])');
writeLn(' -i key (eg: 13)');
writeLn;
writeLn('eg : cipher -i in.txt -o out.txt -c xor -k 30');
end
else
begin
for i := 1 to paramcount do // zczytaj parametry do rekordu
begin
try
predParam := lowerCase(paramstr(i));
succParam := lowerCase(paramstr(i+1));
if predParam = '-i' then params.inputFile := paramstr(i+1);
if predParam = '-o' then params.outputFile := paramstr(i+1);
if predParam = '-c' then
if succParam='xor' then params.codeMethode := cmXor
else
if succParam='add' then params.codeMethode := cmAdd
else
if succParam='rot13' then params.codeMethode := cmRot13
else
begin
syntaxError := true;
writeLn('Unknown coding methode : ' + succParam);
end;
if predParam = '-k' then params.key := StrToInt(succParam);
except
syntaxError := true;
writeLn('Syntax error...');
end;
end;
if (params.key = 0)and(params.codeMethode<>cmRot13) then // gdy nie podano klucza (nie dotyczy Rot13)
begin
syntaxError := true;
writeLn('Enter key value [eg : -k 13]');
end;
if not syntaxError then
if fileExists(params.inputFile) then
if params.outputFile = '' then
writeLn('Enter output file name..')
else
if params.codeMethode = cmNone then
writeLn('Enter coding methode [eg : -c add]')
else
begin // kodowanie
assign(inputFile , params.inputFile);
assign(outputFile, params.outputFile);
reset(inputFile);
rewrite(outputFile);
while not(eof(inputFile)) do
begin
lineOut := '';
readln(inputFile, lineIn);
for j := 1 to length(lineIn) do
begin
if params.codeMethode = cmXor then // xor
lineOut := lineOut + xorChar(lineIn[j], params.key) else
if params.codeMethode = cmRot13 then // rot13
lineOut := lineOut + rot13(lineIn[j]) else
if params.codeMethode = cmAdd then // add
lineOut := lineOut + addChar(lineIn[j], params.key)
end; //for
writeLn(outputFile, lineOut);
end;
close(inputFile);
close(outputFile);
writeLn('Done..');
end
else // (not fileExists)
if params.inputFile = '' then
writeLn('Enter file name to code/decode..')
else
writeLn('File '''+params.inputFile+''' doesn''t exists..');
end;
end.