mainform.pas

来自「著名的SecureBlackBox控件完整源码」· PAS 代码 · 共 2,311 行 · 第 1/5 页

PAS
2,311
字号
unit MainForm;

interface

{$IFDEF VER120}
  {$DEFINE D_3_UP}
  {$DEFINE D_4_UP}
  {$DEFINE VCL40}
{$ENDIF}

{$IFDEF VER125}
  {$DEFINE B_3_UP}
  {$DEFINE B_4_UP}
  {$DEFINE B_4}
  {$DEFINE VCL40}
  {$DEFINE BUILDER_USED}
{$ENDIF}

{$IFDEF VER130}
	{$IFDEF BCB}
		{$DEFINE B_3_UP}
		{$DEFINE B_4_UP}
		{$DEFINE B_5_UP}
		{$DEFINE B_5}
		{$DEFINE VCL40}
		{$DEFINE VCL50}
		{$DEFINE BUILDER_USED}
    {$ELSE}
		{$DEFINE D_3_UP}
		{$DEFINE D_4_UP}
		{$DEFINE D_5_UP}
		{$DEFINE VCL40}
		{$DEFINE VCL50}
		{.DEFINE USEADO}
    {$ENDIF}
{$ENDIF}

{$IFDEF VER140}
	{$IFDEF BCB}
		{$DEFINE B_3_UP}
		{$DEFINE B_4_UP}
		{$DEFINE B_5_UP}
		{$DEFINE B_6_UP}
		{$DEFINE B_6}
		{$DEFINE VCL40}
		{$DEFINE VCL50}
		{$DEFINE VCL60}
		{$DEFINE BUILDER_USED}
    {$ELSE}
		{$DEFINE D_3_UP}
		{$DEFINE D_4_UP}
		{$DEFINE D_5_UP}
		{$DEFINE D_6_UP}
		{$DEFINE D_6}
		{$DEFINE VCL40}
		{$DEFINE VCL50}
		{$DEFINE VCL60}
		{.DEFINE USEADO}
    {$ENDIF}
{$ENDIF}


{$IFDEF VER150}
	{$IFNDEF BCB}
		{$DEFINE D_3_UP}
		{$DEFINE D_4_UP}
		{$DEFINE D_5_UP}
		{$DEFINE D_6_UP}
		{$DEFINE D_7_UP}
		{$DEFINE D_7}
		{$DEFINE VCL40}
		{$DEFINE VCL50}
		{$DEFINE VCL60}
		{$DEFINE VCL70}
		{.DEFINE USEADO}
    {$ENDIF}
{$ENDIF}

{$IFDEF VER160}
    {$DEFINE D_3_UP}
    {$DEFINE D_4_UP}
    {$DEFINE D_5_UP}
    {$DEFINE D_6_UP}
    {$DEFINE D_7_UP}
    {$DEFINE D_8_UP}
    {$DEFINE D_8}
    {$DEFINE VCL40}
    {$DEFINE VCL50}
    {$DEFINE VCL60}
    {$DEFINE VCL70}
    {$DEFINE VCL80}
    {.$DEFINE USE_NAME_SPACE} // Optional !!!
{$ENDIF}

{$IFDEF VER170}
    {$DEFINE D_3_UP}
    {$DEFINE D_4_UP}
    {$DEFINE D_5_UP}
    {$DEFINE D_6_UP}
    {$DEFINE D_7_UP}
    {$DEFINE D_8_UP}
    {$DEFINE D_9_UP}
    {$DEFINE D_9}
    {$DEFINE VCL40}
    {$DEFINE VCL50}
    {$DEFINE VCL60}
    {$DEFINE VCL70}
    {$DEFINE VCL80}
    {$DEFINE VCL90}
    {.$DEFINE USE_NAME_SPACE} // Optional !!!
{$ENDIF}

{$ifdef CLR}
	{$DEFINE DELPHI_NET}
{$endif}

uses
{$IFDEF DELPHI_NET}
  System.IO,
  System.Text,
  System.Drawing,
  Borland.VCL.Windows, Borland.VCL.Messages, Borland.VCL.SysUtils,
  Borland.VCL.Classes,
  Borland.VCL.Forms, Borland.VCL.Controls,
  Borland.VCL.Dialogs, Borland.VCL.StdCtrls, Borland.VCL.Buttons,
  Borland.VCL.ComCtrls, Borland.VCL.ExtCtrls,
  Borland.VCL.Graphics,
{$ELSE}
  Windows, Messages, SysUtils, Classes,
  Graphics, Controls, Forms, {$IFDEF D_6_UP}Variants,{$ENDIF}
  Dialogs, StdCtrls, Buttons, ComCtrls, ExtCtrls,
{$ENDIF}
  SBRDN, SBUtils, SBCustomCertStorage, SBX509, SBX509Ext, SBConstants,
  SBMessages, SBMIME, SBSMIMECore,
  SBPGPKeys, SBPGPConstants, SBPGPUtils, SBPGPMIME;

