⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processorform.pas

📁 著名的SecureBlackBox控件完整源码
💻 PAS
字号:
unit ProcessorForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  {$ifdef DELPHI_NET}
  System.IO,
  System.ComponentModel,
  {$endif}
  StdCtrls, SBUtils, SBX509, SBCustomCertStorage, SBMessages,
  SBConstants;

type
  TfrmProcessor = class(TForm)
    edtInput: TEdit;
    Label1: TLabel;
    btnSelectInput: TButton;
    Label2: TLabel;
    edtOutput: TEdit;
    btnSelectOutput: TButton;
    btnSign: TButton;
    btnVerify: TButton;
    btnEncrypt: TButton;
    btnDecrypt: TButton;
    dlgOpen: TOpenDialog;
    dlgSave: TSaveDialog;
    btnClose: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure btnSelectInputClick(Sender: TObject);
    procedure btnSelectOutputClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnSignClick(Sender: TObject);
    procedure btnVerifyClick(Sender: TObject);
    procedure btnEncryptClick(Sender: TObject);
    procedure btnDecryptClick(Sender: TObject);
  private
  public
    Decryptor: TElMessageDecryptor;
    Encryptor: TElMessageEncryptor;
    Signer: TElMessageSigner;
    Verifier: TElMessageVerifier;
    CertStorage: TElMemoryCertStorage;

    constructor Create(AOwner: TComponent); override;
  end;

var
  frmProcessor: TfrmProcessor;

implementation

{$R *.DFM}

procedure TfrmProcessor.btnSelectInputClick(Sender: TObject);
begin
  if dlgOpen.Execute then
    edtInput.Text := dlgOpen.FileName;
end;

procedure TfrmProcessor.btnSelectOutputClick(Sender: TObject);
begin
  if dlgSave.Execute then
    edtOutput.Text := dlgSave.FileName;
end;

procedure TfrmProcessor.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caHide;
end;

procedure TfrmProcessor.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Decryptor);
  FreeAndNil(Encryptor);
  FreeAndNil(Signer);
  FreeAndNil(Verifier);
  FreeAndNil(CertStorage);
end;

procedure TfrmProcessor.btnSignClick(Sender: TObject);
var InBuffer,
    OutBuffer : ByteArray;
    Stream    : {$ifndef DELPHI_NET}TFileStream{$else}FileStream{$endif};
    InSize,
    OutSize   : integer;
    res       : integer;
begin
  {$ifndef DELPHI_NET}
  Stream := TFileStream.Create(edtInput.Text, fmOpenRead or fmShareDenyWrite);
  {$else}
  Stream := FileStream.Create(edtInput.Text, FileMode.Open, FileAccess.Read);
  {$endif}
  try
    InSize := Stream.{$ifndef DELPHI_NET}Size{$else}Length{$endif};
    SetLength(InBuffer, InSize);
    {$ifndef DELPHI_NET}
    Stream.ReadBuffer(InBuffer[0], InSize);
    {$else}
    Stream.Read(InBuffer, 0, InSize);
    {$endif}
  finally
    Stream.Free;
  end;
  OutSize := InSize + 16384;
  SetLength(OutBuffer, OutSize);
  res := Signer.Sign(InBuffer, {$ifndef DELPHI_NET}InSize,{$endif} OutBuffer, OutSize);
  if res <> 0 then
  begin
    MessageDlg(Format('Error %d when signing the file', [res]), mtError, [mbOk], 0);
    exit;
  end;
  {$ifndef DELPHI_NET}
  Stream := TFileStream.Create(edtOutput.Text, fmCreate or fmShareDenyWrite);
  {$else}
  Stream := FileStream.Create(edtOutput.Text, FileMode.Create, FileAccess.ReadWrite);
  {$endif}
  try
    {$ifndef DELPHI_NET}
    Stream.WriteBuffer(OutBuffer[0], OutSize);
    {$else}
    Stream.Write(OutBuffer, 0, OutSize);
    {$endif}
  finally
    Stream.Free;
  end;
  MessageDlg('The file has been succesfully signed', mtInformation, [mbOk], 0);
end;

