delphi – 使用密码加密.INI文件字符串的简单代码

前端之家收集整理的这篇文章主要介绍了delphi – 使用密码加密.INI文件字符串的简单代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找比ROT13更复杂的东西,但是它不需要一个库(最好甚至不是一个单元,只是一个功能的下降).

我想用用户提供的密码对给定的字符串进行对称加密/解密.但是,结果必须是字符串,因为我必须能够将它存储在.INI文件中.

有没有人有这样做的简单功能(delphi XE2)?谷歌今天不是我的朋友

提前致谢

[更新] / [赏金]只是为了清楚(如果原来不是apologies),我不想要哈希.我有一个列表框,用户可以添加/修改/删除条目.当程序关闭并重新启动时,我想将它们存储在.INI文件中.任何查看.INI文件(例如,在记事本中打开它)的人都不应该读取这些字符串.

我想我可以将二进制文件流化为二进制文件,但是为了安心,我宁愿使用用户提供的密码对字符串进行加密.为了这个应用的目的,如果.INI文件名称或键值是可读的,我只想加密数据,给我一些列表存储在磁盘上的东西:

[config]
numEntries=3

[listBox]
0=ywevdyuvewfcyuw
1=edw
2=hr4uifareiuf

解决方法

这是替代Tinifile.
ReadString和WriteString被覆盖,这些内部用于Read / WriteFloat,Read / WriteInteger等.

字符串被加密并存储为HEX-Strings.

演示用法

uses CryptingIni;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
 ini:TCryptingIni;
begin
    ini:=TCryptingIni.Create('C:\temp\test.ini');
    ini.UseInternalVersion(1234);
    ini.WriteFloat('Sect','Float',123.456);
    ini.WriteString('Sect2','String','How to encode');
    ini.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
 ini:TCryptingIni;
begin
    ini:=TCryptingIni.Create('C:\temp\test.ini');
    ini.UseInternalVersion(1234);
    Showmessage(FloatToStr(ini.ReadFloat('Sect',0)));
    Showmessage(ini.ReadString('Sect2',''));
    Showmessage(ini.ReadString('SectUnkknow','Showdefault','DEFAULT'));
    ini.Free;
end;

您可以使用UseInternalVersion的内部加密方法,或提供自己的过程
过程SetCryptingData(aEncryptProc,aDecryptProc:CryptingProc; aKey:Word);

unit CryptingIni;

// 2013 by Thomas Wassermann
interface

uses
  Windows,SysUtils,Variants,Classes,inifiles;

type

  CryptingProc = Function(const InString: String; Key: Word): String;

  TCryptingIni = Class(TInifile)
    function ReadString(const Section,Ident,Default: string): string; override;
    procedure WriteString(const Section,Value: String); override;
  private
    FEncryptProc: CryptingProc;
    FDecryptProc: CryptingProc;
    FKey: Word;
  public
    Procedure SetCryptingData(aEncryptProc,aDecryptProc: CryptingProc; aKey: Word);
    Procedure UseInternalVersion(aKey: Word);
  End;

implementation

const
  c1 = 52845;
  c2 = 22719;

Type
  TByteArray = Array [0 .. 0] of byte;

Function AsHexString(p: Pointer; cnt: Integer): String;
var
  i: Integer;
begin
  Result := '';
  for i := 0 to cnt do
    Result := Result + '$' + IntToHex(TByteArray(p^)[i],2);
end;

Procedure MoveHexString2Dest(Dest: Pointer; Const HS: String);
var
  i: Integer;
begin
  i := 1;
  while i < Length(HS) do
  begin
    TByteArray(Dest^)[i div 3] := StrToInt(Copy(HS,i,3));
    i := i + 3;
  end;
end;

function EncryptV1(const s: string; Key: Word): string;
var
  i: smallint;
  ResultStr: string;
  UCS: WIDEString;
begin
  Result := s;
  if Length(s) > 0 then
  begin
    for i := 1 to (Length(s)) do
    begin
      Result[i] := Char(byte(s[i]) xor (Key shr 8));
      Key := (smallint(Result[i]) + Key) * c1 + c2
    end;
    UCS := Result;
    Result := AsHexString(@UCS[1],Length(UCS) * 2 - 1)
  end;
end;

function DecryptV1(const s: string; Key: Word): string;
var
  i: smallint;
  sb: String;
  UCS: WIDEString;
begin
  if Length(s) > 0 then
  begin
    SetLength(UCS,Length(s) div 3 div 2);
    MoveHexString2Dest(@UCS[1],s);
    sb := UCS;
    SetLength(Result,Length(sb));
    for i := 1 to (Length(sb)) do
    begin
      Result[i] := Char(byte(sb[i]) xor (Key shr 8));
      Key := (smallint(sb[i]) + Key) * c1 + c2
    end;
  end
  else
    Result := s;
end;

{ TCryptingIni }

function TCryptingIni.ReadString(const Section,Default: string): string;
begin
  if Assigned(FEncryptProc) then
    Result := inherited ReadString(Section,FEncryptProc(Default,FKey))
  else
    Result := inherited ReadString(Section,Default);
  if Assigned(FDecryptProc) then
    Result := FDecryptProc(Result,FKey);
end;

procedure TCryptingIni.SetCryptingData(aEncryptProc,aDecryptProc: CryptingProc; aKey: Word);
begin
  FEncryptProc := aEncryptProc;
  FDecryptProc := aDecryptProc;
  FKey := aKey;
end;

procedure TCryptingIni.UseInternalVersion(aKey: Word);
begin
  FKey := aKey;
  FEncryptProc := EncryptV1;
  FDecryptProc := DecryptV1;
end;

procedure TCryptingIni.WriteString(const Section,Value: String);
var
  s: String;
begin
  if Assigned(FEncryptProc) then
    s := FEncryptProc(Value,FKey)
  else
    s := Value;
  inherited WriteString(Section,s);
end;

end.
原文链接:https://www.f2er.com/delphi/101553.html

猜你在找的Delphi相关文章