type
  TfrmMain = class(TForm)
    Panel3: TPanel;
    Bevel1: TBevel;
    Panel2: TPanel;
    btnBack: TButton;
    btnNext: TButton;
    btnCancel: TButton;
    Panel1: TPanel;
    imgLogo: TImage;
    PageControl: TPageControl;
    tsSelectCertificates: TTabSheet;
    lbSelectCertificates: TLabel;
    lbxCertificates: TListBox;
    btnAddCertificate: TButton;
    btnRemoveCertificate: TButton;
    tsAlgorithm: TTabSheet;
    lbChooseAlgorithm: TLabel;
    rbTripleDES: TRadioButton;
    rbRC4_128: TRadioButton;
    rbRC4_40: TRadioButton;
    rbRC2: TRadioButton;
    rbAES_128: TRadioButton;
    rbAES_256: TRadioButton;
    tsSelectFiles: TTabSheet;
    lbSelectFiles: TLabel;
    lbInputFile: TLabel;
    sbInputFile: TSpeedButton;
    sbOutputFile: TSpeedButton;
    lbOutputFile: TLabel;
    edInputFile: TEdit;
    edOutputFile: TEdit;
    tsCheckData: TTabSheet;
    lbInfo: TLabel;
    mmInfo: TMemo;
    btnDoIt: TButton;
    tsSignAlgorithm: TTabSheet;
    lbChooseSignAlgorithm: TLabel;
    rbMD5: TRadioButton;
    rbSHA1: TRadioButton;
    OpenDlg: TOpenDialog;
    SaveDlg: TSaveDialog;
    tsSelectAction: TTabSheet;
    lbActionToPerform: TLabel;
    rbSMimeVerify: TRadioButton;
    rbSMimeDecrypt: TRadioButton;
    rbSMimeSign: TRadioButton;
    rbSMimeEncrypt: TRadioButton;
    tsResult: TTabSheet;
    lbSMime: TLabel;
    lbPGPMime: TLabel;
    rbPGPMimeEncrypt: TRadioButton;
    rbPGPMimeSign: TRadioButton;
    rbPGPMimeDecrypt: TRadioButton;
    rbPGPMimeVerify: TRadioButton;
    rbDES: TRadioButton;
    rbAES_192: TRadioButton;
    mmResult: TMemo;
    lbResult: TLabel;
    tsSelectKeys: TTabSheet;
    lbSelectKeys: TLabel;
    btnAddKey: TButton;
    btnRemoveKey: TButton;
    tsSelectKey: TTabSheet;
    lbSelectKey: TLabel;
    lbKeyring: TLabel;
    edKeyring: TEdit;
    sbKeyring: TSpeedButton;
    tvKeys: TTreeView;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnNextClick(Sender: TObject);
    procedure sbInputFileClick(Sender: TObject);
    procedure sbOutputFileClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnBackClick(Sender: TObject);
    procedure btnAddCertificateClick(Sender: TObject);
    procedure btnRemoveCertificateClick(Sender: TObject);
    procedure btnRemoveKeyClick(Sender: TObject);
    procedure sbKeyringClick(Sender: TObject);
    procedure btnAddKeyClick(Sender: TObject);
  private
    FAction: Integer;
    FCurrentPage: Integer;
    FMemoryCertStorage: TElMemoryCertStorage;
    FKeyring: TElPGPKeyring;

    FSecretRing: TElPGPKeyring;
    FPublicRing: TElPGPKeyring;
    
    procedure ClearData;

    procedure SMimeEncryptNext;
    procedure SMimeDecryptNext;
    procedure SMimeSignNext;
    procedure SMimeVerifyNext;

    function SMimeEncrypt(const InputFileName, OutputFileName: string;
                const CryptAlgorithm, CryptAlgorithmBitsInKey: Integer): string;
    function SMimeDecrypt(const InputFileName, OutputFileName: string): string;
    function SMimeSign(const InputFileName, OutputFileName: string;
                const SignAlgorithm: string): string;
    function SMimeVerify(const InputFileName: string): string;

    procedure PGPEncryptNext;
    procedure PGPDecryptNext;
    procedure PGPSignNext;
    procedure PGPVerifyNext;

    function PGPEncrypt(const InputFileName, OutputFileName: string): string;
    function PGPDecrypt(const InputFileName, OutputFileName: string): string;
    function PGPSign(const InputFileName, OutputFileName: string): string;
    function PGPVerify(const InputFileName: string): string;

    procedure PGPMIMEKeyPassphrase(Sender: TObject; Key : TElPGPCustomSecretKey;
                   var Passphrase: string; var Cancel: boolean);

    function GetAlgorithm: Integer;
    function GetAlgorithmBitsInKey: Integer;
    function GetAlgorithmName: string;
    function GetSignAlgorithm: string;

    procedure SetResults(const Res: string);

    procedure UpdateCertificatesList;
    function WriteCertificateInfo(Storage: TElCustomCertStorage): string;

    procedure UpdateKeysList;
    function WriteKeyringInfo(Keyring: TElPGPKeyring): string;
  public
    procedure SetPage(Page: Integer);
    procedure Back;
    procedure Next;

    property Action: Integer read FAction write FAction;
    property CurrentPage: Integer read FCurrentPage write FCurrentPage;
  end;

