delphi 字符串加密解密(不支持中文)  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 字符串加密解密(不支持中文)


unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private

    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  key: byte;
implementation

uses  shellapi, ShlObj;

{$R *.dfm}

procedure GenerateSimleKey;
var
  tmp_str: array[0..25] of char;
  serial: DWORD;
  lkey: Byte;
begin
  SHGetSpecialFolderPath(0, @tmp_str[0], CSIDL_DESKTOP, false);
  tmp_str[3] := #0;
  asm
        push    0
        push    0
        push    0
        push    0
        lea     edi, serial
        push    edi
        push    0
        push    0
        lea     edi, tmp_str
        push    edi
        call    GetVolumeInformation
        mov     eax, serial
        mov     ecx, 4
        XOR     bl, bl

@@loop:
        XOR     bl, al
        SHR     eax, 8
        loop    @@loop
        mov     lkey, bl
  end;
  key := lkey;
end;


function HexToInt(Value: string): Integer;
var
  I: Integer;
begin
  Result := 0;
  I := 1;
  if Value = '' then
    Exit;
  if Value[1] = '$' then
    Inc(I);
  while I <= Length(Value) do
  begin
    if Value[I] in ['0'..'9'] then
      Result := (Result shl 4) or (Ord(Value[I]) - Ord('0'))
    else if Value[I] in ['A'..'F'] then
      Result := (Result shl 4) or (Ord(Value[I]) - Ord('A') + 10)
    else if Value[I] in ['a'..'f'] then
      Result := (Result shl 4) or (Ord(Value[I]) - Ord('a') + 10)
    else
      Break;
    Inc(I);
  end;
end;

function decodeString(str: string): string;
var
  i, len: Integer;
begin
  Result := '';
  len := length(str);
  for i := 1 to len do
    if i mod 2 <> 0 then
      Result := Result + Chr(HexToInt(Copy(str, i, 2)) xor key);

end;

function encodeString(str: string): string;
var
  i, len: Integer;
begin
  Result := '';
  len := length(str);
  for i := 1 to len do
    Result := Result + IntToHex(Ord(str[i]) xor key, 2);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
GenerateSimleKey;

Memo1.Text:=decodeString(encodeString('abcabc'));//;    //结果abcabc
end;

end.


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.35856103897095 seconds