Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Excerpt

There are lots of way can encrypt a

...

text string based on

...

a key. By the approach, you can use a single numerical key, or text string. In terms of security's point of view, text string-based key has better security performance.

Below code is an example of string encryption module.

Code Block
languagedelphi
function Encrypt(const InString:string; Salt:string): string;
var
  i : Byte;
  StartKey, MultKey, AddKey: Word;
begin
  Result := '';
  if (Salt = '') then begin
    Result := InString;
  end
  else begin
    StartKey := Length(Salt);
    MultKey := Ord(Salt[1]);
    AddKey := 0;
    for i := 1 to Length(Salt) - 1 do AddKey := AddKey + Ord(Salt[i]);
    for i := 1 to Length(InString) do
    begin
      Result := Result + CHAR(Byte(InString[i]) xor (StartKey shr 8))
Code Block
const CKEY1 = 53761;
      CKEY2 = 32618;

function EncryptStr(const S :WideString; Key: Word): String;
var   i          :Integer;
      RStrStartKey := (Byte(Result[i]) + StartKey) * MultKey + :RawByteStringAddKey;
    end;
  RStrB      :TBytes Absolute RStrend;
end;

function Decrypt(const InString:string; Salt:string): string;
var
  i : Byte;
  StartKey, MultKey, AddKey: Word;
begin
  Result := '';
  RStr:= UTF8Encode(S);

  if (Salt = '') then begin
    Result := InString;
  end
  else begin
    StartKey := Ord(Salt[1]);
    MultKey := Length(Salt);
    AddKey := 0;
    for i := 01 to Length(RStrSalt) - 1 do begin
AddKey := AddKey + RStrBOrd(Salt[i]);
    for i := RStrB1 to Length(InString) do
    begin
      Result := Result + CHAR(Byte(InString[i]) xor (KeyStartKey shr 8));
    Key  StartKey := (Byte(RStrBInString[i]) + KeyStartKey) * CKEY1MultKey + CKEY2AddKey;
    end;
  end;
end;

By the way, above was not helpful for me - I use multibyte character, and below code was so helpful even though I can't use any key on the documentation

Code Block
uses IdCoder, IdCoderMIME, IdGlobal;

..
..

begin
    // encode string
    sMasterXML.Text i := 0 to Length(RStr)-1 do beginTIdEncoderMIME.EncodeString(sMasterXML.Text, IndyTextEncoding_UTF8);

	// deocde string
    sMasterXML.Text Result:= TIdDecoderMIME.DecodeString(sMasterXML.Text, IndyTextEncoding_UTF8);

end;

Below is combined encryption algorithm which provides much better secure results.

Code Block
languagedelphi
uses IdCoder, IdCoderMIME, IdGlobal;

.
.
.


const CK_ENC_VAR_RANGE=10Result + IntToHex(RStrB[i], 2);
  end;
end;

function DecryptStrCKEncrypt(const SSource: Stringstring; KeySalt: Wordstring): Stringstring;
var
   i, tmpKeylenSalt, key, keyWeight:Integer Word;
  InString: string;
begin
  Result RStr       :RawByteString:= '';
  if (Salt = '') RStrBthen begin
    Result :TBytes= Absolute RStrSource;
  end else begin
  tmpStr  InString :=  :stringTIdEncoderMIME.EncodeString(Source, IndyTextEncoding_UTF8);
begin
    lenSalt tmpStr:= UpperCaseLength(SSalt);

  SetLength(RStr, Length(tmpStr) div 2)  keyWeight := 0;
    for i := 1 to lenSalt do keyWeight := keyWeight + ord(Salt[i]);
   try keyWeight := keyWeight mod CK_ENC_VAR_RANGE;

    whilefor (i := 1 <to Length(tmpStrInString)) do
    begin
       RStrB[i div 2]:= StrToInt('$' + tmpStr[i] + tmpStr[i+1]key := ( ord(Salt[(i mod lenSalt) + 1]) mod CK_ENC_VAR_RANGE);
      Result := Result + CHAR(Byte(InString[i]) + key - keyWeight);
    end;
   Inc(i, 2);
    end;
  except
    Result:= '';
    Exit;
  end;
  end;
end;

function CKDecrypt(const InString:string; Salt:string): string;
var
  i, lenSalt, key, keyWeight: Word;
  Target: string;
begin
  if (Salt = '') then begin
    Target := InString;
  end else begin
    lenSalt := Length(Salt);

    keyWeight := 0;
    for i := 01 to Length(RStr)-1 do begin lenSalt do keyWeight := keyWeight + ord(Salt[i]);
    keyWeight := keyWeight mod CK_ENC_VAR_RANGE;

    tmpKeyTarget := RStrB[i]'';
    for RStrB[i]i := 1 to Length(InString) do
    begin
      key := ( RStrBord(Salt[(i] xor (Key shr 8 mod lenSalt) + 1]) mod CK_ENC_VAR_RANGE);
     Key Target := (tmpKeyTarget + KeyCHAR(Byte(InString[i]) *- CKEY1key + CKEY2keyWeight);
    end;
    Result := UTF8Decode(RStr)TIdDecoderMIME.DecodeString(Target, IndyTextEncoding_UTF8);
  end;
end;

Example{ Testng :}

procedure TForm1.btn1ClickButton1Click(Sender: TObject);
begin
  txt2Memo2.Text := EncryptStrCKEncrypt(txt1Memo1.Text, 223'foo');
  lbl1Memo3.CaptionText := DecryptStrCKDecrypt(txt2Memo2.Text, 223'foo');
end;