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

📄 unit1.pas

📁 通Delphi7.0实现对.bmp位图图像的加密解密
💻 PAS
字号:
unit unit1;

interface

uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   ExtCtrls, StdCtrls, ExtDlgs, Buttons;

type
   TFormCrypt = class(TForm)
      ButtonLoad: TButton;
      ButtonEncrypt: TButton;
      ButtonDecrypt: TButton;
      ButtonSave: TButton;
      CheckBoxStretch: TCheckBox;
      SavePictureDialog: TSavePictureDialog;
      OpenPictureDialog: TOpenPictureDialog;
    GroupBox1: TGroupBox;
    Panel1: TPanel;
    ImageOriginal: TImage;
    Panel2: TPanel;
    ImageEncrypted: TImage;
    Panel3: TPanel;
    ImageDecrypted: TImage;
    Label4: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
      procedure FormDestroy(Sender: TObject);
      procedure FormMouseMove(Sender: TObject);
      procedure ButtonLoadClick(Sender: TObject);
      procedure ButtonEncryptClick(Sender: TObject);
      procedure ButtonDecryptClick(Sender: TObject);
      procedure CheckBoxStretchClick(Sender: TObject);
      procedure EditSeedEncryptChange(Sender: TObject);
      procedure EditSeedDecryptChange(Sender: TObject);
      procedure EditNumericKeyPress(Sender: TObject; var Key: Char);
      procedure ButtonSaveClick(Sender: TObject);
   private
      BitmapOriginal: TBitmap;
      BitmapEncrypted: TBitmap;
      procedure DecryptImage;
      procedure EncryptImage;
   public
      { Public declarations }
   end;

var
   FormCrypt: TFormCrypt;

implementation
{$R *.DFM}

uses
   ShellAPI;


procedure TFormCrypt.FormMouseMove (Sender:TObject);
begin
FormCrypt.repaint;
FormCrypt.Canvas.pen.color:=clRed;
FormCrypt.Canvas.pen.width:=10;
FormCrypt.Canvas.polyline([point(0,0),point(FormCrypt.width-10,0),point(FormCrypt.width-10,FormCrypt.height-30),point(0,FormCrypt.height-30),point(0,0)]);
end;

procedure TFormCrypt.EncryptImage;
var
   i: INTEGER;
   j: INTEGER;
   a: INTEGER;
   b: INTEGER;
   c: INTEGER;
   d: INTEGER;
   rowIn: pByteArray;
   rowOut: pByteArray;
   ScanlineByteCount: INTEGER;
begin
if(edit1.text='')or(edit2.text='')or(edit3.text='')or(edit4.text='')
 then
 begin
 showMessage('请输入四个密钥值');
 exit;
 end;
   if Assigned(BitmapEncrypted)
      then BitmapEncrypted.Free;
   BitmapEncrypted := TBitmap.Create;
   BitmapEncrypted.Width := BitmapOriginal.Width;
   BitmapEncrypted.Height := BitmapOriginal.Height;
   BitmapEncrypted.PixelFormat := BitmapOriginal.PixelFormat;
    if BitmapOriginal.PixelFormat in [pf1bit, pf4bit, pf8bit]
      then BitmapEncrypted.Palette := CopyPalette(BitmapOriginal.Palette);

    ScanlineByteCount := ABS(Integer(BitmapOriginal.Scanline[1]) -
      Integer(BitmapOriginal.Scanline[0]));
   try
   a:=strtoint(edit1.text);
   b:=strtoint(edit2.text);
   c:=strtoint(edit3.text);
   d:=strtoint(edit4.text);
   except
   showMessage('请输入0-9数字字符');
   exit;
   end;
   for j := 0 to BitmapOriginal.Height - 1 do

      begin

         RowIn := BitmapOriginal.Scanline[j];
         RowOut := BitmapEncrypted.Scanline[j];

         for i := 0 to ScanlineByteCount - 1 do
         begin
           if (j mod 2 =0)
           then
            begin
               if(i mod 4=0 )
               then
               RowOut[i] := (RowIn[i]+a) mod 256
              else if(i mod 4=1 )
               then
               RowOut[i] := (RowIn[i]+b) mod 256
               else if(i mod 4=2 )
               then
               RowOut[i] := (RowIn[i]+c) mod 256
               else if(i mod 4=3 )
               then
               RowOut[i] := (RowIn[i]+d) mod 256
               end
               else
               begin
               if(i mod 4=0 )
               then
               RowOut[i] := (RowIn[i]+500) mod 256
              else if(i mod 4=1 )
               then
               RowOut[i] := (RowIn[i]+700) mod 256
               else if(i mod 4=2 )
               then
               RowOut[i] := (RowIn[i]+100) mod 256
               else if(i mod 4=3 )
               then
               RowOut[i] := (RowIn[i]+1000) mod 256
            end
            end

      end;
   ImageEncrypted.Picture.Graphic := BitmapEncrypted;
   ButtonDecrypt.Enabled := TRUE;
   ButtonSave.Enabled := TRUE
