游乐游手机版
首页/网络安全/文章详情

常见加密算法全面总结与分类详解

时间:2026-07-24 22:27
针对逆向工程与注册机编写实践,整理了一批Delphi函数集,涵盖字符串累加、反转、截取等通用处理工具及经典加密算法实现,部分算法源自CrackMe逆向改写,代码实用优先,便于快速构建注册机。

学习软件破解与逆向工程时,编写注册机(KeyGen)是必不可少的实践环节。这里整理了一批在实战中积累下来的Delphi函数集,既包含字符串处理的通用工具,也实现了多种经典加密算法,还有从某些CrackMe(CM)中逆向分析后复现的算法。代码风格可能偏简陋,但实用性优先。

Delphi 字符串基础操作函数(注册机常用)

这些函数用于处理注册名、机器码等字符串的常见变换,例如累加ASCII值、提取中间部分、反转字符串、过滤字符等。

取累加值(ASCII十进制与十六进制)

这是最简单的校验算法之一:将字符串每个字符的ASCII码累加,结果可转换为十进制或十六进制。许多早期共享软件采用这种简易验证方式。

function wzwgp(s: string): string; //取累加值
var i,sum:integer;
begin 
   sum:=0; for i:=1 to length(s) do 
begin 
   sum:=sum + ord(s[i]);
end; 
   Result :=inttostr(sum);
end;

function ASCII10ADD(s: string): string; //取累加值
var i,sum:integer;
begin
   sum:=0; for i:=1 to length(s) do
begin
   sum:=sum + ord(s[i]);
end;
   Result :=inttostr(sum);
end;

function ASCII16ADD(s: string): string; //取累加值(十六进制)
var i,sum:integer;
begin
   sum:=0; for i:=1 to length(s) do
begin
   sum:=sum + ord(s[i]);
end;
   Result :=inttohex(sum,2);
end;

字符串反转与中间截取

字符串反转常用于打乱字符顺序;取中间部分则用于从特定位置提取子串。

function StrToBack(s: string): string;    //将字符串倒转过来
var i:integer;
begin
    for i:=1 to length(s) do
    begin
    result :=s[i] + result;
    end;
end;

function mdistr(str:string;int:integer):string; //取字符串的中间部份
begin
if int

字符串与ASCII码互转

将字符串转换为十六进制或十进制表示的ASCII序列,在分析序列号时经常用到。

function StrToASCII16(s: string): string;      //字符串转换ascii码16进制
var i:integer;
begin
    for i:=1 to length(s) do
    begin
    result := result + IntToHex(ord(s[i]),2);
    end;
end;

function StrToASCII10(s: string): string;    //字符串转换ascii码10进制
var i:integer;
begin
    for i:=1 to length(s) do
    begin
    result:= result + inttostr(ord(s[i]));
    end;
end;

// 带分隔符的十六进制,如“$BA,$DA,$D2”
function StrToASCII16(s: string): string;      //字符串转换ascii码16进制,
var i:integer;                  // 如:黑夜彩虹=$BA,$DA,$D2,$B9,$B2,$CA,$BA,$E7
begin
    for i:=1 to length(s) do
    begin
    result := result + '$' + IntToHex(ord(s[i]),2) + ',';
    end;
    Result:=copy(Result,0,Length(result)-1);
end;

取偶数位字符与汉字提取

偶数位提取常用于混淆后的恢复;取出汉字则用于处理中文字符串。

function DoubleStr(Str: string): string;    //取字符串偶位数字符
var
i: Integer;
begin
   Result := '';
   for i := 2 to Length(Str) do
   if i mod 2 = 0 then
   Result := Result + Str[i];
end;

function WideStr(str:string):String;   //取出字符串中的汉字
var I: Integer;
begin
    for I := 1 to Length(WideString(Str)) do
    if Length(string(WideString(Str)[I])) = 2 then
    result:= result + WideString(Str)[I];
end;

