delphi 异或运算 加密办法

go 复制代码
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等。

相关推荐
艾莉丝努力练剑29 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
倔强青铜35 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上5 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang6 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc6 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
时光追逐者6 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 46 期(2025年7.7-7.13)
c#·.net·.netcore
AI视觉网奇6 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀6 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc