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.

相关阅读 >>

Delphi datamodule1 fdconnection1数据库连接

Delphi xe8 form.onshow的一个小问题

Delphi 对int64计算的一种处理方式

Delphi unicode 转 gbk

Delphi整理三(窗体和基本组件)

Delphi application.messagebox 详解

Delphi tms web core 判断是否包含汉字

Delphi 判断网络是否连通

Delphi 双击隐藏tabsheet

Delphi中编写无输出函数名的dll文件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...