开发环境:DelphiXE11.3
做个笔记
-----------------------
--------------------------
链接1是某位大佬的源码,被俺收藏了;
链接2是本人从链接1中提取一部分,改了一点点东西;(比如下面的代码
fRSAOpenSSL.PublickEncrypt(aRSAData,memPubKey.Text); //公钥加密(公钥是字符串类型)
链接1中不支持这种加密,链接一中的公钥是文件的形式;
)
只有部分代码,不展示全部代码,只是测试的例子;仅供参考;
Unit
unit Unit2; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, System.JSON, RSAOpenSSL, DBGridEhGrouping, ToolCtrlsEh, DBGridEhToolCtrls, DynVarsEh, EhLibVCL, GridsEh, DBAxisGridsEh, DBGridEh; type TForm2 = class(TForm) Panel1: TPanel; Panel2: TPanel; PageControl1: TPageControl; TabSheet1: TTabSheet; edtUserNO: TEdit; Button1: TButton; Button2: TButton; Label1: TLabel; Label2: TLabel; edtUserPassword: TEdit; memPubKey: TMemo; memKoten: TMemo; IdHTTP1: TIdHTTP; memUserPasswordEncrypt: TMemo; edtStoreId: TEdit; edtMerchantId: TEdit; Label3: TLabel; Label4: TLabel; Label5: TLabel; edtAgencyId: TEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormCreate(Sender: TObject); private fRSAOpenSSL : TRSAOpenSSL; FToken:string; function DealPemStr(const AStr :String):string; { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation uses uHttpAbout; {$R *.dfm} procedure TForm2.Button1Click(Sender: TObject); var vStr:string; JsonRoot :TJSONObject; vPem : string; begin fun_Http_Get(vStr); JsonRoot := TJSONObject.ParseJSONValue(vStr) as TJSONObject; if JsonRoot = nil then Exit; try vPem := JsonRoot.Values['data'].Value;// DealPemStr(JsonRoot.Values['data'].Value); memPubKey.Text:=vPem; memPubKey.Lines.SaveToFile('PubKey.pem',TEncoding.UTF8) finally JsonRoot.Free; end; end; procedure TForm2.Button2Click(Sender: TObject); var aRSAData: TRSAData; vPostStr, vStr:string; JsonRoot, JsonData, JsonSub1, JsonSub2:TJSONObject; JsonArray:TJSONArray; begin aRSAData.DecryptedData := edtUserPassword.Text; fRSAOpenSSL.PublickEncrypt(aRSAData,memPubKey.Text); //公钥加密(公钥是字符串类型) if aRSAData.ErrorResult = 0 then memUserPasswordEncrypt.Text := aRSAData.CryptedData else memUserPasswordEncrypt.Lines.Add(aRSAData.ErrorMessage); JsonData := TJSONObject.Create; JsonData.AddPair('account', edtUserNO.Text);// JsonData.AddPair('loginType', 3); JsonData.AddPair('password', memUserPasswordEncrypt.Text); //memUserPasswordEncrypt.Text vPostStr:= JsonData.ToString; fun_Http_Post(vPostStr,vStr); JsonRoot := TJSONObject.ParseJSONValue(vStr) as TJSONObject; JsonData.Free; if JsonRoot = nil then Exit; try if vStr<>'' then begin edtStoreId.Text := ''; edtMerchantId.Text := ''; edtAgencyId.Text := ''; if JsonRoot.Values['success'].Value='true' then begin memKoten.Text := vStr; //ShowMessage(JsonRoot.Values['data'].ToJSON ); JsonSub1:=TJSONObject.ParseJSONValue(JsonRoot.Values['data'].ToJSON ) as TJSONObject; if JsonSub1<> nil then begin JsonSub2:=TJSONObject.ParseJSONValue(JsonSub1.Values['currentUser'].ToJSON ) as TJSONObject; edtStoreId.Text :=''; //JsonRoot.Values['storeId'].Value; edtMerchantId.Text := JsonSub2.Values['merchantId'].Value; edtAgencyId.Text := JsonSub2.Values['agencyId'].Value; FToken:=JsonSub1.Values['token'].Value ; end; //if then end else begin ShowMessage(JsonRoot.Values['message'].Value+',登入失败!') ; end; end; finally JsonRoot.Free; if JsonSub1 <>nil then JsonSub1.Free; if JsonSub2 <>nil then JsonSub2.Free; end; end; function TForm2.DealPemStr(const AStr: String): string; var vStr, vSubStr:string; begin vStr := AStr; if vStr='' then Result := ''; vSubStr := '-----BEGIN PUBLIC KEY-----'#10; while Length(vStr)>0 do begin vSubStr := vSubStr+Copy(vStr,1,64)+#10; vStr := Copy(vStr,65,1000); end; Result := vSubStr+'-----END PUBLIC KEY-----'; //'-----BEGIN PUBLIC KEY-----'#13#10+JsonRoot.Values['data'].Value+#13#10'-----END PUBLIC KEY-----'; end; procedure TForm2.FormCreate(Sender: TObject); var aPathToPublickKey: string; begin aPathToPublickKey := 'PubKey.pem'; fRSAOpenSSL := TRSAOpenSSL.Create(aPathToPublickKey, ''); end; procedure TForm2.FormDestroy(Sender: TObject); begin fRSAOpenSSL.Free; end; end.
某个键值对不存在时去访问会报错 showmessage(vJsonData.Values['storeId'].Value)//当没有这个键storeId时,这么访问会报错 /////////////////////// var vJsonData :TJSONObject; vstoreId: string; if vJsonData.FindValue('storeId')<>nil then//判断这个storeId键是否存在 showmessage(vJsonData.Values['storeId'].Value); vJsonData.TryGetValue('storeId', vstoreId);//这样写不报错
Form
object Form2: TForm2 Left = 0 Top = 0 Caption = 'Form2' ClientHeight = 556 ClientWidth = 822 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -12 Font.Name = 'Segoe UI' Font.Style = [] OnCreate = FormCreate OnDestroy = FormDestroy TextHeight = 15 object Panel1: TPanel Left = 0 Top = 0 Width = 822 Height = 41 Align = alTop TabOrder = 0 ExplicitWidth = 818 object Label3: TLabel Left = 24 Top = 18 Width = 37 Height = 15 Caption = #38376#24215'ID' end object Label4: TLabel Left = 213 Top = 18 Width = 37 Height = 15 Caption = #21830#23478'ID' end object Label5: TLabel Left = 410 Top = 18 Width = 50 Height = 15 Caption = #20195#29702#21830'ID' end object edtStoreId: TEdit Left = 64 Top = 12 Width = 128 Height = 23 ReadOnly = True TabOrder = 0 end object edtMerchantId: TEdit Left = 253 Top = 12 Width = 134 Height = 23 ReadOnly = True TabOrder = 1 end object edtAgencyId: TEdit Left = 466 Top = 12 Width = 153 Height = 23 ReadOnly = True TabOrder = 2 end end object Panel2: TPanel Left = 0 Top = 41 Width = 822 Height = 515 Align = alClient Caption = 'Panel2' TabOrder = 1 ExplicitWidth = 818 ExplicitHeight = 514 object PageControl1: TPageControl Left = 1 Top = 1 Width = 820 Height = 513 ActivePage = TabSheet1 Align = alClient TabOrder = 0 object TabSheet1: TTabSheet Caption = #30331#20837#39564#35777 object Label1: TLabel Left = 25 Top = 138 Width = 65 Height = 15 Alignment = taRightJustify Caption = #29992#25143#36134#21495#65306 end object Label2: TLabel Left = 25 Top = 167 Width = 65 Height = 15 Alignment = taRightJustify Caption = #29992#25143#23494#30721#65306 end object edtUserNO: TEdit Left = 93 Top = 134 Width = 209 Height = 23 TabOrder = 0 Text = 'SSSSSS' end object Button1: TButton Left = 93 Top = 3 Width = 75 Height = 25 Caption = #33719#21462#20844#38053 TabOrder = 1 OnClick = Button1Click end object Button2: TButton Left = 77 Top = 241 Width = 75 Height = 25 Caption = #30331#20837 TabOrder = 2 OnClick = Button2Click end object edtUserPassword: TEdit Left = 93 Top = 163 Width = 209 Height = 23 TabOrder = 3 Text = '12345678' end object memPubKey: TMemo Left = 25 Top = 34 Width = 649 Height = 94 Lines.Strings = ( 'memPubKey') TabOrder = 4 end object memKoten: TMemo Left = 16 Top = 312 Width = 721 Height = 168 Lines.Strings = ( 'memKoten') ScrollBars = ssVertical TabOrder = 5 end object memUserPasswordEncrypt: TMemo Left = 184 Top = 216 Width = 553 Height = 89 Lines.Strings = ( 'memUserPasswordEncrypt') TabOrder = 6 end end end end object IdHTTP1: TIdHTTP ProxyParams.BasicAuthentication = False ProxyParams.ProxyPort = 0 Request.ContentLength = -1 Request.ContentRangeEnd = -1 Request.ContentRangeStart = -1 Request.ContentRangeInstanceLength = -1 Request.Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' Request.BasicAuthentication = False Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)' Request.Ranges.Units = 'bytes' Request.Ranges = <> HTTPOptions = [hoForceEncodeParams] Left = 277 Top = 92 end end