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 unigui从数据表里下载流文件

Delphi实现win10下Delphi 10.3.1 inline hook 修改mac网卡地址之getadaptersaddresses

Delphi 搭配hotkeyedit控件来解决问题

Delphi 网卡工作状态检测

Delphi 检查程序是否在(vm,vpc等)虚拟机运行 Delphi(测试可用)

Delphi xe7android应用启用蓝牙

Delphi 在windows右键菜单中加上关联

纯真ip数据库解析Delphi d10.1下正常使用

Delphi 中窗体form显示在第二个显示器中的方法

Delphi savelog 日志

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



打赏

取消

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

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

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

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

评论

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