delphi 异或运算 加密办法

2023-12-14 15:35:23

在这里插入图片描述

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  System.IOUtils, System.Hash, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
  IdHMAC, IdHMACSHA1, IdGlobal, System.Generics.Collections, Vcl.StdCtrls, IdSSL,
  IdSSLOpenSSL, IdBaseComponent, IdComponent, IdServerIOHandler;

type
  TForm1 = class(TForm)
    btnbut: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    Edit3: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit4: TEdit;
    Button3: TButton;
    Button4: TButton;
    Edit5: TEdit;

    procedure btnbutClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  OutFileName: string;
  MaxBufferSize: Integer;
  inputFileName: string;

implementation

{$R *.dfm}

procedure EncryptFile(const FileName, OutputFileName: string; const Key: Word);
var
  InputFile, OutputFile: TFileStream;
  Buffer: array of Byte; // 动态大小的缓冲区
  BytesRead, I: Integer;
  EncryptedByte: Byte;
begin
  ShowMessage(OutputFileName);
  InputFile := TFileStream.Create(FileName, fmOpenRead);
  OutputFile := TFileStream.Create(OutputFileName, fmCreate);
  try
    SetLength(Buffer, MaxBufferSize); // 根据最大缓冲区大小设置动态缓冲区大小
    while InputFile.Position < InputFile.Size do
    begin
      BytesRead := InputFile.Read(Buffer[0], Length(Buffer));
      for I := 0 to BytesRead - 1 do
      begin
        EncryptedByte := Buffer[I] xor (Key shr 8);
        Buffer[I] := EncryptedByte;
      end;
      OutputFile.WriteBuffer(Buffer[0], BytesRead);
    end;
  finally
    InputFile.Free;
    OutputFile.Free;
  end;
end;

procedure DecryptFile(const FileName, OutputFileName: string; const Key: Word);
begin

  EncryptFile(FileName, OutputFileName, Key);
end;


// 使用示例
procedure TForm1.btnbutClick(Sender: TObject);
var
  EncryptedFileName, DecryptedFileName: string;
  Key: Word;
  FileStream: TFileStream;
  Buffer: TBytes;
begin
  FileStream := TFileStream.Create(Edit1.Text, fmOpenRead);
  if Edit1.Text = '' then
  begin
    ShowMessage('加密文件路径不能为空');

  end
  else if Edit4.Text = '' then
  begin
    ShowMessage('解密秘钥不能为空');

  end
  else if Edit5.Text = '' then
  begin
    ShowMessage('存放路径不能为空');

  end
  else
  begin
    try
    // 获取文件大小
      MaxBufferSize := FileStream.Size;

    // 根据文件大小调整缓冲区大小
      SetLength(Buffer, MaxBufferSize);

    // 读取文件内容到缓冲区
      FileStream.ReadBuffer(Buffer[0], MaxBufferSize);

    // 在这里可以对缓冲区进行处理或使用

    finally
      FileStream.Free;
    end;
    try
      EncryptFile(Edit1.Text, Edit5.Text + '\加密' + InputFileName, StrToInt(Edit4.Text));
      ShowMessage('文件加密成功');
    except
      ShowMessage('文件加密失败');
    end;
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
  OpenDlg: TOpenDialog;
  strFileName: string;
begin
  OpenDlg := TOpenDialog.Create(nil);
  try
    OpenDlg.Filter := '所有文件(*.*)|*.*';  // 将过滤器设置为空字符串,允许选择任意文件
    OpenDlg.Options := OpenDlg.Options + [ofPathMustExist, ofFileMustExist, ofHideReadOnly];
    if OpenDlg.Execute then
    begin
      strFileName := Trim(OpenDlg.FileName);
      if strFileName <> '' then
      begin
        InputFileName := ExtractFileName(OpenDlg.FileName);
        Edit1.Text := strFileName;

      end;
    end;
  finally
    FreeAndNil(OpenDlg);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  OpenDlg: TOpenDialog;
  strFileName: string;
begin
  OpenDlg := TOpenDialog.Create(nil);
  try
    OpenDlg.Filter := '所有文件(*.*)|*.*';  // 将过滤器设置为空字符串,允许选择任意文件
    OpenDlg.Options := OpenDlg.Options + [ofPathMustExist, ofFileMustExist, ofHideReadOnly];
    if OpenDlg.Execute then
    begin
      strFileName := Trim(OpenDlg.FileName);
      if strFileName <> '' then
      begin
        OutFileName := ExtractFileName(OpenDlg.FileName);
        Edit2.Text := strFileName;

      end;
    end;
  finally
    FreeAndNil(OpenDlg);
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  FileStream: TFileStream;
  Buffer: TBytes;
  MaxBufferSize: Integer;
begin
  if Edit2.Text = '' then
  begin
    ShowMessage('解密文件路径不能为空');

  end
  else if Edit3.Text = '' then
  begin
    ShowMessage('解密秘钥不能为空');

  end
  else if Edit5.Text = '' then
  begin
    ShowMessage('存放路径不能为空');

  end
  else
  begin
    FileStream := TFileStream.Create(Edit2.Text, fmOpenRead);
    try
    // 获取文件大小
      MaxBufferSize := FileStream.Size;

    // 根据文件大小调整缓冲区大小
      SetLength(Buffer, MaxBufferSize);

    // 读取文件内容到缓冲区
      FileStream.ReadBuffer(Buffer[0], MaxBufferSize);

    // 在这里可以对缓冲区进行处理或使用

    finally
      FileStream.Free;
    end;
    try
      DecryptFile(Edit2.Text, Edit5.Text + '\解密' + OutFileName, StrToInt(Edit3.Text));
      ShowMessage('文件解密成功');
    except
      ShowMessage('文件解密失败');
    end;
  end;

end;

procedure TForm1.Button4Click(Sender: TObject);
var
  OpenDlg: TFileOpenDialog;
  FolderPath: string;
begin
  OpenDlg := TFileOpenDialog.Create(nil);
  try
    OpenDlg.Options := [fdoPickFolders];
    if OpenDlg.Execute then
    begin
      FolderPath := OpenDlg.FileName;
      Edit5.Text := FolderPath;
    end;
  finally
    FreeAndNil(OpenDlg);
  end;
end;

end.

InputFileName 需要加密的文件路径
EncryptedFileName 加密完成以后得文件路径加文件名
DecryptedFileName 解密成功以后存放的文件路径
Key 秘钥
FileStream 计算加密需要的内存空间文件路径

在这个代码中,加密和解密的过程是一样的,因为它们使用了异或运算(xor),而异或运算是可逆的。在加密过程中,将每个字节与密钥的右移8位进行异或运算,得到加密后的字节,并将加密后的缓冲区写入输出文件。在解密过程中,同样对每个字节进行异或运算,使用相同的密钥进行解密,得到原始的字节,并将解密后的缓冲区写入输出文件。

需要注意的是,这种简单的异或运算并不是一种强大的加密算法,它只是一个基本的示例。在实际的加密应用中,我们需要使用更加复杂和安全的加密算法,如AES或RSA等。

文章来源:https://blog.csdn.net/qq_45083002/article/details/134859620
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。