Delphi String Encryption/Decryption

Source: Internet
Author: User

Unit uencrypt_decrypt; interface uses Sysutils; const xorkey:array[0..7] of Byte = ($B 2, $09, $AA, $55, $9 3, $6d, $84, $47);  //through key key encryption function encryptstring (source, key:string): String;function unencryptstring (source , key:string): string; //XOR/Cryptographic function Enc (str:string): String;function Dec (str:string): string;   Implementation function encryptstring (Source, key:string): string;var  Keylen,keypos,offset,srcpos, srcasc,range:integer;  dest:string;begin  Keylen: = Length (Key);  If Keylen = 0 then    Key: = ' Delphi ';  keypos: = 0;  Range: = 256;  randomize;  Offset: = Random (Range);  Dest: = Format ('%1 '). 2x ', [Offset]);  for Srcpos: = 1 to Length (Source) do  begin    SRCASC: = (Ord (Source[srcpos]) + OFFSE T) mod 255;    If Keypos < Keylen then      Keypos: = Keypos + 1    else  &nbsp ;   Keypos: = 1;    SRCASC: = SrCAsc xor Ord (Key[keypos]);    Dest: = Dest + Format ('%1.2x ', [SRCASC]);    Offset: = srcasc;  End ;  Result: = Dest;end; function unencryptstring (Source, key:string): string;var  Keylen,keypos,offset, srcpos,srcasc,tmpsrcasc:integer;  dest:string;begin  Keylen: = Length (Key);  If Keylen = 0 then  &nb Sp Key: = ' Delphi ';  keypos: = 0;  Offset: = Strtoint (' $ ' + copy (Source, 1, 2));  Srcpos: = 3;  repeat&nb Sp   SRCASC: = Strtoint (' $ ' + copy (Source, Srcpos, 2));    If Keypos < Keylen then      KEYP OS: = Keypos + 1    else      Keypos: = 1;    TMPSRCASC: = Srcasc xor Ord (Key[keypos]);     If TMPSRCASC <= Offset then      TMPSRCASC: = 255 + tmpsrcasc-offset    ELSE&N Bsp     TMPSRCASC: = tmpsrcasc-offset;    Dest: = Dest + chr (TMPSRCASC);    Offset: = srcasc;& nbsp   SrCPos: = Srcpos + 2;  until    srcpos >= Length (Source);  Result: = Dest;end; function Enc (str: String): string;var  I, j:integer;begin  Result: = ';  J: = 0;  for I: = 1 to Length (str) do  begin    Result: = result + Inttohex (Byte (Str[i]) xor Xorkey[j], 2);    J: = (j + 1) mod 8;  end; End; function Dec (str:string): string;var  i, j:integer;begin  Result: = ';  J: = 0;  for I: = 1 to Length (str) div 2 do  begin    Result: = result + Char (strtoint (' $ ' + Copy (str, I * 2-1, 2)) XOR&NB Sp   XORKEY[J]);    J: = (j + 1) mod 8;  end;end; end. ====================================== ======-based DES encryption and decryption:  http://blog.csdn.net/warrially/article/details/8089041

Delphi String Encryption/decryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.