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

📄 mainform.pas

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

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, SBPDF, SBPDFSecurity, SBConstants, SBX509, SBUtils;

type
  TfrmMain = class(TForm)
    lSourceFile: TLabel;
    editSource: TEdit;
    lDestFile: TLabel;
    editDest: TEdit;
    btnBrowseSource: TButton;
    btnBrowseDest: TButton;
    gbEncryptionProps: TGroupBox;
    rbPasswordEncryption: TRadioButton;
    rbPublicKeyEncryption: TRadioButton;
    editPassword: TEdit;
    lCertificate: TLabel;
    editCert: TEdit;
    btnBrowseCert: TButton;
    lCertPassword: TLabel;
    editCertPassword: TEdit;
    cbAlgorithm: TComboBox;
    lEncryptionAlgorithm: TLabel;
    cbEncryptMetadata: TCheckBox;
    btnEncrypt: TButton;
    btnCancel: TButton;
    OpenDialogPDF: TOpenDialog;
    SaveDialogPDF: TSaveDialog;
    OpenDialogCert: TOpenDialog;
    Document: TElPDFDocument;
    PasswordHandler: TElPDFPasswordSecurityHandler;
    PublicKeyHandler: TElPDFPublicKeySecurityHandler;
    Cert: TElX509Certificate;
    procedure FormCreate(Sender: TObject);
    procedure btnBrowseSourceClick(Sender: TObject);
    procedure btnBrowseDestClick(Sender: TObject);
    procedure btnBrowseCertClick(Sender: TObject);
    procedure rbPasswordEncryptionClick(Sender: TObject);
    procedure rbPublicKeyEncryptionClick(Sender: TObject);
    procedure btnEncryptClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
  private
    function GenerateTempFilename : string;
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.DFM}

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  cbAlgorithm.ItemIndex := 0;
end;

procedure TfrmMain.btnBrowseSourceClick(Sender: TObject);
begin
  if OpenDialogPDF.Execute then
    editSource.Text := OpenDialogPDF.Filename;
end;

procedure TfrmMain.btnBrowseDestClick(Sender: TObject);
begin
  if SaveDialogPDF.Execute then
    editDest.Text := SaveDialogPDF.FileName;
end;

procedure TfrmMain.btnBrowseCertClick(Sender: TObject);
begin
  if OpenDialogCert.Execute then
    editCert.Text := OpenDialogCert.Filename;
end;

procedure TfrmMain.rbPasswordEncryptionClick(Sender: TObject);
begin
  editPassword.Enabled := true;
  editCert.Enabled := false;
  editCertPassword.Enabled := false;
  lCertificate.Enabled := false;
  lCertPassword.Enabled := false;
  btnBrowseCert.Enabled := false;
end;

procedure TfrmMain.rbPublicKeyEncryptionClick(Sender: TObject);
begin
  editPassword.Enabled := false;
  editCert.Enabled := true;
  editCertPassword.Enabled := true;
  lCertificate.Enabled := true;
  lCertPassword.Enabled := true;
  btnBrowseCert.Enabled := true;
end;

function TfrmMain.GenerateTempFilename : string;
var
  Needed : DWORD;
  Path : string;
  Index : integer;
const
  SBB : string = 'sbb'#0;
begin
  Needed := GetTempPathA(0, nil);
  SetLength(Path, Needed);
  Needed := GetTempPathA(Needed, @Path[1]);
  if Needed = 0 then
    raise Exception.Create('Failed to get temporary path');
  SetLength(Result, MAX_PATH);
  if GetTempFileNameA(@Path[1], @SBB[1], 0, @Result[1]) = 0 then
    raise Exception.Create('Failed to generate a temporary file name');
  Index := Pos(#0, Result);
  Result := Copy(Result, 1, Index - 1);
end;

procedure TfrmMain.btnEncryptClick(Sender: TObject);
var
  F, CertF : TFileStream;
  CurrHandler : TElPDFSecurityHandler;
  Alg : integer;
  KeySize : integer;
  GroupIndex : integer;
  CertFormat : TSBCertFileFormat;
  Success : boolean;
  TempPath : string;
begin
  // creating a temporary file copy
  TempPath := GenerateTempFilename;
  if not CopyFile(PChar(editSource.Text), PChar(TempPath), false) then
  begin
    MessageDlg('Failed to create a temporary file', mtError, [mbOk], 0);
    Exit;
  end;
  // opening the temporary file
  Success := false;
  F := TFileStream.Create(TempPath, fmOpenReadWrite or fmShareDenyWrite);
  try
    try
      // opening the document
      Document.Open(F);
      try
        // checking if the document is already encrypted
        if Document.Encrypted then
        begin
          MessageDlg('The document is already encrypted', mtError, [mbOk], 0);
          Exit;
        end;
        // setting up property values
        if rbPasswordEncryption.Checked then
          CurrHandler := PasswordHandler
        else
          CurrHandler := PublicKeyHandler;
        // the following encryption handler assignment must be executed before
        // other handler properties are assigned, since encryption handler
        // has to access document properties during its work.
        Document.EncryptionHandler := CurrHandler;
        CurrHandler.EncryptMetadata := cbEncryptMetadata.Checked;
        case cbAlgorithm.ItemIndex of
          0 :
          begin
            Alg := SB_ALGORITHM_CNT_RC4;
            KeySize := 40;
          end;
          1 :
          begin
            Alg := SB_ALGORITHM_CNT_RC4;
            KeySize := 128;
          end;
          2 :
          begin
            Alg := SB_ALGORITHM_CNT_AES128;
            // the key size for this cipher is always 128 bits, so we may
            // omit the assignment in this point. However, just to calm the
            // compiler we assing the 0 value to KeySize variable.
            KeySize := 0;
          end;
          else
          begin
            // normally, we should not reach this point, so just making the
            // compiler silent.
            MessageDlg('Unsupported algorithm', mtError, [mbOk], 0);
            Exit;
          end;
        end;
        // PDF specification allows to use different ciphers for streams and
        // strings contained in a document. However, using the same value for
        // both string and stream encryption allows to achieve greater compatibility
        // with other implementations
        CurrHandler.StreamEncryptionAlgorithm := Alg;
        CurrHandler.StringEncryptionAlgorithm := Alg;
        CurrHandler.StreamEncryptionKeyBits := KeySize;
        CurrHandler.StringEncryptionKeyBits := KeySize;
        if CurrHandler is TElPDFPasswordSecurityHandler then
        begin
          PasswordHandler.UserPassword := editPassword.Text;
          PasswordHandler.OwnerPassword := editPassword.Text;
          PasswordHandler.Permissions.EnableAll;
        end
        else
        begin
          // loading certificate
          CertF := TFileStream.Create(editCert.Text, fmOpenRead or fmShareDenyWrite);
          try
            CertFormat := Cert.DetectCertFileFormat(CertF);
            CertF.Position := 0;
            case CertFormat of
              cfDER : Cert.LoadFromStream(CertF);
              cfPEM : Cert.LoadFromStreamPEM(CertF, editCertPassword.Text);
              cfPFX : Cert.LoadFromStreamPFX(CertF, editCertPassword.Text);
              else
              begin
                MessageDlg('Failed to load certificate', mtError, [mbOk], 0);
                Exit;
              end;
            end;
          finally
            FreeAndNil(CertF);
          end;
          // creating recipient group
          GroupIndex := PublicKeyHandler.AddRecipientGroup();
          // adding recipient certificate to group
          PublicKeyHandler.RecipientGroups[GroupIndex].AddRecipient(Cert);
          PublicKeyHandler.RecipientGroups[GroupIndex].Permissions.EnableAll;
        end;
        // encrypting the document
        Document.Encrypt;
        // allowing to save the document
        Success := true;
      finally
        // closing the document
        Document.Close(Success);
      end;
    finally
      FreeAndNil(F);
    end;
  except
    on E : Exception do
    begin
      MessageDlg('Error: ' + E.Message, mtError, [mbOk], 0);
      Success := false;
    end;
  end;
  // if encryption process succeeded, moving the temporary file to the place
  // of destination file
  if Success then
  begin
    if not CopyFile(PChar(TempPath), PChar(editDest.Text), false) then
      MessageDlg('Failed to save temporary file', mtError, [mbOk], 0)
    else
      MessageDlg('Encryption process successfully finished', mtInformation, [mbOk], 0);
  end
  else
    MessageDlg('Encryption failed', mtError, [mbOk], 0);
  // deleting temporary file
  DeleteFile(TempPath);
  Close();
end;

procedure TfrmMain.btnCancelClick(Sender: TObject);
begin
  Close();
end;

initialization
SetLicenseKey('ADDCD14AD06709806817E0B3D7BFD0A2222D536FE156466C5D5FE65DB5DEAE76' + 
  'FFDEBC07E915A5751C12C01C783958872A38E4A5EDA140E7247E0F2E56442A3C' + 
  'F3E9347AD8FDE52083A0DFC86BC00ECB0FD0CF1B51159A2BCB84F6EA6349EF47' + 
  '5C15A59AFCC55F7C3AAD26C279628B5D91B1DC94BD2385354A70CCA3B76101D9' + 
  'F41C84A639FC3CCE4BA8F0CC4A66DCD150114A3F58C1AD46B7B94643741BC20A' + 
  '8DCA83AB921480951B423CAA19EF1863A47CA2C3422E7E5634BED98939A5AE43' + 
  'DE1E4BAD79E66D8A5C973B3455656C8C9B6FF024FADD6CDA02D0F506D98493C8' + 
  'BD1ED7B237DB75FA31F2C82654490CDDDEE24E19939137B9E1DB05508733B22F');

end.

⌨️ 快捷键说明

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