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
CodePudding user response:
- Your program cannot compile as you show it in this example, because of
You tagged this question Delphi, and by that identifiers are case insensitive:var Key: Word; const KEY = 7519;
KEY
cannot be defined sinceKey
already exists. This might be possible in Pascal alone, tho. So please check the example you gave us against your actual code. - Your loop variable is not big enough:
Shouldvar i: Byte; begin for i := 1 to Length(Text) do
Length(Text)
be 304, then the loop will run 48 iterations only, becauseByte
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 codition. Which is 304 minus 256 equals 48. - Why do you need a temporary String when you only need one single character? And even then you only treat is as Byte anyway, so why not using that?
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;
- Never encrypt text - encrypt bytes instead. The declaration
will differ as per Delphi version. Why not making it at leastfunction Encrypt(const Text: string): string;
AnsiString
? Or even better:TBytes
orArray of Byte
? When dealing with any kind ofString
you can never be certain how many bytes one character needs. UsingString
in Delphi 2009 and above uses more than 1 byte per character, so withByte(String[i])
you're walking on thin ice.