统计子串出现次数与过滤指定字符

function StrSubCount(const Source,Sub:string):integer; //判断某字符在字符串中的个数
var Buf:string;
    Len,i:integer;
begin
   Result:=0;
   Buf:=Source;
   i:=Pos(Sub, Buf);
   Len:=Length(Sub);
while i <> 0 do
    begin
    Inc(Result);
    Delete(Buf,1,i+Len-1);
    i:=Pos(Sub,Buf);
end;
end;

function SiftStr(Str: string): string; //过滤字符串(只保留0-9,a-f,A-F)
var i,j:integer;
begin
    Result:='';
    j:=Length(str);
    for i:=0 to j do
    begin
    if str[i] in ['0'..'9','a'..'f','A'..'F'] then
    Result:=Result + str[i];
    end;
end;

移位字符串与逐位XOR运算

这两种操作经常出现在序列号生成中:前者将两个字符串交错合并,后者对每个字符按常量表做XOR。

function ShiftStr(str1,str2:string):string; //移位字符串(交错合并)
var i:integer;
begin
    Result:='';
    for i:=1 to length(str1) do
    begin
    Result:=Result + str1[i] + str2[i];
    end;
end;

function OpeateStr(const s :string): string; //字符逐位 xor 运算
    const
    snLen = 5 ;
    sn:array[1..snLen] of Integer =($0D, $01, $14, $05,$02);
    var
    i,n: integer;
    begin
    setLength(result,Length(s));
    for i :=1 to Length(s) do begin
    n := i mod snLen ;
    if n = 0 then
    n := 5 ;
    result[i] := char(ord(s[i]) xor sn[n]);
    end;
end;

加密/解密与注册机算法实战

以下是几种实际逆向分析中遇到的注册算法实现,包括销售王进销存的KeyGen、通用XOR加密、加法加密,以及一个完整的异或加解密流程。

销售王进销存 KeyGen 算法(StrToEncrypt)

此函数模拟了某商业软件的序列号生成方式:将用户名与ID拼接后,与密码逐位XOR,再对产生的特殊字符进行处理,最后按四位一组加横线。

function StrToEncrypt(Str,ID,Pass:string): string;        //销售王进销存_keygen算法
var
username: string;
a, b, c_str, c_hex, d, e, f: string;
I, a_len: Integer;
begin
    username:=str;
     a:=id + str;
     //b:= 'MraketSoft62095231';
     b:=pass;
     a_len := Length(a);
c_str := '';
c_hex := '';
for I := 1 to a_len do
begin
    c_hex := c_hex + IntToHex(Byte(a[I]) xor Byte(b[I mod Length(b)]), 2) + ' ';
    c_str := c_str + Chr(Byte(a[I]) xor Byte(b[I mod Length(b)]));
end;
d := '';
for I := 1 to Length(c_str) do
begin
   if Byte(c_str[I]) in [$01..$09,$0A..$0F] then
      d := d + QuotedStr('#$' + IntToHex(Byte(c_str[I]), 1))
    else d := d + c_str[I];
end;
d := '''' + d + '''';
e := '';
for I := 1 to Length(d) do
begin
    if d[I] in ['0'..'9','a'..'z','A'..'Z'] then e := e + d[I];
