mainform.pas
来自「著名的SecureBlackBox控件完整源码」· PAS 代码 · 共 966 行 · 第 1/3 页
PAS
966 行
SetLength(Buf, F.{$ifndef DELPHI_NET}Size{$else}Length{$endif});
if F.{$ifndef DELPHI_NET}Size{$else}Length{$endif} > 0 then
{$ifndef DELPHI_NET}
F.Read(Buf[0], F.Size);
{$else}
F.Read(Buf, 0, F.Length);
{$endif}
FreeAndNil(F);
HMACKeyData.Key.Key := Buf;
Verifier.HMACKey := HMACKeyData;
end
else
begin
if Assigned(Cert) then
begin
X509KeyData := TElXMLKeyInfoX509Data.Create(False);
X509KeyData.Certificate := Cert;
Verifier.KeyData := X509KeyData;
end
else
begin
RSAKeyData := TElXMLKeyInfoRSAData.Create(True);
RSAKeyData.RSAKeyMaterial.Passphrase := frmSign.Passphrase;
X509KeyData := TElXMLKeyInfoX509Data.Create(True);
PGPKeyData := TElXMLKeyInfoPGPData.Create(True);
{$ifndef DELPHI_NET}
F := TFileStream.Create(frmSign.KeyFile, fmOpenRead or fmShareDenyWrite);
{$else}
F := FileStream.Create(frmSign.KeyFile, FileMode.Open, FileAccess.Read);
{$endif}
try
RSAKeyData.RSAKeyMaterial.LoadPublic(F);
except
end;
if not RSAKeyData.RSAKeyMaterial.PublicKey then
begin
F.Position := 0;
try
RSAKeyData.RSAKeyMaterial.LoadSecret(F);
except
end;
end;
if not RSAKeyData.RSAKeyMaterial.PublicKey then
begin
F.Position := 0;
LoadCertificate(F, frmSign.Passphrase, X509KeyData);
end;
if not RSAKeyData.RSAKeyMaterial.PublicKey and
not Assigned(X509KeyData.Certificate) then
begin
F.Position := 0;
PGPKeyData.PublicKey := TElPGPPublicKey.Create;
try
PGPKeyData.PublicKey.LoadFromStream(F);
except
PGPKeyData.PublicKey.Free;
PGPKeyData.PublicKey := nil;
end;
if not Assigned(PGPKeyData.PublicKey) then
begin
F.Position := 0;
PGPKeyData.SecretKey := TElPGPSecretKey.Create;
PGPKeyData.SecretKey.Passphrase := frmSign.Passphrase;
try
PGPKeyData.SecretKey.LoadFromStream(F);
except
PGPKeyData.SecretKey.Free;
PGPKeyData.SecretKey := nil;
end;
end;
end;
FreeAndNil(F);
if RSAKeyData.RSAKeyMaterial.PublicKey then
Verifier.KeyData := RSAKeyData
else
if Assigned(X509KeyData.Certificate) then
Verifier.KeyData := X509KeyData
else
if Assigned(PGPKeyData.PublicKey) or
Assigned(PGPKeyData.SecretKey) then
Verifier.KeyData := PGPKeyData
else
raise EXMLError.Create('Key not loaded.');
end;
end;
if not Verifier.ValidateSignature then
begin
SigOK := False;
MessageDlg('Signature is invalid', mtError, [mbOK], 0);
end;
end
else
SigOK := False;
end;
if SigOK then
begin
if Verifier.ValidateReference(nil) then
MessageDlg('Signature and refences validated successfully.', mtInformation, [mbOK], 0)
else
if MessageDlg('Signature validated successfully.'#13#10'Do you want to validate references?', mtInformation, [mbYes, mbNo], 0) = mrYes then
frmReferences.ShowModal;
if Assigned(XAdESVerifier) and Assigned(XAdESVerifier.QualifyingProperties) then
begin
if MessageDlg('Do you want to view XAdES information?', mtInformation, [mbYes, mbNo], 0) = mrYes then
frmXAdES.ShowModal;
end;
end;
finally
FreeAndNil(Verifier);
FreeAndNil(XAdESVerifier);
FreeAndNil(HMACKeyData);
FreeAndNil(RSAKeyData);
FreeAndNil(X509KeyData);
FreeAndNil(PGPKeyData);
end;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
FXMLDocument := TElXMLDOMDocument.Create;
cbCharset.Items.BeginUpdate;
cbCharset.Items.Add('');
EnumCharsets(EnumCharsetsCallback, cbCharset);
cbCharset.Items.EndUpdate;
UpdateXML;
end;
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
FreeAndNil(FXMLDocument);
end;
procedure TfrmMain.LoadCertificate(F: {$ifndef DELPHI_NET}TFileStream{$else}FileStream{$endif};
const Password: string; X509KeyData: TElXMLKeyInfoX509Data);
var
CertFormat : TSBCertFileFormat;
begin
X509KeyData.Certificate := TElX509Certificate.Create(nil);
try
CertFormat := X509KeyData.Certificate.DetectCertFileFormat(F);
F.Position := 0;
case CertFormat of
cfDER: X509KeyData.Certificate.LoadFromStream(F);
cfPEM: X509KeyData.Certificate.LoadFromStreamPEM(F, Password);
cfPFX: X509KeyData.Certificate.LoadFromStreamPFX(F, Password);
else
X509KeyData.Certificate.Free;
X509KeyData.Certificate := nil;
end;
except
X509KeyData.Certificate.Free;
X509KeyData.Certificate := nil;
end;
end;
procedure TfrmMain.sbBrowseXMLFileClick(Sender: TObject);
begin
dlgOpenXML.InitialDir := ExtractFilePath(Application.ExeName) + 'Samples';
dlgOpenXML.FileName := edXMLFile.Text;
if dlgOpenXML.Execute then
edXMLFile.Text := dlgOpenXML.FileName;
end;
procedure TfrmMain.tvXMLChange(Sender: TObject; Node: TTreeNode);
var
N: TElXMLDOMNode;
s, nt: string;
begin
if Assigned(tvXML.Selected) and
Assigned(tvXML.Selected.Data) then
begin
N := TElXMLDOMNode(tvXML.Selected.Data);
if N is TElXMLDOMAttr then
s := TElXMLDOMAttr(N).NodeValue
else
s := N.OuterXML;
dlbNamespaceURI.Caption := N.NamespaceURI;
mmXML.Text := AdjustLineBreaks(s);
if N is TElXMLDOMAttr then
nt := 'Attribute'
else
if N is TElXMLDOMElement then
begin
if Assigned(N.ParentNode) and
not (N.ParentNode is TElXMLDOMDocument) then
nt := 'Element'
else
nt := 'Root element';
end
else
if N is TElXMLDOMText then
nt := 'Text'
else
if N is TElXMLDOMComment then
nt := 'Comment'
else
if N is TElXMLDOMCharacterData then
nt := 'CDATA'
else
if N is TElXMLDOMDocument then
nt := 'Document'
else
if N is TElXMLDOMEntityReference then
nt := 'Entity Reference'
else
nt := 'Unknown';
dlbNodeType.Caption := nt;
end
else
begin
mmXML.Clear;
dlbNodeType.Caption := 'None';
dlbNamespaceURI.Caption := '';
end;
end;
procedure TfrmMain.UpdateXML;
function AddNode(Sibling: TTreeNode; Node: TElXMLDOMNode): TTreeNode;
var
T: TElXMLDOMNode;
AttrNode: TTreeNode;
Attributes: TElXMLDOMNamedNodeMap;
i: Integer;
s: string;
begin
if Node is TElXMLDOMDocument then
s := TElXMLDOMDocument(Node).LocalName
else if Node is TElXMLDOMElement then
s := TElXMLDOMElement(Node).NodeName
else if Node is TElXMLDOMAttr then
s := TElXMLDOMAttr(Node).NodeName
else if Node is TElXMLDOMEntityReference then
s := '#entity reference'
else
s := Node.NodeName;
Result := tvXML.Items.AddChild(Sibling, s);
Result.Data := {$ifndef DELPHI_NET}Pointer{$endif}(Node);
if Node is TElXMLDOMElement then
begin
Attributes := TElXMLDOMElement(Node).Attributes;
if Assigned(Attributes) and (Attributes.Length > 0) then
begin
AttrNode := Result;
// AttrNode := tvXML.Items.AddChild(Result, 'Attributes');
for i := 0 to Attributes.Length - 1 do
AddNode(AttrNode, Attributes.Item[i]);
end;
end;
if Node is TElXMLDOMAttr then
Exit;
T := Node.FirstChild;
while Assigned(T) do
begin
if (T is TElXMLDOMElement) or
(T is TElXMLDOMAttr) or
(T is TElXMLDOMEntityReference) or
((T is TElXMLDOMCharacterData) and
((Trim(TElXMLDOMCharacterData(T).Data) <> '') or
(not T.Loaded and (T.RawDataLength > 0)) )) then
AddNode(Result, T);
T := T.NextSibling;
end;
end;
begin
mmXML.Clear;
tvXML.Items.BeginUpdate;
try
tvXML.Items.Clear;
AddNode(nil, FXMLDocument);
tvXML.Items.GetFirstNode.Expand(False);
finally
tvXML.Items.EndUpdate;
end;
end;
initialization
{$ifndef DELPHI_NET}
SetLicenseKey('ADDCD14AD06709806817E0B3D7BFD0A2222D536FE156466C5D5FE65DB5DEAE76' +
'FFDEBC07E915A5751C12C01C783958872A38E4A5EDA140E7247E0F2E56442A3C' +
'F3E9347AD8FDE52083A0DFC86BC00ECB0FD0CF1B51159A2BCB84F6EA6349EF47' +
'5C15A59AFCC55F7C3AAD26C279628B5D91B1DC94BD2385354A70CCA3B76101D9' +
'F41C84A639FC3CCE4BA8F0CC4A66DCD150114A3F58C1AD46B7B94643741BC20A' +
'8DCA83AB921480951B423CAA19EF1863A47CA2C3422E7E5634BED98939A5AE43' +
'DE1E4BAD79E66D8A5C973B3455656C8C9B6FF024FADD6CDA02D0F506D98493C8' +
'BD1ED7B237DB75FA31F2C82654490CDDDEE24E19939137B9E1DB05508733B22F');
{$else}
SBUtils.SetLicenseKey(
'0566A0892842A9A7E2957B21B3D81A1C6EDB361CFEC2CCCA088A386D658ED927' +
'B01744E8DFBC590717631F42F840ED63C0322DDA17AA712010211551FFCD6042' +
'AB769E2D8FE083C15338C99902232783FAB30AA65EEBEE98338B8FCBC1AFE342' +
'DF79686C1E1587E6E3EACCBF9DA720F12BA80C66CE2191BDE832BB59AB459236' +
'B12FC1EFFC0FDDB198869B2E95FC5C5593FE6D69FEA95AC03E97D4F78C948C85' +
'AD18E5589A7E827E7D09AB04FEB7C69C0AA7ED2530F8AEE623BCE705D4F39E16' +
'44CF22872C3425C2A260234AE3410F32642FE0683781FC6833F5A5BA7306488B' +
'CE4F7D13D91E892DAE4C908D92415BF05F61A380CB8CB796F047334B58FE79FA'
);
{$endif}
end.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?