if RSADecrypt(注册码,公钥,模数)=用户名 then begin MessageBox(Handle,'注册成功','提示',MB_OK); end else MessageBox(Handle,'注册失败','提示',MB_OK);
示例代码如下:function HexToInt(Hex:String):Integer;var I,Res:Integer; ch:Char;begin Res:= 0; for I := 0 to Length(Hex) do begin ch:=Hex[I+1]; if (ch>=0) and (ch<='9') then Res := Res 16 + Ord(ch) - Ord('0') else if (ch>='A') and (ch <= 'F') then Res := Res 16 + Ord(ch) - Ord('A') + 10 else if (ch >= 'a') and (ch <='f') then Res:= Res 16 + Ord(ch)-Ord('a') + 10 else raise Exception.Create('Error'); end; Result := Res;end;//以128位例子,256位,512位,768位,1024位的一样function RSADescryption(InStr,ModStr,KeyStr:String): String;var Str,Temp:String; I:Integer; RSA:TLBRSA;begin Str:= ''; for I := 0 to Length(InStr) div 2 - 1 do begin Temp := Copy(InStr,I 2 + 1,2); Str := Str + Chr(HexToInt(Temp)); end; RSA := TLBRSA.Create(nil); RSA.KeySize := aks128; RSA.PrivateKey.ModulusAsString := ModStr; RSA.PrivateKey.ExponentAsString := KeyStr; Result := RSA.DecryptString(Str);end;procedure TForm2.Button1Click(Sender: TObject);begin try if RSAEncryption(edt1.Text,'51757B748A942803C8D558CFD9C5AD83','591B') = edt2.Text then MessageBox(Handle,'注册成功','提示',MB_OK) else MessageBox(Handle,'注册失败','提示',MB_OK) except MessageBox(Handle,'注册失败','提示',MB_OK) end;end;
RSA算法的注册机方式如下://字符串变换成十六进制function StrToHex(Instr:String): String;var StrResult,Temp:String; i:Integer;begin StrResult := ''; Temp := ''; for I := 0 to Length(InStr) - 1 do begin Temp := Format('%x',[Ord(InStr[I+1])]); if Length(Temp) = 1 then Temp := '0' + Temp; StrResult := StrResult + Temp; end; Result := StrResult;end;//以128位为例子,256 位,512位、768位、1024位的一样function REAEncryption(InStr,ModStr,KeyStr:String): String;var RSA:TLBRSA; TempResult:String;begin TempResult := ''; RSA := TLBRSA.Create(nil); RSA.KeySize := aks128; RSA.PrivateKey.ModulusAsString := ModStr; RSA.PrivateKey.ExponentAsString := KeyStr; Result := RSA.DecryptString(Str); RSA.Free; Result := StrToHex(TempResult);end;procedure TForm2.Button2Click(Sender: TObject);begin edt1.Text := RSAEncryption(edt2.Text,'51757B748A942803C8D558CFD9C5AD83','C1ECF3CB8C36BBFCC3D427BD1EB97A15');end;
(图片来源网络,侵删)
0 评论