// 加密方法一(通过密钥加密解密)
function EncryptString(Source, Key: string): string;
function UnEncryptString(Source, Key: string): string;
//加密方法二(通过移位加密解密)
function Encode(Str: string): string;
function Decode(Str: string): string;
//加密方法三(异或加密解密)
function Enc(str: string): string;
function Dec(str: string): string;
---------------
function TFrmRegister.EncryptString(Source, Key: string): string;
// 对字符串加密(Source:源 Key:密匙)
var
KeyLen: integer;
KeyPos: integer;
Offset: integer;
Dest: string;
SrcPos: integer;
SrcAsc: integer;
Range: integer;
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]) + Offset) mod 255;
if KeyPos < KeyLen then
KeyPos := KeyPos + 1
else
KeyPos := 1;
SrcAsc := SrcAsc xor Ord(Key[KeyPos]);
Dest := Dest + format('%1.2x', [SrcAsc]);
Offset := SrcAsc;
end;
result := Dest;
end;
---------------
function TFrmRegister.UnEncryptString(Source, Key: string): string;
// 对字符串解密(Src:源 Key:密匙)
var
KeyLen: integer;
KeyPos: integer;
Offset: integer;
Dest: string;
SrcPos: integer;
SrcAsc: integer;
TmpSrcAsc: integer;
begin
KeyLen := Length(Key);
if KeyLen = 0 then
Key := 'delphi';
KeyPos := 0;
Offset := strtoint('$' + copy(Source, 1, 2));
SrcPos := 3;
repeat
SrcAsc := strtoint('$' + copy(Source, SrcPos, 2));
if KeyPos < KeyLen then
KeyPos := KeyPos + 1
else
KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
if TmpSrcAsc <= Offset then
TmpSrcAsc := 255 + TmpSrcAsc - Offset
else
TmpSrcAsc := TmpSrcAsc - Offset;
Dest := Dest + chr(TmpSrcAsc);
Offset := SrcAsc;
SrcPos := SrcPos + 2;
until SrcPos >= Length(Source);
result := Dest;
end;
----------------
function TFrmRegister.Decode(Str: string): string;
var
TmpChr: AnsiChar;
i, Len: Integer;
begin
Result := Str;
len := Length(Result);
TmpChr := result[len];
for i := Len downto 2 do
begin
Result[i] := result[i - 1];
Result[1] := TmpChr;
end;
end;
function TFrmRegister.Encode(Str: string): string;
var
TmpChr: AnsiChar;
i, Len: Integer;
begin
Result := Str;
len := Length(Result);
TmpChr := result[1];
for i := 1 to len - 1 do
begin
Result[i] := result[i + 1];
Result[len] := TmpChr;
end;
end;
---------
XorKey: array[0..7] of Byte = ($B2, $09, $AA, $55, $93, $6D, $84, $47);
function TFrmRegister.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 TFrmRegister.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
XorKey[j]);
j := (j + 1) mod 8;
end;
end;
=====================2014-08-22整理后如下=========================
unit UMiMaApi; interface uses System.Classes, System.SysUtils; /// <summary> /// 经过测试此类的方法通常仅仅支持英文和符号,不支持中文,通常密码也只能英文无法中文 /// </summary> type TMiMaApi = class const XorKey: array[0..7] of Byte = ($B2, $09, $AA, $55, $93, $6D, $84, $47); public /// <summary> /// 加密方法一(通过密钥加密解密),对字符串加密(Source:源 Key:密匙) /// </summary> function JiaMiByMiYao(ASource, AKey: string): string; function JieMiByMiYao(ASource, AKey: string): string; /// <summary> /// 加密方法二(通过移位加密解密) /// </summary> function JiaMiByYiWei(AStr: string): string; function JieMiByYiWei(AStr: string): string; /// <summary> /// 加密方法三(异或加密解密) /// </summary> function JiaMiByYiHuo(AStr: string): string; function JieMiByYiHuo(AStr: string): string; end; implementation function TMiMaApi.JiaMiByMiYao(ASource, AKey: string): string; var KeyLen: integer; KeyPos: integer; Offset: integer; Dest: string; SrcPos: integer; SrcAsc: integer; Range: integer; begin KeyLen := Length(AKey); if KeyLen = 0 then begin AKey := 'delphi'; end; KeyPos := 0; Range := 256; randomize; Offset := random(Range); Dest := format('%1.2x', [Offset]); for SrcPos := 1 to Length(ASource) do begin SrcAsc := (Ord(ASource[SrcPos]) + Offset) mod 255; if KeyPos < KeyLen then begin KeyPos := KeyPos + 1; end else begin KeyPos := 1; end; SrcAsc := SrcAsc xor Ord(AKey[KeyPos]); Dest := Dest + format('%1.2x', [SrcAsc]); Offset := SrcAsc; end; Result := Dest; end; function TMiMaApi.JieMiByMiYao(ASource, AKey: string): string; var KeyLen: integer; KeyPos: integer; Offset: integer; Dest: string; SrcPos: integer; SrcAsc: integer; TmpSrcAsc: integer; begin KeyLen := Length(AKey); if KeyLen = 0 then begin AKey := 'delphi'; end; KeyPos := 0; Offset := strtoint('$' + copy(ASource, 1, 2)); SrcPos := 3; repeat SrcAsc := strtoint('$' + copy(ASource, SrcPos, 2)); if KeyPos < KeyLen then begin KeyPos := KeyPos + 1; end else begin KeyPos := 1; end; TmpSrcAsc := SrcAsc xor Ord(AKey[KeyPos]); if TmpSrcAsc <= Offset then begin TmpSrcAsc := 255 + TmpSrcAsc - Offset; end else begin TmpSrcAsc := TmpSrcAsc - Offset; end; Dest := Dest + chr(TmpSrcAsc); Offset := SrcAsc; SrcPos := SrcPos + 2; until SrcPos >= Length(ASource); Result := Dest; end; function TMiMaApi.JiaMiByYiWei(AStr: string): string; var TmpChr: Char; i,Len: Integer; begin Result := AStr; len := Length(Result); TmpChr := result[1]; for i := 1 to len - 1 do begin Result[i] := result[i + 1]; Result[len] := TmpChr; end; end; function TMiMaApi.JieMiByYiWei(AStr: string): string; var TmpChr: Char; i,Len: Integer; begin Result := AStr; len := Length(Result); TmpChr := result[len]; for i := Len downto 2 do begin Result[i] := result[i - 1]; Result[1] := TmpChr; end; end; function TMiMaApi.JiaMiByYiHuo(AStr: string): string; var i,j: Integer; begin Result := ''; j := 0; for i := 1 to Length(AStr) do begin Result := Result + IntToHex(Byte(AStr[i]) xor XorKey[j], 2); j := (j + 1) mod 8; end; end; function TMiMaApi.JieMiByYiHuo(AStr: string): string; var i,j: Integer; begin Result := ''; j := 0; for i := 1 to Length(AStr) div 2 do begin Result := Result + Char(StrToInt('$' + Copy(AStr, i * 2 - 1, 2)) xor XorKey[j]); j := (j + 1) mod 8; end; end; end.
本文来自博客园,作者:del88,转载请注明原文链接:https://www.cnblogs.com/del88/p/3929207.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人