procedure TfrmProcessor.btnVerifyClick(Sender: TObject);
var InBuffer,
    OutBuffer : ByteArray;
    Stream    : {$ifndef DELPHI_NET}TFileStream{$else}FileStream{$endif};
    InSize,
    OutSize   : integer;
    res       : integer;
begin
  {$ifndef DELPHI_NET}
  Stream := TFileStream.Create(edtInput.Text, fmOpenRead or fmShareDenyWrite);
  {$else}
  Stream := FileStream.Create(edtInput.Text, FileMode.Open, FileAccess.Read);
  {$endif}
  try
    InSize := Stream.{$ifndef DELPHI_NET}Size{$else}Length{$endif};
    SetLength(InBuffer, InSize);
    {$ifndef DELPHI_NET}
    Stream.ReadBuffer(InBuffer[0], InSize);
    {$else}
    Stream.Read(InBuffer, 0, InSize);
    {$endif}
  finally
    Stream.Free;
  end;
  OutSize := InSize + 16384;
  SetLength(OutBuffer, OutSize);
  res := Verifier.Verify(InBuffer, {$ifndef DELPHI_NET}InSize,{$endif} OutBuffer, OutSize);
  if res = SB_MESSAGE_ERROR_NO_CERTIFICATE then
  begin
    MessageDlg('The file was signed using certificate, different from the one you specified', mtError, [mbOk], 0);
    exit;
  end
  else
  if (res = SB_MESSAGE_ERROR_INVALID_SIGNATURE) or (res = SB_MESSAGE_ERROR_INVALID_DIGEST) then
  begin
    MessageDlg('The signature of the file is invalid. This means that the file has been modified or is corrupt.', mtError, [mbOk], 0);
    exit;
  end
  else
  if res <> 0 then
  begin
    MessageDlg(Format('Error %d when verifying the file', [res]), mtError, [mbOk], 0);
    exit;
  end;
  {$ifndef DELPHI_NET}
  Stream := TFileStream.Create(edtOutput.Text, fmCreate or fmShareDenyWrite);
  {$else}
  Stream := FileStream.Create(edtOutput.Text, FileMode.Create, FileAccess.ReadWrite);
  {$endif}
  try
    {$ifndef DELPHI_NET}
    Stream.WriteBuffer(OutBuffer[0], OutSize);
    {$else}
    Stream.Write(OutBuffer, 0, OutSize);
    {$endif}
  finally
    Stream.Free;
  end;
  if Verifier.Certificates.{$ifndef DELPHI_NET}FindByHash{$else}FindByHashSHA1{$endif}(CertStorage.Certificates[0].GetHashSHA1) <> -1 then
    MessageDlg('The file has been succesfully verified and saved to output file.', mtInformation, [mbOk], 0)
  else
    MessageDlg('The file has been verified using internally contained certificate (i.e. not the one you selected).', mtInformation, [mbOk], 0);
end;

procedure TfrmProcessor.btnEncryptClick(Sender: TObject);
var InBuffer,
    OutBuffer : ByteArray;
    Stream    : {$ifndef DELPHI_NET}TFileStream{$else}FileStream{$endif};
    InSize,
    OutSize   : integer;
    res       : integer;
begin
  {$ifndef DELPHI_NET}
  Stream := TFileStream.Create(edtInput.Text, fmOpenRead or fmShareDenyWrite);
  {$else}
  Stream := FileStream.Create(edtInput.Text, FileMode.Open, FileAccess.Read);
  {$endif}
  try
    InSize := Stream.{$ifndef DELPHI_NET}Size{$else}Length{$endif};
    SetLength(InBuffer, InSize);
    {$ifndef DELPHI_NET}
    Stream.ReadBuffer(InBuffer[0], InSize);
    {$else}
    Stream.Read(InBuffer, 0, InSize);
    {$endif}
  finally
    Stream.Free;
  end;
  OutSize := InSize + 16384;
  SetLength(OutBuffer, OutSize);
  res := Encryptor.Encrypt(InBuffer, {$ifndef DELPHI_NET}InSize,{$endif} OutBuffer, OutSize);
  if res <> 0 then
  begin
    MessageDlg(Format('Error %d when encrypting the file', [res]), mtError, [mbOk], 0);
    exit;
  end;
  {$ifndef DELPHI_NET}
  Stream := TFileStream.Create(edtOutput.Text, fmCreate or fmShareDenyWrite);
  {$else}
  Stream := FileStream.Create(edtOutput.Text, FileMode.Create, FileAccess.ReadWrite);
  {$endif}
  try
    {$ifndef DELPHI_NET}
    Stream.WriteBuffer(OutBuffer[0], OutSize);
    {$else}
    Stream.Write(OutBuffer, 0, OutSize);
    {$endif}
  finally
    Stream.Free;
  end;
  MessageDlg('The file has been succesfully encrypted', mtInformation, [mbOk], 0);
