i'm using this method to encrypt small string's for a long time and work just fine.. when i try to pass a large string (lenght > 500) it dont work someone can tell me why?
`function Encrypt(const Text: string): string;
var
i: Byte;
Key: Word;
strTemp: string;
const
KEY = 7519;
KEY1 = 03001;
KEY2 = 002279;
begin
Key := KEY;
SetLength(strTemp, Length(Text));
Result := '';
for i := 1 to Length(Text) do
begin
strTemp[i] := Char(byte(Text[I]) xor (Key shr 8));
Result := Result + IntToHex(Byte(strTemp[i]), 2);
Key := (Byte(strTemp[i]) + KEY) * KEY1 + KEY2 ;
end;
end;`
i tried to pass a large value in base64 with approximately 5000 characters, the result after encryption is approximately 250 characters..
i'm trying to encrypt a base64 text.. base64 extracted from a .jpeg
example:
var vText: AnsiString;
vText := 'iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAgAElEQVR4XuydUbbjOJIss5bR+19oz' + '1Fla1KpS4kgCcLdI+z9XpBhbgGRfIisnn/+85///PcX/w8DGMAABsoa+Oeff8pmew1WMWelTJWyPPZdt' +'TzP31LVXO8PwS45jz788XLUmMd6+ubRh8oU//0vRwZp/aVnnzvWxU3FnNUyVcpTKcvz6VExzFgtU6U8l' +'bJUHGp/e09U693eO5G/YwADGOhggAFwhy6TEQMYaGegy0FyxZyVMpHF99FTqTcMfsf2';
Result = 6EA0B289D3DB602BC0EFCC2F2B38A54FF5916CE39FF43E4F7CACFFF7BB372D2E5485038DE606514C0BC943D55B9246C8
You tagged this question Delphi, and by that identifiers are case insensitive:var Key: Word; const KEY = 7519;
KEY
cannot be defined since Key
already exists. This might be possible in Pascal alone, tho. So please check the example you gave us against your actual code.Shouldvar i: Byte; begin for i := 1 to Length(Text) do
Length(Text)
be 304, then the loop will run 48 iterations only, because Byte
can at max hold 256 values. Should a loop end condition have a higher value than the variable is able to ever hold, the maximum amount of the variable is subtracted from that end condition. Which is 304 minus 256 equalling 48.This can be written a lot cleaner as:var strTemp: string; begin SetLength(strTemp, Length(Text)); for i := 1 to Length(Text) do begin strTemp[i] := Char(...); Result := Result + IntToHex(Byte(strTemp[i]), 2); Key := (Byte(strTemp[i]) ...)...;
var
byteTemp: Byte;
begin
for i := 1 to Length(Text) do
begin
byteTemp := Byte(Text[I]) xor (Key shr 8);
Result := Result + IntToHex(byteTemp, 2);
Key := (byteTemp + KEY0) * KEY1 + KEY2;
will differ as per Delphi version. Why not making it at leastfunction Encrypt(const Text: string): string;
AnsiString
? Or even better: TBytes
or Array of Byte
? When dealing with any kind of String
you can never be certain how many bytes one character needs. Using String
in Delphi 2009 and above uses more than 1 byte per character, so with Byte(String[i])
you're walking on thin ice.