end;
f := '';
for I := 1 to Length(e) do
begin
    f := f + e[I];
    if (I mod 4 = 0)and(I

通用字符串与十六进制互转

function myStrtoHex(s: string): string;       //原字符串转16进制字符串
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 myHextoStr(S: string): string;           //16进制字符串转原字符串
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;

多轮异或加密/解密(encryptstr / decryptstr)

该加密过程先将密钥转为十六进制,再对原文进行多轮逐位XOR,每轮使用密钥的一位。解密时反向操作即可。

function 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 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;

//调用示例
//Edit2.Text :=encryptstr(Edit1.Text,Editkey.Text);

最简单的XOR加密/解密与加法加密

单字节XOR是最基础的加密方式,加解密使用同一个函数。加法加密则需要分开实现。

// XOR 加密/解密
function XorEncDec(AStr:String;Key:Byte):String;
var
i,n:Integer;
begin
n:=Length(AStr);
SetLength(Result,n);
for i:=1 to n do
    Result[i]:=Char(Byte(AStr[i]) xor Key);
end;

//加法加密
function AddEnc(AStr:String;Key:Byte):String;
var
i,n:Integer;
begin
n:=Length(AStr);
SetLength(Result,n);
for i:=1 to n do
    Result[i]:=Char(Byte(AStr[i]) + Key);
end;

//加法解密
function AddDec(AStr:String;Key:Byte):String;
var
i,n:Integer;
begin
n:=Length(AStr);
SetLength(Result,n);
for i:=1 to n do
    Result[i]:=Char(Byte(AStr[i]) - Key);
end;

其中XorEncDec的加密/解密均为同一个过程,而加法加密、解密则需要两个过程配套使用。

procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text:=XorEncDec(Edit1.Text,123); //加密(Edit1中存放明文,Edit2存放密文)
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.Text:=XorEncDec(Edit2.Text,123); //解密(Edit2存放密文,Edit1中存放解密的明文)
end;

算法趣味题与收集的CM算法

四位数字的全排列(经典练习题:1、2、3、4能组成多少个无重复的三位数?)

function permutation( int:integer ):string;
var
i,j,k:integer;
begin
for i:=1 to int do
for j:=1 to int do
for k:=1 to int do
begin
if (i<>j) and (i<>k) and (j<>k)then
result:=result + inttostr(i) + inttostr(j) + inttostr(k) + #13 + #10;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     Memo1.Clear;
     Memo1.Lines.Add(permutation(4));
     label1.Caption:=inttostr(memo1.Lines.Count);
end;

acafeel 算法(来自逆向分析)

这是一个从某个CrackMe中提取的注册算法,包含多层字符串拼接和字符替换,最终输出5段形式的注册码。

function acafeel(Name:string):string;
var
strA,strB, strC : string;
sum, pos : integer;
begin
if Name ='' then exit;
for pos := 1 to length(Name) do
    if (ord(Name[pos]) < $20) or (ord(Name[pos]) > $7E) then
      begin
        showmessage('请输入字母或者数字,不支持中文!');
        exit;
      end;
sum := ord(Name[1]) * length(Name) * $64;
strA := ' ' + intTostr(sum) + 'NoName SwordMan nOnAME';
strB := strA[$12] + (strA[$7] + strA[$8]) + strA[$9] + strA[$5] + strA[$3] +
          strA[$1] + (strA[$14] + strA[$15] + strA[$16] + strA[$17] + strA[$18]) +
          (strA[$D] + strA[$E]) + strA[$8];
for pos := 1 to length(strB) do
    if (ord(strB[pos]) <> $20) then strC := strC + strB[pos];
if length(strC) < 14 then
    begin
      strC := strC + copy(strA, 7, 23);
      strC := copy(strC, 1, 15) + 'bywjy';
    end;
Result := copy(strC, 1, 5) + '-' + copy(strC, 5, 4) + '-' + copy(strC, 8, 4) +
          '-' + copy(strC, 11, 4) + '-' + copy(strC, 14, 7); 
end;

acafeel2 算法(更复杂的运算)

该算法对用户名长度做了分支判断,引入了大量数学运算和字符串反序操作,最后生成形如“RHM-xxxxxxxxx”的注册码。

function acafeel2(Name:string):string;
var
temp1, temp2, temp3,
tempA, tempB, tempC1, tempC2, tempD1, tempD2,
pos, posSTR, posADD, posSUB : integer;
begin
    if length(Name) < 5 then
    begin
    showmessage('注册名的长度必须大于4位数!');
    exit;
    end;
    //注册名长度5-9
    if (5 <= length(Name)) and (length(Name) <= 9) then
    begin
    temp1 := ((ord(Name[1]) + $56B) xor $890428) + $18;
    temp2 := ((ord(Name[4]) + length(Name)) xor $54) xor $25D;
    temp3 := (ord(Name[1]) + $56B) * $1024;
    tempA := ((temp1 * temp2) + $400) + temp3 ;
     for pos := 2 to length(Name) do
     begin
     temp1 := temp1 + ((ord(Name[pos]) + $56B) xor $890428);
     temp2 := ((ord(Name[4]) + length(Name)) xor $54) xor $25D;
     temp3 := (ord(Name[pos]) + $56B) * $1024;
     tempA := tempA + (temp1 * temp2) + temp3;
     end;
     end;
    //注册名长度>9
    if length(Name) > 9 then
    begin
     temp1 := ((ord(Name[1]) + $56B) xor $890428) + $18;
     temp2 := (((ord(Name[4]) + length(Name)) xor $54) xor $25D) * $400;
     temp3 := ((ord(Name[1]) + $56B) * $1024) + $400;
     tempA := temp3;
     for pos := 2 to length(Name) do
     begin
     temp1 := temp1 + temp2 + ((ord(Name[pos]) + $56B) xor $890428);
     temp2 := (((ord(Name[4]) + length(Name)) xor $54) xor $25D) * temp3;
     temp3 := temp3 + ((ord(Name[pos]) + $56B) * $1024);
     tempA := temp3;
     end;
     temp1 := temp1 + temp2;
     end;
     //小循环1
    tempB := ord(Name[5+1]) + $32 + $134A;
     for posSTR := length(Name) downto 1 do
     begin
     Name := Name + Name[posSTR];
     end;
     posSTR := length(Name) div 2;
     Name := copy(Name, posSTR+1, posSTR);
    for pos := 4 downto 1 do
    begin
    tempB := tempB + ord(Name[pos+1]) + $134A;
    for posSTR := length(Name) downto 1 do
    begin
    Name := Name + Name[posSTR];
    end;
    posSTR := length(Name) div 2;
    Name := copy(Name, posSTR+1, posSTR);
    end;
    //小循环2
    tempC1 := ord(Name[1]) + tempB + $134A;
    tempC2 := ((ord(Name[2]) + $23) * $25A) + temp1;
    posADD := 2;
    for pos := 4 downto 1 do
    begin
    posADD := posADD + 1;
    tempC1 := tempC1 + ord(Name[1]) + $134A;
    tempC2 := tempC2 + ((ord(Name[posADD]) + $23) * $25A);
    if (posADD = 4) or (posADD = 5) then
    begin
    for posSTR := length(Name) downto 1 do
    begin
    Name := Name + Name[posSTR];
    end;
    posSTR := length(Name) div 2;
    Name := copy(Name, posSTR+1, posSTR);
    end;
    end;
    //最后检测
    tempD1 := (tempC2 + $3C) xor ($1337 - ord(Name[3]));
    tempD2 := (tempC1 + tempA) xor ($18 - ord(Name[6]));
    Result:= 'RHM' + '-' + inttostr(tempD1) + inttostr(tempD2);
end;

johnroot 写的注册机改写(不懂算法的CM)

这个算法相对简单:对16字节的name做两次不同常量的XOR,然后对结果进行变换,最后拼接成注册码。

function johnroot(Name:string):string;
var
nameok,gg,gg2,mm,mm2:pchar;
i,j,j2,k:integer;
begin
getmem(nameok,$10);
ZeroMemory(nameok,$10);
getmem(mm,5);
ZeroMemory(mm,5);
getmem(mm2,5);
ZeroMemory(mm2,5);
for i:=0 to (length(name)-1) do
begin
nameok[i]:=Name[i];
end;
j:=0;
for i:=0 to $f do
begin
   k:=ord(nameok[i]) xor $82;
   j:=j + k;
end;
gg := pchar(inttostr(j));
j:=0;
for i:=0 to $f do
begin
   k:=ord(nameok[i]) xor $28;
   j2:=j2 + k;
end;
gg2 := pchar(inttostr(j2));
if length(gg2)<4 then
begin
gg2:=pchar('0' + string(gg2));
end;
for i:=0 to 3 do
begin
   mm[i]:= char($69 - ord(gg[i]));
end;
for i:=0 to 3 do
begin
   mm2[i]:= char($69 - ord(gg2[i]));
end;
Result:=string(gg) + string(gg2) + string(mm) + string(mm2);
end;

这些代码大部分来自早期学习逆向时的实践,有不少直接改写自网上其他兄弟的成果。核心思路就是理解算法后,用Delphi重现一遍。对于初学者来说,多读多改这些现成的实现,比从头造轮子更能快速进步。

来源:https://www.jb51.net/hack/5140.html
上一篇网页加密完全攻略网站安全防护全面教程 下一篇PHPWIND与Discuz! CSRF漏洞安全分析
本站内容用于信息整理与展示,如有侵权或内容问题请及时联系处理。

相关推荐

补充同频道和同主题内容,方便继续浏览更多相关内容。

同类最新

继续查看同栏目最近更新的文章。

更多
零基础Python网络安全学习指南,包住宿实战培训
网络安全 · 2026-07-25

零基础Python网络安全学习指南,包住宿实战培训

先抛出几个核心判断:网络安全人才缺口确实已经超过70万。这个数字不算夸张,各大招聘网站上安全岗位的薪资涨幅和需求增长都是实实在在的。但实话实说,行业真正缺的是能上手干活的人,不是只懂几个理论概念的新手。因此,一套扎实且成体系的训练方案,对于零基础转行或进阶提升就显得格外关键。 从零基础到能够独立应对

网络安全定义与核心概念详解
网络安全 · 2026-07-25

网络安全定义与核心概念详解

谈到网络安全,不同组织与标准体系对其定义虽有差异,但核心目标高度统一:保护信息与系统不受威胁侵害。以下梳理几种主流定义—— 什么是网络安全? 国际标准化组织(ISO)在ISO-74982文献中将安全定义为:最大程度地降低数据和资源遭受攻击的可能性。该定义虽简短,却精准点出安全的本质——即风险控制。

IIS 7.0网站漏洞利用与修复方法指南
网络安全 · 2026-07-25

IIS 7.0网站漏洞利用与修复方法指南

先来聊聊当前网络安全领域比较流行的一种实战技巧——以PHP环境为例,详细讲解图片马合并与解析漏洞利用的具体操作步骤。整个过程并不复杂,但细节往往决定成败,大家跟着流程逐步操作即可。 合并一张PHP一句话图片马 首先,我们需要将一句话木马脚本与一张正常图片合并,生成一个看似无害的图片文件。合并方式主要

织梦管理系统后台查找功能教程
网络安全 · 2026-07-25

织梦管理系统后台查找功能教程

通过SQL注入漏洞获取织梦CMS(DedeCMS)管理员密码后,满怀期待地准备进入后台,却发现怎么也找不到登录入口——这种情况其实相当常见。别着急,这里有一个实用的技巧值得尝试。 直接在网站地址后面添加以下路径: include dialog select_media php?f=form1 mur

微软IE浏览器BrowseDialog类(ccrpbds6.dll)拒绝服务安全漏洞深度分析
网络安全 · 2026-07-25

微软IE浏览器BrowseDialog类(ccrpbds6.dll)拒绝服务安全漏洞深度分析

BrowseDialog Class (ccrpbds6 dll) 导致 Internet Explorer 拒绝服务漏洞分析 该漏洞的PoC(概念验证代码)实现极为直接——触发浏览器崩溃的条件简单得令人意外。测试环境采用Windows XP Professional SP2(已安装全部补丁)并运行