zoukankan      html  css  js  c++  java
  • 一个简单的加解密算法

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;

    type
    TForm1
    = class(TForm)
    mmo: TMemo;
    mmo1: TMemo;
    mmo2: TMemo;
    key: TEdit;
    btn1: TButton;
    btn2: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    function encryptstr(const s:string; skey:string):string;
    function decryptstr(const s:string; skey:string):string;
    function myStrtoHex(s:string):string; //原字符串转16进制字符串
    function myHextoStr(S: string): string;//16进制字符串转原字符串
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}
    function TForm1.myStrtoHex(s: string): string;
    var tmpstr:string;
    i:integer;
    begin
    tmpstr :
    = '';
    for i:=1 to length(s) do
    begin
    tmpstr :
    = tmpstr + inttoHex(ord(s[i]),2);
    end;
    result :
    = tmpstr;
    end;

    function TForm1.myHextoStr(S: string): string;
    var hexS,tmpstr:string;
    i:integer;
    a:byte;
    begin
    hexS :
    =s;//应该是该字符串
    if length(hexS) mod 2=1 then
    begin
    hexS:
    =hexS+'0';
    end;
    tmpstr:
    ='';
    for i:=1 to (length(hexS) div 2) do
    begin
    a:
    =strtoint('$'+hexS[2*i-1]+hexS[2*i]);
    tmpstr :
    = tmpstr+chr(a);
    end;
    result :
    =tmpstr;
    end;

    function TForm1.encryptstr(const s:string; skey:string):string;
    var
    i,j: integer;
    hexS,hexskey,midS,tmpstr:
    string;
    a,b,c:byte;
    begin
    hexS :
    =myStrtoHex(s);
    hexskey:
    =myStrtoHex(skey);
    midS :
    =hexS;
    for i:=1 to (length(hexskey) div 2) do
    begin
    if i<>1 then midS:= tmpstr;
    tmpstr:
    ='';
    for j:=1 to (length(midS) div 2) do
    begin
    a:
    =strtoint('$'+midS[2*j-1]+midS[2*j]);
    b:
    =strtoint('$'+hexskey[2*i-1]+hexskey[2*i]);
    c:
    =a xor b;
    tmpstr :
    = tmpstr+myStrtoHex(chr(c));
    end;
    end;
    result :
    = tmpstr;
    end;

    function TForm1.decryptstr(const s:string; skey:string):string;
    var
    i,j: integer;
    hexS,hexskey,midS,tmpstr:
    string;
    a,b,c:byte;
    begin
    hexS :
    =s;//应该是该字符串
    if length(hexS) mod 2=1 then
    begin
    showmessage(
    '密文错误!');
    exit;
    end;
    hexskey:
    =myStrtoHex(skey);
    tmpstr :
    =hexS;
    midS :
    =hexS;
    for i:=(length(hexskey) div 2) downto 1 do
    begin
    if i<>(length(hexskey) div 2) then midS:= tmpstr;
    tmpstr:
    ='';
    for j:=1 to (length(midS) div 2) do
    begin
    a:
    =strtoint('$'+midS[2*j-1]+midS[2*j]);
    b:
    =strtoint('$'+hexskey[2*i-1]+hexskey[2*i]);
    c:
    =a xor b;
    tmpstr :
    = tmpstr+myStrtoHex(chr(c));
    end;
    end;
    result :
    = myHextoStr(tmpstr);
    end;


    procedure TForm1.btn1Click(Sender: TObject);
    begin
    mmo1.Text:
    =encryptstr(mmo.Text,key.Text);
    end;

    procedure TForm1.btn2Click(Sender: TObject);
    begin
    mmo2.Text:
    =decryptstr(mmo1.Text,key.Text);
    end;

    end.


    
    
  • 相关阅读:
    Less-21
    Less-22
    Less-21
    Less-20
    ssrf redis gopher
    Less19
    Less18
    Arm 系统查看、修改系统时间
    通过 grpc 请求标头发送自定义数据
    gRpc 空参数
  • 原文地址:https://www.cnblogs.com/Aricc/p/1341374.html
Copyright © 2011-2022 走看看