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

📄 unit1.pas

📁 一个加密解密算法
💻 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;
    ImageOriginal: TImage;
    ImageEncrypted: TImage;
    ImageDecrypted: TImage;
    GroupBox2: TGroupBox;
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
      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;
   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]));

   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]+10000) mod 256
              else if(i mod 4=1 )
               then
               RowOut[i] := (RowIn[i]+20000) mod 256
               else if(i mod 4=2 )
               then
               RowOut[i] := (RowIn[i]+30000) mod 256
               else if(i mod 4=3 )
               then
               RowOut[i] := (RowIn[i]+40000) 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;
   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]));

   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]-10000) ) mod 256
              else if(i mod 4=1 )
               then
               RowOut[i] := ( 256+(RowIn[i]-20000) ) mod 256
               else if(i mod 4=2 )
               then
              RowOut[i] := ( 256+(RowIn[i]-30000) ) mod 256
               else if(i mod 4=3 )
               then
               RowOut[i] := ( 256+(RowIn[i]-40000) ) 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 + -