var
  frmMain: TfrmMain;

const
  ACTION_UNKNOWN                = 0;
  ACTION_SMIME_ENCRYPT          = 1;
  ACTION_SMIME_SIGN             = 2;
  ACTION_SMIME_DECRYPT          = 3;
  ACTION_SMIME_VERIFY           = 4;
  ACTION_PGPMIME_ENCRYPT        = 5;
  ACTION_PGPMIME_SIGN           = 6;
  ACTION_PGPMIME_DECRYPT        = 7;
  ACTION_PGPMIME_VERIFY         = 8;

  PAGE_DEFAULT                  = 0;
  PAGE_SELECT_ACTION            = 1;
  PAGE_SELECT_FILES             = 2;
  PAGE_SELECT_CERTIFICATES      = 3;
  PAGE_SELECT_ALGORITHM         = 4;
  PAGE_SELECT_KEYS              = 5;
  PAGE_CHECK_DATA               = 6;
  PAGE_PROCESS                  = 7;

const
  cDemoVersion = '2005.04.18';
  cXMailerDemoFieldValue = 'EldoS ElMime Demos, version: ' + cDemoVersion +
    ' ( '+cXMailerDefaultFieldValue + ' )';

resourcestring
  sSelectFilesForEncryption        = 'Please select message file to encrypt and file where to write encrypted data';
  sSelectFilesForDecryption        = 'Please select input (encrypted) file and file where to write decrypted data';
  sSelectFilesForSigning           = 'Please select file to sign and file where to write signed data';
  sSelectFilesForVerifying         = 'Please select file with a signed message'{ and file where to put original message'};

  sSelectCertificatesForEncryption = 'Please choose certificates which should be used to encrypt message';
  sSelectCertificatesForDecryption = 'Please select certificates which may be used to decrypt message. Each certificate should be loaded with corresponding private key';
  sSelectCertificatesForSigning    = 'Please choose certificates which should be used to sign the file. At least one certificate must be loaded with corresponding private key';
  sSelectCertificatesForVerifying  = 'Please select certificates which may be used to verify digital signature. Note, that in most cases signer''s certificates are included in signed message, so you may leave certificate list empty';

  sSelectKeysForEncryption         = 'Please choose PGP public key which should be used to encrypt message';
  sSelectKeysForDecryption         = 'Please select PGP secret keys which may be used to decrypt message';
  sSelectKeysForSigning            = 'Please choose PGP secret key which should be used to sign the file';
  sSelectKeysForVerifying          = 'Please select PGP public keys which may be used to verify digital signature.';

  sInfoEncryption                  = 'Ready to start encryption. Please check all the parameters to be valid';
  sInfoSigning                     = 'Ready to start signing. Please check that all signing options are correct.';
  sInfoDecryption                  = 'Ready to start decryption. Please check that all decryption options are correct.';
  sInfoVerifying                   = 'Ready to start verifying. Please check that all options are correct.';

  sSelectInputFiles                = 'You must select input file';
  sSelectInputOutputFiles          = 'You must select both input and output files';

implementation

{$R *.dfm}

function GetStringByOID(const S : BufferType) : string;
begin
  if CompareContent(S, SB_CERT_OID_COMMON_NAME) then
    Result := 'CommonName'
  else
  if CompareContent(S, SB_CERT_OID_COUNTRY) then
    Result := 'Country'
  else
  if CompareContent(S, SB_CERT_OID_LOCALITY) then
    Result := 'Locality'
  else
  if CompareContent(S, SB_CERT_OID_STATE_OR_PROVINCE) then
    Result := 'StateOrProvince'
  else
  if CompareContent(S, SB_CERT_OID_ORGANIZATION) then
    Result := 'Organization'
  else
  if CompareContent(S, SB_CERT_OID_ORGANIZATION_UNIT) then
    Result := 'OrganizationUnit'
  else
  if CompareContent(S, SB_CERT_OID_EMAIL) then
    Result := 'Email'
  else
    Result := 'UnknownField';
end;

function GetOIDValue(NTS: TElRelativeDistinguishedName; const S: BufferType; const Delimeter: AnsiString = ' / '): AnsiString;
var
  i: Integer;
  t: AnsiString;
begin
  Result := '';
  for i := 0 to NTS.Count - 1 do
    if CompareContent(S, NTS.OIDs[i]) then
    begin
      t := AnsiString(NTS.Values[i]);
      if t = '' then
        Continue;

      if Result = '' then
      begin
        Result := t;
        if Delimeter = '' then
          Exit;
      end
      else
        Result := Result + Delimeter + t;
    end;
end;

function GetPublicKeyNames(Key: TElPGPPublicKey): string;
var
  i: Integer;
begin
  Result := '';
  if not Assigned(Key) then
    Exit;

  for i := 0 to Key.UserIDCount - 1 do
    if Key.UserIDs[i].Name <> '' then
    begin
      if Result <> '' then
        Result := Result + ', ';

      Result := Result + Key.UserIDs[i].Name;
    end;
end;

procedure TfrmMain.Back;
var
  NewPage: Integer;
begin
  if not Assigned(PageControl.ActivePage) then
  begin
    SetPage(PAGE_DEFAULT);
    Exit;
  end;

  NewPage := PAGE_DEFAULT;
  case CurrentPage of
    PAGE_SELECT_FILES: NewPage := PAGE_SELECT_ACTION;
    PAGE_SELECT_CERTIFICATES: NewPage := PAGE_SELECT_FILES;
    PAGE_SELECT_ALGORITHM: NewPage := PAGE_SELECT_CERTIFICATES;
    PAGE_SELECT_KEYS: NewPage := PAGE_SELECT_FILES;
    PAGE_CHECK_DATA:
    begin
      case Action of
        ACTION_SMIME_ENCRYPT, ACTION_SMIME_SIGN:
          NewPage := PAGE_SELECT_ALGORITHM;

        ACTION_SMIME_DECRYPT, ACTION_SMIME_VERIFY:
          NewPage := PAGE_SELECT_CERTIFICATES;
      else
        NewPage := PAGE_SELECT_KEYS;
      end;
    end;

    PAGE_PROCESS: NewPage := PAGE_CHECK_DATA;
  end;

  SetPage(NewPage);
end;

procedure TfrmMain.btnAddCertificateClick(Sender: TObject);
var
  F: TFileStream;
  Buf: array of Byte;
  Cert: TElX509Certificate;
  sFrom: string;
  KeyLoaded: Boolean;
  Res: Integer;
{$IFDEF DELPHI_NET}
  Sz: Integer;
{$ELSE}
  Sz: Word;
{$ENDIF}
  Index : integer;
begin
  KeyLoaded := False;
  OpenDlg.FileName := '';
  OpenDlg.Title := 'Select certificate file';
  OpenDlg.Filter := 'PEM-encoded certificate (*.pem)|*.pem|DER-encoded certificate (*.cer)|*.cer|PFX-encoded certificate (*.pfx)|*.pfx';
  if not OpenDlg.Execute then
    Exit;

  F := TFileStream.Create(OpenDlg.Filename, fmOpenRead or fmShareExclusive);
  SetLength(Buf, F.Size);
  F.Read({$IFDEF DELPHI_NET}Buf, 0{$ELSE}Buf[0]{$ENDIF}, F.Size);
  F.Free;

  Res := 0;
  Cert := TElX509Certificate.Create(nil);
  if OpenDlg.FilterIndex = 3 then

⌨️ 快捷键说明

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