end;

procedure TFormCrypt.DecryptImage;
var
   BitmapDecrypted: TBitmap;
   i: INTEGER;
   j: INTEGER;
   a: INTEGER;
   b: INTEGER;
   c: INTEGER;
   d: INTEGER;
   rowIn: pByteArray;
   rowOut: pByteArray;
   ScanlineByteCount: INTEGER;
begin
   BitmapDecrypted := TBitmap.Create;
   BitmapDecrypted.Width := BitmapEncrypted.Width;
   BitmapDecrypted.Height := BitmapEncrypted.Height;
   BitmapDecrypted.PixelFormat := BitmapEncrypted.PixelFormat;

   if BitmapEncrypted.PixelFormat in [pf1bit, pf4bit, pf8bit]
      then BitmapDecrypted.Palette := CopyPalette(BitmapEncrypted.Palette);

   ScanlineByteCount := ABS(Integer(BitmapEncrypted.Scanline[1]) -
      Integer(BitmapEncrypted.Scanline[0]));
   try
   a:=strtoint(edit1.text);
   b:=strtoint(edit2.text);
   c:=strtoint(edit3.text);
   d:=strtoint(edit4.text);
   except
   showMessage('请输入0-9数字字符');
   exit;
   end;
   for j := 0 to BitmapEncrypted.Height - 1 do
      begin
         RowIn := BitmapEncrypted.Scanline[j];
         RowOut := BitmapDecrypted.Scanline[j];

         for i := 0 to ScanlineByteCount - 1 do

            begin
               if (j mod 2 =0)
               then
                begin
               if(i mod 4=0 )
               then
               RowOut[i] := ( 256+(RowIn[i]-a) ) mod 256
              else if(i mod 4=1 )
               then
               RowOut[i] := ( 256+(RowIn[i]-b) ) mod 256
               else if(i mod 4=2 )
               then
              RowOut[i] := ( 256+(RowIn[i]-c) ) mod 256
               else if(i mod 4=3 )
               then
               RowOut[i] := ( 256+(RowIn[i]-d) ) mod 256
              end
              else
                if(i mod 4=0 )
               then
               RowOut[i] := ( 256+(RowIn[i]-500) ) mod 256
              else if(i mod 4=1 )
               then
               RowOut[i] := ( 256+(RowIn[i]-700) ) mod 256
               else if(i mod 4=2 )
               then
              RowOut[i] := ( 256+(RowIn[i]-100) ) mod 256
               else if(i mod 4=3 )
               then
               RowOut[i] := ( 256+(RowIn[i]-1000) ) mod 256
            end

      end;
ImageDecrypted.Picture.Graphic := BitmapDecrypted;
end;

procedure TFormCrypt.FormDestroy(Sender: TObject);
begin
   BitmapOriginal.Free;
   BitmapEncrypted.Free
end;


procedure TFormCrypt.ButtonLoadClick(Sender: TObject);
begin
   if OpenPictureDialog.Execute
      then begin
         if Assigned(BitmapOriginal)
            then BitmapOriginal.Free;

         BitmapOriginal := TBitmap.Create;
         BitmapOriginal.LoadFromFile(OpenPictureDialog.Filename);
         ImageOriginal.Picture.Graphic := BitmapOriginal;

         ButtonEncrypt.Enabled := TRUE;
 
      end
end;


procedure TFormCrypt.ButtonEncryptClick(Sender: TObject);
begin
   EncryptImage
end;


procedure TFormCrypt.ButtonDecryptClick(Sender: TObject);
begin
   DecryptImage
end;


procedure TFormCrypt.CheckBoxStretchClick(Sender: TObject);
begin
   ImageOriginal.Stretch := CheckBoxStretch.Checked;
   ImageEncrypted.Stretch := CheckBoxStretch.Checked;
   ImageDecrypted.Stretch := CheckBoxStretch.Checked
end;


procedure TFormCrypt.EditSeedEncryptChange(Sender: TObject);
begin
   EncryptImage
end;


procedure TFormCrypt.EditSeedDecryptChange(Sender: TObject);
begin
   DecryptImage
end;

procedure TFormCrypt.EditNumericKeyPress(Sender: TObject;
   var Key: Char);

const
   Backspace = #$08;
begin
   if not (Key in [Backspace, '0'..'9'])
      then Key := #$00
end;


procedure TFormCrypt.ButtonSaveClick(Sender: TObject);
begin
   if SavePictureDialog.Execute
      then BitmapEncrypted.SaveToFile(SavePictureDialog.Filename)
end;


end.

⌨️ 快捷键说明

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