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

📄 unitexample.pas

📁 AES 加密解密示例
💻 PAS
字号:
{ *********************************************************************** }
{                                                                         }
{ AES Interface Unit 加密算法接口演示程序                                 }
{                                                                         }
{ 版权所有 (c) 2004 杨泽晖                                               }
{                                                                         }
{ *********************************************************************** }

unit UnitExample;

interface

uses
  Windows, Messages, SysUtils, Classes, Forms, Controls, StdCtrls, AES,
  ComCtrls, Dialogs, ExtCtrls;

const
  MsgInformation = '信息';
  MsgError = '错误';

  StrEncStrEmpty = '请输入要加密的字符串。';
  StrDecStrEmpty = '请输入要解密的字符串。';
  StrSrcFileNotExists = '要进行操作的源文件不存在。';

  StrKeyBitMode = '%s 密匙支持的密码最大长度为 %d 个字符。';

  StrOpraTime = '操作时间 : %d 毫秒';
  StrEncrypting = '操作状态 : 正在加密...';
  StrEncrypted = '操作状态 : 加密完成';
  StrDecrypting = '操作状态 : 正在解密...';
  StrDecrypted = '操作状态 : 解密完成';

type
  TFormAES = class(TForm)
    btnEnc: TButton;
    btnExit: TButton;
    btnDec: TButton;
    btnAbout: TButton;
    PageControl: TPageControl;
    TabSheet1: TTabSheet;
    GroupBox1: TGroupBox;
    memSrcStr: TMemo;
    GroupBox2: TGroupBox;
    memEncStr: TMemo;
    GroupBox3: TGroupBox;
    memDecStr: TMemo;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    GroupBox4: TGroupBox;
    GroupBox5: TGroupBox;
    ledSrcFile: TLabeledEdit;
    btnOpenFile: TButton;
    btnSaveFile: TButton;
    ledDstFile: TLabeledEdit;
    Panel1: TPanel;
    ledKey: TLabeledEdit;
    lblKeyHint: TLabel;
    cbKeyBit: TComboBox;
    Label2: TLabel;
    GroupBox6: TGroupBox;
    lblTime: TLabel;
    OpenDialog: TOpenDialog;
    SaveDialog: TSaveDialog;
    lblState: TLabel;
    Label1: TLabel;
    imAppIcon: TImage;
    procedure btnDecClick(Sender: TObject);
    procedure btnEncClick(Sender: TObject);
    procedure btnAboutClick(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure cbKeyBitSelect(Sender: TObject);
    procedure btnOpenFileClick(Sender: TObject);
    procedure btnSaveFileClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormAES: TFormAES;

implementation

{$R *.DFM}

procedure TFormAES.cbKeyBitSelect(Sender: TObject);
begin
  case cbKeyBit.ItemIndex of
    0: ledKey.MaxLength := 16;
    1: ledKey.MaxLength := 24;
    2: ledKey.MaxLength := 32;
  end;
  lblKeyHint.Caption := Format(StrKeyBitMode, [cbKeyBit.Text, ledKey.MaxLength]);
end;

procedure TFormAES.btnEncClick(Sender: TObject);
var
  Start, Stop: Cardinal;
begin
  case PageControl.ActivePageIndex of
    0: {  --  字符串加密  --  }
      begin
        if Length(memSrcStr.Text) > 0 then
        begin
          {  --  调用标准例程 EncryptString 加密字符串  --  }
          case cbKeyBit.ItemIndex of
            0: memEncStr.Text := EncryptString(memSrcStr.Text, ledKey.Text);
               {  --  此处省略了 KeyBit的参数  KeyBit := kb128  -- }
            1: memEncStr.Text := EncryptString(memSrcStr.Text, ledKey.Text, kb192);
            2: memEncStr.Text := EncryptString(memSrcStr.Text, ledKey.Text, kb256);
          end;
        end else
          MessageBox(Handle, StrEncStrEmpty, MsgInformation, MB_ICONINFORMATION);
      end;
    1: {  --  流加密  --  }
      begin
        MessageBox(Handle, '暂时没有流加密演示。', MsgInformation, MB_ICONINFORMATION);
      end;
    2: {  --  文件加密  --  }
      begin
        if FileExists(ledSrcFile.Text) then
        begin
          lblState.Caption := StrEncrypting;
          Start := GetTickCount;
          case cbKeyBit.ItemIndex of
            0: EncryptFile(ledSrcFile.Text, ledDstFile.Text, ledKey.Text);
             {  --  此处省略了 KeyBit的参数  KeyBit := kb128  -- }
            1: EncryptFile(ledSrcFile.Text, ledDstFile.Text, ledKey.Text, kb192);
            2: EncryptFile(ledSrcFile.Text, ledDstFile.Text, ledKey.Text, kb256);
          end;
          Stop := GetTickCount;
          lblTime.Caption := Format(StrOpraTime, [Stop - Start]);
          lblState.Caption := StrEncrypted;
         end else
          MessageBox(Handle, StrSrcFileNotExists, MsgError, MB_ICONERROR);
      end;
  end;
end;

procedure TFormAES.btnDecClick(Sender: TObject);
var
  Start, Stop: Cardinal;
begin
  case PageControl.ActivePageIndex of
    0:
      begin
        if Length(memEncStr.Text) > 0 then
        begin
          {  --  调用标准例程 DecryptString 解密字符串  --  }
          case cbKeyBit.ItemIndex of
            0: memDecStr.Text := DecryptString(memEncStr.Text, ledKey.Text);
             {  --  此处省略了 KeyBit的参数  KeyBit := kb128  -- }
            1: memDecStr.Text := DecryptString(memEncStr.Text, ledKey.Text, kb192);
            2: memDecStr.Text := DecryptString(memEncStr.Text, ledKey.Text, kb256);
          end;
        end else
          MessageBox(Handle, StrDecStrEmpty, MsgInformation, MB_ICONINFORMATION);
      end;
    1: {  --  流解密  --  }
      begin
        MessageBox(Handle, '暂时没有流解密演示。', MsgInformation, MB_ICONINFORMATION);
      end;
    2: {  --  文件解密  --  }
      begin
        if FileExists(ledSrcFile.Text) then
        begin
          lblState.Caption := StrDecrypting;
          Start := GetTickCount;
          case cbKeyBit.ItemIndex of
            0: DecryptFile(ledSrcFile.Text, ledDstFile.Text, ledKey.Text);
             {  --  此处省略了 KeyBit的参数  KeyBit := kb128  -- }
            1: DecryptFile(ledSrcFile.Text, ledDstFile.Text, ledKey.Text, kb192);
            2: DecryptFile(ledSrcFile.Text, ledDstFile.Text, ledKey.Text, kb256);
          end;
          Stop := GetTickCount;
          lblTime.Caption := Format(StrOpraTime, [Stop - Start]);
          lblState.Caption := StrDecrypted;
         end else
          MessageBox(Handle, StrSrcFileNotExists, MsgError, MB_ICONERROR);
      end;
  end;
end;

procedure TFormAES.btnAboutClick(Sender: TObject);
var
  S: string;
begin
  S := 'AES 加密算法演示程序 v1.3' + #13 + #13 +
    'Advanced Encryption Standard (AES)' + #13 +
    'Copyright (c) 1998-2001 EldoS, Alexander Ionov.' + #13 + #13 +
    'AES Interface Unit v1.3' + #13 +
    'Copyright (c) 2004 Jorlen Young.' + #13 + #13 +
    'Website:' + #13 +
    '         http://jorlen.51.net/' + #13 +
    '         http://mycampus.1155.net/' + #13 +
    '         http://mycampus.ecoo.net/' + #13 +
    '         http://mycampus.5500.org/' + #13 + #13 +
    'Email: stanley_xfx@163.com';        
  MessageBox(Handle, PChar(S), MsgInformation, MB_ICONINFORMATION);
end;

procedure TFormAES.btnExitClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TFormAES.btnOpenFileClick(Sender: TObject);
begin
  with OpenDialog do
  begin
    if Execute then
      ledSrcFile.Text := FileName;
  end;
end;

procedure TFormAES.btnSaveFileClick(Sender: TObject);
begin
  with SaveDialog do
  begin
    if Execute then
      ledDstFile.Text := FileName;
  end;
end;

procedure TFormAES.FormCreate(Sender: TObject);
begin
  imAppIcon.Picture.Icon.Assign(Application.Icon);
end;

end.

⌨️ 快捷键说明

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