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

📄 screencryptbmp.pas

📁 图形的加密与解密的代码
💻 PAS
字号:
unit ScreenCryptBMP;

interface

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

type
   TFormCrypt = class(TForm)
      ButtonLoad: TButton;
      ImageOriginal: TImage;
      ImageEncrypted: TImage;
      ImageDecrypted: TImage;
      ButtonEncrypt: TButton;
      ButtonDecrypt: TButton;
      ButtonSave: TButton;
      EditSeedEncrypt: TEdit;
      LabelSeedEncrypt: TLabel;
      CheckBoxStretch: TCheckBox;
      SavePictureDialog: TSavePictureDialog;
      OpenPictureDialog: TOpenPictureDialog;
      LabelSeedDecrypt: TLabel;
      EditSeedDecrypt: TEdit;
      procedure FormDestroy(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; // ShellExecute


procedure TFormCrypt.EncryptImage;
var
   i: INTEGER;
   j: INTEGER;
   RandomValue: BYTE;
   rowIn: pByteArray;
   rowOut: pByteArray;
   ScanlineByteCount: INTEGER;
begin
   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
      RandSeed := StrToInt(EditSeedEncrypt.Text)
   except
      RandSeed := 79997 // use this prime number if entry is invalid
   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
               RandomValue := Random(256);
               RowOut[i] := RowIn[i] xor RandomValue
            end
      end;

   ImageEncrypted.Picture.Graphic := BitmapEncrypted;

   DecryptImage;

   ButtonDecrypt.Enabled := TRUE;
   ButtonSave.Enabled := TRUE
end {EncryptImage};


procedure TFormCrypt.DecryptImage;
var
   BitmapDecrypted: TBitmap;
   i: INTEGER;
   j: INTEGER;
   RandomValue: BYTE;
   rowIn: pByteArray;
   rowOut: pByteArray;
   ScanlineByteCount: INTEGER;
begin
   BitmapDecrypted := TBitmap.Create;
   BitmapDecrypted.Width := BitmapEncrypted.Width;
   BitmapDecrypted.Height := BitmapEncrypted.Height;
   BitmapDecrypted.PixelFormat := BitmapEncrypted.PixelFormat;

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

   // This finds the number of bytes per scanline regardless of PixelFormat
   ScanlineByteCount := ABS(Integer(BitmapEncrypted.Scanline[1]) -
      Integer(BitmapEncrypted.Scanline[0]));

   try
      RandSeed := StrToInt(EditSeedDecrypt.Text)
   except
      RandSeed := 79997 // use this prime number if entry is invalid
   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
               RandomValue := Random(256);
               RowOut[i] := RowIn[i] xor RandomValue
            end
      end;

   ImageDecrypted.Picture.Graphic := BitmapDecrypted;
   EditSeedDecrypt.Enabled := TRUE;
end {DecryptImage};


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;
         EditSeedEncrypt.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 + -