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

📄 mainform.cpp

📁 著名的SecureBlackBox控件完整源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

         case STATE_PROTECT_FINISH:
             {
                 SetCaption("End of Work", "Protection finished");
                 EnableButtons(false, false);
                 PageControl->ActivePage = tsFinish;
                 btnSignatures->Visible = false;
                 btnCancel->Caption = "Finish";
             }
             break;

         case STATE_DECRYPT_SELECT_KEYRING:
             {
                 SetCaption("Step 1 of 3", "Select keyring files");
                 EnableButtons(true, true);
                 PageControl->ActivePage = tsKeyringSelect;
             }
             break;

         case STATE_DECRYPT_SELECT_SOURCE:
             {
                 SetCaption("Step 2 of 3", "Select PGP-protected file");
                 if (pgpKeyring->SecretCount == 0)
                   lbFileSelectComment->Caption = "Your keyring does not contain private keys. You will not be able to decrypt encrypted files.";
                 else
                   lbFileSelectComment->Caption = "";

                 EnableButtons(true, true);
                 PageControl->ActivePage = tsFileSelect;
             }
             break;

         case STATE_DECRYPT_PROGRESS:
             {
                 SetCaption("Step 3 of 3", "Extracting protected data");
                 EnableButtons(false, false);
                 pbProgress->Position = 0;
                 lbProcessingFile->Visible = true;
                 pbProgress->Visible = true;
                 lbErrorComment->Caption = "";
                 lbFinished->Caption = "Operation successfully finished";
                 PageControl->ActivePage = tsProgress;
                 try
                 {
                     DecryptFile(Source);
                 }
                 catch(Exception &e)
                 {
                     lbErrorComment->Caption = e.Message;
                     lbFinished->Caption = "ERROR";
                 }

                 pbProgress->Visible = false;
                 lbProcessingFile->Visible = false;
                 ChangeState(STATE_DECRYPT_FINISH);
             }
             break;

         case STATE_DECRYPT_FINISH:
             {
                 SetCaption("End of Work", "Decryption finished");
                 EnableButtons(false, false);
                 PageControl->ActivePage = tsFinish;
                 btnSignatures->Visible = true;
                 btnSignatures->Enabled = ((sigs != NULL) && (sizeof(*sigs)/sizeof(TElPGPSignature*)>0));
                 btnCancel->Caption = "Finish";
             }
             break;
    }

    State = nextState;
}
//---------------------------------------------------------------------------
void TfrmForm::KeysToList(bool PublicKeys)
{
    lvKeys->Items->Clear();
    if (PublicKeys)
    {
        for (int i = 0; i< pgpKeyring->PublicCount; i++)
        {
            AnsiString Name;
            if (pgpKeyring->PublicKeys[i]->UserIDCount > 0)
                Name = pgpKeyring->PublicKeys[i]->UserIDs[0]->Name;
            else
                Name = "<no name>";

            TListItem* Item = lvKeys->Items->Add();
            Item->Caption = Name;
            AnsiString alg = PKAlg2Str(pgpKeyring->PublicKeys[i]->PublicKeyAlgorithm);
            Item->SubItems->Add(alg);
            Item->SubItems->Add(IntToStr(pgpKeyring->PublicKeys[i]->BitsInKey));

            if ((pgpKeyring->PublicKeys[i]->PublicKeyAlgorithm == SB_PGP_ALGORITHM_PK_RSA) ||
                (pgpKeyring->PublicKeys[i]->PublicKeyAlgorithm == SB_PGP_ALGORITHM_PK_RSA_ENCRYPT) ||
                (pgpKeyring->PublicKeys[i]->PublicKeyAlgorithm == SB_PGP_ALGORITHM_PK_RSA_SIGN))
                Item->ImageIndex = 1;
            else
                Item->ImageIndex = 0;

            Item->Data = pgpKeyring->PublicKeys[i];
        }
    }
    else
    {
        for (int i = 0; i<pgpKeyring->SecretCount; i++)
        {
            AnsiString Name;
            if (pgpKeyring->SecretKeys[i]->PublicKey->UserIDCount > 0)
                Name = pgpKeyring->SecretKeys[i]->PublicKey->UserIDs[0]->Name;
            else
                Name = "<no name>";

            TListItem* Item = lvKeys->Items->Add();
            Item->Caption = Name;
            AnsiString alg = PKAlg2Str(pgpKeyring->SecretKeys[i]->PublicKeyAlgorithm);
            Item->SubItems->Add(alg);
            Item->SubItems->Add(IntToStr(pgpKeyring->SecretKeys[i]->BitsInKey));

            if ((pgpKeyring->SecretKeys[i]->PublicKeyAlgorithm == SB_PGP_ALGORITHM_PK_RSA) ||
                (pgpKeyring->SecretKeys[i]->PublicKeyAlgorithm == SB_PGP_ALGORITHM_PK_RSA_ENCRYPT) ||
                (pgpKeyring->SecretKeys[i]->PublicKeyAlgorithm == SB_PGP_ALGORITHM_PK_RSA_SIGN))
                Item->ImageIndex = 1;
            else
                Item->ImageIndex = 0;

            Item->Data = pgpKeyring->SecretKeys[i];
        }
    }
}
//---------------------------------------------------------------------------
void TfrmForm::ProtectFile(const AnsiString SourceFile, AnsiString DestFile)
{
    pgpWriter->Armor = true;
    pgpWriter->ArmorHeaders->Clear();
    pgpWriter->ArmorHeaders->Add("Version: EldoS PGPBlackbox (.NET edition)");
    pgpWriter->ArmorBoundary = "PGP MESSAGE";
    pgpWriter->Compress = cbCompress->Checked;
    pgpWriter->EncryptingKeys = pgpPubKeyring;
    pgpWriter->SigningKeys = pgpSecKeyring;

    if ((cbUseConvEnc->Checked) && (pgpPubKeyring->PublicCount > 0))
        pgpWriter->EncryptionType = etBoth;
    else if ((cbUseConvEnc->Checked) && (pgpPubKeyring->PublicCount == 0))
        pgpWriter->EncryptionType = etPassphrase;
    else
        pgpWriter->EncryptionType = etPublicKey;

	pgpWriter->Filename = Sbutils::ExtractFileName(SourceFile);
    pgpWriter->InputIsText = cbTextInput->Checked;
    pgpWriter->Passphrases->Clear();
    pgpWriter->Passphrases->Add(edPassphrase->Text);
    if (cbProtLevel->Text == "Low")
        pgpWriter->Protection = ptLow;
    else if (cbProtLevel->Text == "Normal")
        pgpWriter->Protection = ptNormal;
    else
        pgpWriter->Protection =ptHigh;

    pgpWriter->SignBufferingMethod = sbmRewind;

    if (cbEncryptionAlg->Text == "CAST5")
        pgpWriter->SymmetricKeyAlgorithm = SB_PGP_ALGORITHM_SK_CAST5;
    else if (cbEncryptionAlg->Text == "3DES")
        pgpWriter->SymmetricKeyAlgorithm = SB_PGP_ALGORITHM_SK_3DES;
    else if (cbEncryptionAlg->Text == "AES128")
        pgpWriter->SymmetricKeyAlgorithm = SB_PGP_ALGORITHM_SK_AES128;
    else
        pgpWriter->SymmetricKeyAlgorithm = SB_PGP_ALGORITHM_SK_AES256;

    pgpWriter->Timestamp = Now();
    pgpWriter->UseNewFeatures = cbUseNewFeatures->Checked;
    pgpWriter->UseOldPackets = false;

    TFileStream* inF = new TFileStream(Source, fmOpenRead);
    try
    {
        TFileStream* outF = new TFileStream(edFile->Text, fmCreate);
        try
        {
            if ((!cbEncrypt->Checked) && (cbSign->Checked) && (cbTextInput->Checked))
                pgpWriter->ClearTextSign(inF, outF, 0);
            else if ((cbEncrypt->Checked) && (cbSign->Checked))
                pgpWriter->EncryptAndSign(inF, outF, 0);
            else if ((cbEncrypt->Checked) && (!cbSign->Checked))
                pgpWriter->Encrypt(inF, outF, 0);
            else
                pgpWriter->Sign(inF, outF, false, 0);
        }
        __finally
        {
            delete outF;
        }
    }
    __finally
    {
        delete inF;
    }
}
//---------------------------------------------------------------------------
void TfrmForm::DecryptFile(const AnsiString SourceFile)
{
    pgpReader->DecryptingKeys = pgpKeyring;
    pgpReader->VerifyingKeys = pgpKeyring;

    TFileStream* inF = new TFileStream(SourceFile, fmOpenRead);
    try
    {
        pgpReader->DecryptAndVerify(inF, 0);
    }
    __finally
    {
        delete inF;
    }
}
//---------------------------------------------------------------------------
void __fastcall TfrmForm::cbEncryptClick(TObject *Sender)
{
    cbCompress->Enabled = cbEncrypt->Checked;
    cbEncryptionAlg->Enabled = cbEncrypt->Checked;
    cbProtLevel->Enabled = cbEncrypt->Checked;
    cbUseConvEnc->Enabled = cbEncrypt->Checked;
    lbProtLevel->Enabled = cbEncrypt->Checked;
    lbEncryptionAlg->Enabled = cbEncrypt->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TfrmForm::cbSignClick(TObject *Sender)
{
    cbTextInput->Enabled = cbSign->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TfrmForm::btnBrowseFileClick(TObject *Sender)
{
    if ((State == STATE_PROTECT_SELECT_SOURCE) ||
        (State == STATE_DECRYPT_SELECT_SOURCE))
    {
        DlgOpen->Filter = "";
        DlgOpen->Title = "Select file";
        if (DlgOpen->Execute())
            edFile->Text = DlgOpen->FileName;
    }
    else if (State == STATE_PROTECT_SELECT_DESTINATION)
    {
        if (DlgSave->Execute())
            edFile->Text = DlgSave->FileName;
    }
}
//---------------------------------------------------------------------------
void __fastcall TfrmForm::pgpWriterKeyPassphrase(TObject *Sender,
      TElPGPCustomSecretKey *Key, AnsiString &Passphrase, bool &Cancel)
{
    Passphrase = RequestKeyPassphrase(Key, Cancel);
}
//---------------------------------------------------------------------------
void __fastcall TfrmForm::pgpWriterProgress(TObject *Sender, int Processed,
      int Total, bool &Cancel)
void __fastcall TfrmForm::btnBrowsePubClick(TObject *Sender)
{
    DlgOpen->Filter = "PGP Keyring Files (*.pkr, *.skr, *.pgp, *.gpg, *.asc)|*.PKR;*.SKR;*.PGP;*.GPG;*.ASC";
    DlgOpen->FilterIndex = 1;
    DlgOpen->Title = "Select public keyring file";
    if (DlgOpen->Execute())
        edPubKeyring->Text = DlgOpen->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TfrmForm::btnBrowseSecClick(TObject *Sender)
{
    DlgOpen->Filter = "PGP Keyring Files (*.pkr, *.skr, *.pgp, *.gpg, *.asc)|*.PKR;*.SKR;*.PGP;*.GPG;*.ASC";
    DlgOpen->FilterIndex = 1;
    DlgOpen->Title = "Select secret keyring file";
    if (DlgOpen->Execute())
        edSecKeyring->Text = DlgOpen->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TfrmForm::pgpReaderKeyPassphrase(TObject *Sender,
      TElPGPCustomSecretKey *Key, AnsiString &Passphrase, bool &Cancel)
{
    Passphrase = RequestKeyPassphrase(Key, Cancel);
}
//---------------------------------------------------------------------------
void __fastcall TfrmForm::pgpReaderPassphrase(TObject *Sender,
      AnsiString &Passphrase, bool &Cancel)
{
    Passphrase = RequestKeyPassphrase(NULL, Cancel);
}
//---------------------------------------------------------------------------
void __fastcall TfrmForm::pgpReaderCreateOutputStream(TObject *Sender,
      const AnsiString Filename, TDateTime TimeStamp, TStream *&Stream,
      bool &FreeOnExit)
{
    DlgSave->FileName = Filename;
    if (DlgSave->Execute())
        Stream = new TFileStream(DlgSave->FileName, fmCreate);
    else
        Stream = new TMemoryStream;

    FreeOnExit = true;
}
//---------------------------------------------------------------------------
//void __fastcall TfrmForm::pgpReaderSignatures(TObject *Sender,
//      TList *Signatures, _ARRAYOF TSBPGPSignatureValidity* Validities,
//      const int Validities_Size, )

//void __fastcall TfrmForm::pgpReaderSignatures(TObject *Sender,
//	  TList *Signatures, const TSBPGPSignatureValidity* Validities,
//	  const int Validities_Size)
void __fastcall TfrmForm::pgpReaderSignatures(System::TObject* Sender,
  Classes::TList* Signatures, TSBPGPSignatureValidity * Validities,
  const int Validities_Size)

{
    TElPGPSignature* sig;

    sigs = (TElPGPSignature**) new (TElPGPSignature*)[Signatures->Count];
    vals = (TSBPGPSignatureValidity**) new (TSBPGPSignatureValidity*)[Signatures->Count];
//    SetLength(sigs, Length(Signatures));
//    SetLength(vals, Length(Signatures));
    for (int i = 0; i<Signatures->Count; i++)
    {
        sig = new TElPGPSignature;
        sig->Assign((TElPGPSignature*)(Signatures->Items[i]));
        sigs[i] = sig;
        vals[i] = (TSBPGPSignatureValidity*)(&(Validities[i]));
    }
}
//---------------------------------------------------------------------------
void __fastcall TfrmForm::btnSignaturesClick(TObject *Sender)
{
    TfrmSignatures* frmSignatures = new TfrmSignatures(this);
    try
    {
        frmSignatures->Init( (const TElPGPSignature**)sigs, (const TSBPGPSignatureValidity**)vals, pgpKeyring);
        frmSignatures->ShowModal();
    }
    __finally
    {
        delete frmSignatures;
    }
}
//---------------------------------------------------------------------------

void __fastcall TfrmForm::pgpReaderProgress(TObject *Sender, __int64 Processed,
      __int64 Total, bool &Cancel)
{
    pbProgress->Max = Total;
	pbProgress->Position = Processed;

	Application->ProcessMessages();
}
//---------------------------------------------------------------------------

void __fastcall TfrmForm::pgpWriterProgress(TObject *Sender, __int64 Processed,
      __int64 Total, bool &Cancel)
{
	pbProgress->Max = Total;
	pbProgress->Position = Processed;

	Application->ProcessMessages();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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