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

📄 mainform.cpp

📁 著名的SecureBlackBox控件完整源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    {
        // Exception. Possibly, passphrase is needed.
        signingKey->Passphrase = RequestPassphrase(signingKey->PublicKey);
        try
        {
            signingKey->Sign(((TElPGPPublicKey*)(userKey)), user, Sig, ctGeneric);
        }
        catch(Exception &e)
        {
            MessageDlg(e.Message, mtError, TMsgDlgButtons()<<mbOK, 0);
            delete Sig;
        }
    }
    return Sig;
}
//---------------------------------------------------------------------------
void __fastcall TfrmKeys::actSignExecute(TObject *Sender)
{
    if (pgpKeyring->SecretCount == 0)
    {
        MessageDlg("There is no secret keys", mtError, TMsgDlgButtons()<<mbOK, 0);
        return;
    }

    if ((tvKeyring->Selected != NULL) && (tvKeyring->Selected->Data != NULL))
    {
        if (((AnsiString(((TObject*)(tvKeyring->Selected->Data))->ClassName())) == "TElPGPCustomUser") &&
           (tvKeyring->Selected->Parent != NULL) &&
           ((AnsiString(((TObject*)(tvKeyring->Selected->Parent->Data))->ClassName())) == "TElPGPPublicKey"))
        {
  //        with TfrmPrivateKeys.Create(Self) do
            TfrmPrivateKeys* pForm = new TfrmPrivateKeys(this);
            try
            {
                pForm->lstKeys->Items->Clear();
                for (int i = 0; i < pgpKeyring->SecretCount; i++)
                    pForm->lstKeys->Items->Add(GetDefaultUserID(pgpKeyring->SecretKeys[i]->PublicKey));

                if (pForm->ShowModal() == mrOk)
                {
                    int i = 0;
                    while ((i < pForm->lstKeys->Items->Count) && (!pForm->lstKeys->Selected[i]))
                        i++;

                    TElPGPSignature* Sig = SignUser(((TElPGPCustomUser*)(tvKeyring->Selected->Data)),
                              ((TElPGPPublicKey*)(tvKeyring->Selected->Parent->Data)),
                              pgpKeyring->SecretKeys[i]);

                    if (Sig != NULL)
                    {
                        ((TElPGPCustomUser*)(tvKeyring->Selected->Data))->AddSignature(Sig);
                        RedrawKeyring(tvKeyring, pgpKeyring);
                        SetStatus("Signed successfully");
                    }
                }
            }
            __finally
            {
                delete pForm;
            }
        }
        else
            MessageDlg("Only User information may be signed", mtError, TMsgDlgButtons()<<mbOK, 0);
    }
}
//---------------------------------------------------------------------------
TElPGPSignature* TfrmKeys::RevokeUser(TElPGPCustomUser* user, TElPGPCustomPublicKey* userKey, TElPGPSecretKey* signingKey)
{
    TElPGPSignature* Sig = new TElPGPSignature;
    Sig->CreationTime = TDateTime::CurrentDateTime();
    try
    {
        signingKey->Revoke(((TElPGPPublicKey*)(userKey)), user, Sig, NULL);
    }
    catch(Exception &e)
    {
        // Exception. Possibly, passphrase is needed.
        signingKey->Passphrase = RequestPassphrase(signingKey->PublicKey);
        try
        {
            signingKey->Revoke(((TElPGPPublicKey*)(userKey)), user, Sig, NULL);
        }
        catch(Exception &e)
        {
            MessageDlg(e.Message, mtError, TMsgDlgButtons()<<mbOK, 0);
            delete Sig;
        }
    }
    return Sig;
}
//---------------------------------------------------------------------------
void __fastcall TfrmKeys::actRevokeExecute(TObject *Sender)
{
    if (pgpKeyring->SecretCount == 0)
    {
        MessageDlg("There is no secret keys", mtError, TMsgDlgButtons()<<mbOK, 0);
        return;
    }

    if ((tvKeyring->Selected != NULL) && (tvKeyring->Selected->Data != NULL))
    {
        if (((AnsiString(((TObject*)(tvKeyring->Selected->Data))->ClassName())) == "TElPGPCustomUser") &&
           (tvKeyring->Selected->Parent != NULL) &&
           ((AnsiString(((TObject*)(tvKeyring->Selected->Parent->Data))->ClassName())) == "TElPGPPublicKey"))
        {
            TfrmPrivateKeys* pForm = new TfrmPrivateKeys(this);
            try
            {
                pForm->lstKeys->Items->Clear();
                for (int i = 0; i < pgpKeyring->SecretCount; i++)
                    pForm->lstKeys->Items->Add(GetDefaultUserID(pgpKeyring->SecretKeys[i]->PublicKey));

                if (pForm->ShowModal() == mrOk)
                {
                    int i = 0;
                    while ((i < pForm->lstKeys->Items->Count) && (!pForm->lstKeys->Selected[i]))
                        i++;

                    TElPGPSignature* Sig = RevokeUser(((TElPGPCustomUser*)(tvKeyring->Selected->Data)),
                              ((TElPGPPublicKey*)(tvKeyring->Selected->Parent->Data)),
                              pgpKeyring->SecretKeys[i]);

                    if (Sig != NULL)
                    {
                        ((TElPGPCustomUser*)(tvKeyring->Selected->Data))->AddSignature(Sig);
                        RedrawKeyring(tvKeyring, pgpKeyring);
                        SetStatus("Revoked successfully");
                    }
                }
            }
            __finally
            {
                delete pForm;
            }
        }
        else
            MessageDlg("Only User information may be revoked", mtError, TMsgDlgButtons()<<mbOK, 0);
    }
}
//---------------------------------------------------------------------------
AnsiString TfrmKeys::RequestPassphrase(TElPGPPublicKey* Key)
{
    AnsiString Result = "";
//    with TfrmPassphraseRequest.Create(Self) do
    TfrmPassphraseRequest* pForm = new TfrmPassphraseRequest(this);
    try
    {
		Byte* KeyID = NULL;
		Key->GetKeyID(KeyID);
		pForm->lbKeyID->Caption = GetDefaultUserID(Key) + " (" + KeyID2Str(KeyID, true) + ")";
        if (pForm->ShowModal() == mrOk)
            Result = pForm->edPassphrase->Text;
    }
    __finally
    {
        delete pForm;
    }
    return Result;
}
//---------------------------------------------------------------------------
void TfrmKeys::SetStatus(AnsiString s)
{
    sbrMain->SimpleText = s;
}
//---------------------------------------------------------------------------
void TfrmKeys::HideAllInfoPanels(void)
{
    pKeyInfo->Visible = false;
    pUserInfo->Visible = false;
    pSigInfo->Visible = false;
}
//---------------------------------------------------------------------------
void TfrmKeys::EnableView(TPanel* p)
{
    p->Align = alClient;
    p->Visible = true;
}
//---------------------------------------------------------------------------
void TfrmKeys::DrawPublicKeyProps(TElPGPCustomPublicKey* Key)
{
    HideAllInfoPanels();
    lbKeyAlgorithm->Caption = (AnsiString)"Algorithm: " + PKAlg2Str(Key->PublicKeyAlgorithm) + " (" + IntToStr(Key->BitsInKey) + " bits)";
	Byte* KeyID = NULL;
	Key->GetKeyID(KeyID);
	lbKeyID->Caption = (AnsiString)"KeyID: " + KeyID2Str(KeyID, false);
    lbKeyFP->Caption = (AnsiString)"KeyFP: " + KeyFP2Str(Key->KeyFP());
    lbTimeStamp->Caption = (AnsiString)"Created: " + FormatDateTime("yyyy/mm/dd hh:nn:ss", Key->Timestamp);
    if (Key->Expires == 0)
      lbExpires->Caption = "Expires: NEVER";
    else
      lbExpires->Caption = (AnsiString)"Expires: " + FormatDateTime("yyyy/mm/dd hh:nn:ss", Key->Timestamp + Key->Expires);

    EnableView(pKeyInfo);
}
//---------------------------------------------------------------------------
void TfrmKeys::DrawUserIDProps(TElPGPUserID* user)
{
    HideAllInfoPanels();
    picUser->Visible = false;
    lbUserName->Visible = true;
    lbUserName->Caption = (AnsiString)"User name: " + user->Name;
    EnableView(pUserInfo);
}
//---------------------------------------------------------------------------
void TfrmKeys::DrawUserAttrProps(TElPGPUserAttr* user)
{
  /*var
    strm: TMemoryStream;*/

  // Load picture (Jpeg format)
    HideAllInfoPanels();
  /*  strm := TMemoryStream.Create();
    picUser.Visible := True;
    lbUserName.Visible := False;
    strm.Write(user.Images[0].JpegData[0], 0, Length(user.get_Images(0).JpegData));
    strm.Position := 0;
    picUser.Image := System.Drawing.Image.FromStream(strm);
    EnableView(pUserInfo);*/
}
//---------------------------------------------------------------------------
void TfrmKeys::DrawSignatureProps(TElPGPSignature* sig, TElPGPCustomUser* user, TElPGPCustomPublicKey* userKey)
{
    AnsiString Validity = "Unable to verify";
    TElPGPCustomPublicKey* Key = NULL;
	HideAllInfoPanels();
	Byte* KeyID = NULL;
	sig->GetSignerKeyID(KeyID);
	pgpKeyring->FindPublicKeyByID(KeyID, Key, 0);

    if (Key != NULL)
    {
        if (AnsiString(Key->ClassName()) == "TElPGPPublicKey")
        {
            lbSigner->Caption = (AnsiString)"Signer: " + GetDefaultUserID(((TElPGPPublicKey*)(Key)));
            if (user != NULL)
            {
                try
                {
                    if (sig->IsUserRevocation())
                    {
                        if (Key->RevocationVerify(userKey, user, sig))
                            Validity = "Valid";
                        else
                            Validity = "INVALID";
                    }
                    else
                    {
                        if (Key->Verify(userKey, user, sig))
                            Validity = "Valid";
                        else
                            Validity = "INVALID";
                    }
                }
                catch(Exception &e)
                {
                    Validity = e.Message;
                }
            }
            else
                Validity = "UserID not found";
        }
        else
            lbSigner->Caption = "Signer: Unknown signer";
    }
    else
        lbSigner->Caption = "Signer: Unknown signer";

    lbSigCreated->Caption = FormatDateTime("yyyy/mm/dd hh:nn:ss", sig->CreationTime);
    lbValidity->Caption = (AnsiString)"Validity: " + Validity;
    if (sig->IsUserRevocation())
        lbSigType->Caption = "Type: User revocation";
    else
        lbSigType->Caption = "Type: Certification signature";

    EnableView(pSigInfo);
}
//---------------------------------------------------------------------------
void TfrmKeys::DrawSignatureProps(TElPGPSignature* sig, TElPGPPublicSubkey* subkey, TElPGPCustomPublicKey* userKey)
{
    AnsiString Validity = "Unable to verify";
    HideAllInfoPanels();

    lbSigner->Caption = (AnsiString)"Signer: " + GetDefaultUserID(((TElPGPPublicKey*)(userKey)));
    if (subkey != NULL)
    {
        try
        {
            if (sig->IsSubkeyRevocation())
            {
                if (userKey->RevocationVerify(subkey, sig))
                    Validity = "Valid";
                else
                    Validity = "INVALID";
            }
            else
            {
                if (userKey->Verify(subkey, sig))
                    Validity = "Valid";
                else
                    Validity = "INVALID";
            }
        }
        catch(Exception &e)
        {
            Validity = e.Message;
        }
    }
    else
        Validity = "Subkey not found";

    lbSigCreated->Caption = FormatDateTime("yyyy/mm/dd hh:nn:ss", sig->CreationTime);
    lbValidity->Caption = (AnsiString)"Validity: " + Validity;
    if (sig->IsSubkeyRevocation())
        lbSigType->Caption = "Type: Subkey revocation";
    else
        lbSigType->Caption = "Type: Subkey binding signature";

    EnableView(pSigInfo);
}
//---------------------------------------------------------------------------
void __fastcall TfrmKeys::tvKeyringChange(TObject *Sender, TTreeNode *Node)
{
    if ((AnsiString(((TObject*)(Node->Data))->ClassName())) == "TElPGPCustomPublicKey")
        DrawPublicKeyProps(((TElPGPCustomPublicKey*)(Node->Data)));
    else if ((AnsiString(((TObject*)(Node->Data))->ClassName())) == "TElPGPUserID")
        DrawUserIDProps(((TElPGPUserID*)(Node->Data)));
    else if ((AnsiString(((TObject*)(Node->Data))->ClassName())) == "TElPGPUserAttr")
        DrawUserAttrProps(((TElPGPUserAttr*)(Node->Data)));
    else if ((AnsiString(((TObject*)(Node->Data))->ClassName())) == "TElPGPSignature")
    {
        if ((Node->Parent != NULL) && ((AnsiString(((TObject*)(Node->Parent->Data))->ClassName())) == "TElPGPCustomUser") &&
            (Node->Parent->Parent != NULL) && ((AnsiString(((TObject*)(Node->Parent->Parent->Data))->ClassName())) == "TElPGPCustomPublicKey"))
        {
            DrawSignatureProps(((TElPGPSignature*)(Node->Data)), ((TElPGPCustomUser*)(Node->Parent->Data)),
                               ((TElPGPCustomPublicKey*)(Node->Parent->Parent->Data)));
        }
        else if ((Node->Parent != NULL) && ((AnsiString(((TObject*)(Node->Parent->Data))->ClassName())) == "TElPGPPublicSubkey") &&
                 (Node->Parent->Parent != NULL) && ((AnsiString(((TObject*)(Node->Parent->Parent->Data))->ClassName())) == "TElPGPCustomPublicKey"))
        {
            DrawSignatureProps(((TElPGPSignature*)(Node->Data)), ((TElPGPPublicSubkey*)(Node->Parent->Data)),
                               ((TElPGPCustomPublicKey*)(Node->Parent->Parent->Data)));
        }
        else
        {
            DrawSignatureProps(((TElPGPSignature*)(Node->Data)), ((TElPGPCustomUser*)(NULL)), NULL);
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TfrmKeys::FormCreate(TObject *Sender)
{
    HideAllInfoPanels();
}
//---------------------------------------------------------------------------
void __fastcall TfrmKeys::actAboutExecute(TObject *Sender)
{
    TfrmAbout* pForm = new TfrmAbout(this);
    try
    {
        pForm->ShowModal();
    }
    __finally
    {
        delete pForm;
    }
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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