end;

procedure TfrmProcessor.btnDecryptClick(Sender: TObject);
var InBuffer,
    OutBuffer : ByteArray;
    Stream    : {$ifndef DELPHI_NET}TFileStream{$else}FileStream{$endif};
    InSize,
    OutSize   : integer;
    res       : integer;
begin
  {$ifndef DELPHI_NET}
  Stream := TFileStream.Create(edtInput.Text, fmOpenRead or fmShareDenyWrite);
  {$else}
  Stream := FileStream.Create(edtInput.Text, FileMode.Open, FileAccess.Read);
  {$endif}
  try
    InSize := Stream.{$ifndef DELPHI_NET}Size{$else}Length{$endif};
    SetLength(InBuffer, InSize);
    {$ifndef DELPHI_NET}
    Stream.ReadBuffer(InBuffer[0], InSize);
    {$else}
    Stream.Read(InBuffer, 0, InSize);
    {$endif}
  finally
    Stream.Free;
  end;
  OutSize := InSize + 16384;
  SetLength(OutBuffer, OutSize);
  res := Decryptor.Decrypt(InBuffer, {$ifndef DELPHI_NET}InSize,{$endif} OutBuffer, OutSize);
  if res = SB_MESSAGE_ERROR_NO_CERTIFICATE then
  begin
    MessageDlg('Certificate, necessary to decrypt the file, was not found.', mtError, [mbOk], 0);
    exit;
  end
  else
  if (res = SB_MESSAGE_ERROR_KEY_DECRYPTION_FAILED) or (res = SB_MESSAGE_ERROR_CONTENT_DECRYPTION_FAILED) then
  begin
    MessageDlg('The file could not be decrypted. Most likely the file was changed or is corrupt.', mtError, [mbOk], 0);
    exit;
  end
  else
  if res <> 0 then
  begin
    MessageDlg(Format('Error %d when derifying the file', [res]), mtError, [mbOk], 0);
    exit;
  end;
  {$ifndef DELPHI_NET}
  Stream := TFileStream.Create(edtOutput.Text, fmCreate or fmShareDenyWrite);
  {$else}
  Stream := FileStream.Create(edtOutput.Text, FileMode.Create, FileAccess.ReadWrite);
  {$endif}
  try
    {$ifndef DELPHI_NET}
    Stream.WriteBuffer(OutBuffer[0], OutSize);
    {$else}
    Stream.Write(OutBuffer, 0, OutSize);
    {$endif}
  finally
    Stream.Free;
  end;
  MessageDlg('The file has been succesfully decrypted and saved to output file.', mtInformation, [mbOk], 0);
end;

constructor TfrmProcessor.Create(AOwner: TComponent);
begin
  inherited;

  CertStorage := TElMemoryCertStorage.Create(nil);
  Decryptor := TElMessageDecryptor.Create(nil);
  Encryptor := TElMessageEncryptor.Create(nil);
  Signer := TElMessageSigner.Create(nil);
  Verifier := TElMessageVerifier.Create(nil);

  Decryptor.CertStorage := CertStorage;
  Encryptor.CertStorage := CertStorage;
  Encryptor.UseOAEP := False;
  Encryptor.BitsInKey := 128;
  Encryptor.Algorithm := SB_ALGORITHM_CNT_AES128;
  Signer.CertStorage := CertStorage;
  Signer.UsePSS := False;
  Verifier.CertStorage := CertStorage;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -