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

📄 mainform.cpp

📁 著名的SecureBlackBox控件完整源码
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SBCustomCertStorage"
#pragma link "SBPDF"
#pragma link "SBPDFSecurity"
#pragma link "SBX509"
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
    SetLicenseKey((AnsiString)"ADDCD14AD06709806817E0B3D7BFD0A2222D536FE156466C5D5FE65DB5DEAE76" +
      "FFDEBC07E915A5751C12C01C783958872A38E4A5EDA140E7247E0F2E56442A3C" +
      "F3E9347AD8FDE52083A0DFC86BC00ECB0FD0CF1B51159A2BCB84F6EA6349EF47" +
      "5C15A59AFCC55F7C3AAD26C279628B5D91B1DC94BD2385354A70CCA3B76101D9" +
      "F41C84A639FC3CCE4BA8F0CC4A66DCD150114A3F58C1AD46B7B94643741BC20A" +
      "8DCA83AB921480951B423CAA19EF1863A47CA2C3422E7E5634BED98939A5AE43" +
      "DE1E4BAD79E66D8A5C973B3455656C8C9B6FF024FADD6CDA02D0F506D98493C8" +
      "BD1ED7B237DB75FA31F2C82654490CDDDEE24E19939137B9E1DB05508733B22F");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnBrowseSourceClick(TObject *Sender)
{
    if (OpenDialogPDF->Execute())
        editSource->Text = OpenDialogPDF->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnBrowseDestClick(TObject *Sender)
{
    if (SaveDialogPDF->Execute())
        editDest->Text = SaveDialogPDF->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnBrowseCertClick(TObject *Sender)
{
    if (OpenDialogCert->Execute())
        editCert->Text = OpenDialogCert->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
  cbSignatureType->ItemIndex = 0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnCancelClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnOKClick(TObject *Sender)
{
    // creating a temporary file copy
    AnsiString TempPath = GenerateTempFilename();
    if (!CopyFile(editSource->Text.c_str(), TempPath.c_str(), false))
    {
      MessageDlg("Failed to create a temporary file", mtError, TMsgDlgButtons()<<mbOK, 0);
      return;
    }
    // opening the temporary file
    bool Success = false;
    TFileStream* F = new TFileStream(TempPath, fmOpenReadWrite | fmShareDenyWrite);
    try
    {
        try
        {
            // opening the document
            Document->Open(F);
            try
            {
                // checking if the document is already encrypted
                if (Document->Encrypted)
                {
                    MessageDlg("Cannot sign the encrypted document", mtError, TMsgDlgButtons()<<mbOK, 0);
                    return;
                }
                // adding the signature and setting up property values
                int Index = Document->AddSignature();
                TElPDFSignature* Sig = Document->Signatures[Index];
                Sig->Handler = PublicKeyHandler;
                Sig->AuthorName = editAuthorName->Text;
                Sig->SigningTime = Now();
                if (cbReason->Text != "<none>")
                    Sig->Reason = cbReason->Text;
                else
                    Sig->Reason = "";
                // configuring signature type
                switch (cbSignatureType->ItemIndex)
                {
                    case 0: // invisible document signature
                        Sig->Invisible = true;
                        break;
                    case 1: // visible document signature
                        Sig->Invisible = false;
                        break;
                    case 2:
                        Sig->SignatureType = stMDP;
                        Sig->Invisible = false;
                        break;
                }

                // loading certificate
                TFileStream* CertF = new TFileStream(editCert->Text, fmOpenRead | fmShareDenyWrite);
                try
                {
                  TSBCertFileFormat CertFormat = Cert->DetectCertFileFormat(NULL, CertF);
                  CertF->Position = 0;
                  switch (CertFormat)
                  {
                      case cfDER:
                          Cert->LoadFromStream(CertF, CertF->Size);
                          break;
                      case cfPEM:
                          Cert->LoadFromStreamPEM(CertF, editCertPassword->Text, CertF->Size);
                          break;
                      case cfPFX:
                          Cert->LoadFromStreamPFX(CertF, editCertPassword->Text, CertF->Size);
                          break;
                      default:
                          MessageDlg("Failed to load certificate", mtError, TMsgDlgButtons()<<mbOK, 0);
                          return;
                  }
                }
                __finally
                {
                    delete CertF;
                }
                CertStorage->Clear();
                CertStorage->Add(Cert, true);
                PublicKeyHandler->CertStorage = CertStorage;
                // allowing to save the document
                Success = true;
            }
            __finally
            {
                // closing the document
                Document->Close(Success);
            }
        }
        __finally
        {
              delete F;
        }
    }
    catch(Exception &e)
    {
        MessageDlg((AnsiString)"Error: " + e.Message, mtError, TMsgDlgButtons()<<mbOK, 0);
        Success = false;
    }
    // if encryption process succeeded, moving the temporary file to the place
    // of destination file
    if (Success)
    {
        if(!CopyFile(TempPath.c_str(), editDest->Text.c_str(), false))
            MessageDlg("Failed to save temporary file", mtError, TMsgDlgButtons()<<mbOK, 0);
        else
            MessageDlg("Signing process successfully finished", mtInformation, TMsgDlgButtons()<<mbOK, 0);
    }
    else
        MessageDlg("Signing failed", mtError, TMsgDlgButtons()<<mbOK, 0);
    // deleting temporary file
    DeleteFile(TempPath);
    Close();
}
//---------------------------------------------------------------------------
AnsiString TfrmMain::GenerateTempFilename(void)
{
    const char* szSBB = "sbb";
    DWORD dwNeeded = GetTempPathA(0, NULL);
    char* szPath = new char[dwNeeded+1];
    dwNeeded = GetTempPathA(dwNeeded, szPath);
    if (dwNeeded == 0)
    {
        delete[] szPath;
        throw(Exception("Failed to get temporary path"));
    }
    char* szResult = new char[MAX_PATH];
    if (GetTempFileNameA(szPath, szSBB, 0, szResult) == 0)
    {
        delete[] szPath;
        delete[] szResult;
        throw(Exception("Failed to generate a temporary file name"));
    }

    AnsiString sResult = AnsiString(szResult);
    delete[] szPath;
    delete[] szResult;
    return sResult;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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