加密和解密Unicode字符串

时间:2014-10-13 08:19:41

标签: delphi encryption cryptography firemonkey delphi-xe7

有人可以给我加密和解密delphi firemonkey Mobile中的Unicode字符串的代码吗? 我已经尝试了xor与其他库的一切,什么都没有。 总有一些字符无法被识别为欧元符号€。 如果有人可以帮助我,我将不胜感激。

编辑: 谢谢汉斯,但我总是遇到与stringstream相同的问题。这段代码在windows中完美运行,但是ios给了我这个错误:"目标多字节代码页中没有Unicode字符的映射"

unit UMain;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, ElAES,
  FMX.StdCtrls, FMX.Layouts, FMX.Memo, Math;

type
  TForm2 = class(TForm)
    ToolBar1: TToolBar;
    Label1: TLabel;
    Label2: TLabel;
    Memo1: TMemo;
    Layout1: TLayout;
    Button1: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
const
  PASSWORD = '1234';
var
  Form2: TForm2;

implementation

{$R *.fmx}
{$R *.iPhone.fmx IOS}
function StringToHex(S: string): string;
var
    i: integer;

begin
  Result := '';

  // Go throught every single characters, and convert them
  // to hexadecimal...
    for i := 1 to Length( S ) do
    Result := Result + IntToHex( Ord( S[i] ), 2 );
end;

function HexToString(S: string): string;
var
    i: integer;

begin
  Result := '';

  // Go throught every single hexadecimal characters, and convert
  // them to ASCII characters...
  for i := 1 to Length( S ) do
  begin
    // Only process chunk of 2 digit Hexadecimal...
    if ((i mod 2) = 1) then
        Result := Result + Chr( StrToInt( '0x' + Copy( S, i, 2 )));
  end;
end;


procedure TForm2.Button1Click(Sender: TObject);
var
  Source: TStringStream;
  Dest: TStringStream;
  Key: TAESKey128;
begin
try

  Source := TStringStream.Create( Memo1.Text );
  Dest   := TStringStream.Create('');
  FillChar( Key, SizeOf(Key), 0 );
  Move( PChar(PASSWORD)^, Key, Min( SizeOf( Key ), Length( PASSWORD )));
  EncryptAESStreamECB( Source, 0, Key, Dest );
  //Memo1.Lines.BeginUpdate;
  Memo1.Text := Dest.DataString;
  //Memo1.Lines.EndUpdate;
  Label2.Text := 'Texto Encriptado';
  Source.Free;
  Dest.Free;
except on E: Exception do
  begin
    ShowMessage(e.ToString);
    Source.Free;
    Dest.Free;
    Memo1.Text :='';
  end;
end;

end;


procedure TForm2.Button3Click(Sender: TObject);
var
  Source: TStringStream;
  Dest: TStringStream;
  Key: TAESKey128;
  Size: integer;
begin
try
  Source := TStringStream.Create(Trim(Memo1.Text) );
  Dest   := TStringStream.Create('');
  Size := Source.Size;
  Source.ReadBuffer(Size, SizeOf(Size));
  FillChar(Key, SizeOf(Key), 0);
  Move(PChar(PASSWORD)^, Key, Min(SizeOf(Key), Length(PASSWORD)));
  Source.Position := 0;
  DecryptAESStreamECB(Source, Source.Size - Source.Position, Key, Dest);
  Memo1.Text := Trim(Dest.DataString);
  Label2.Text := 'Texto Original';
  Source.Free;
  Dest.Free;
except on E: Exception do
  begin
    ShowMessage(e.ToString);
    Source.Free;
    Dest.Free;
    Memo1.Text :='';
  end;
end;

end;

end.

我还试图用这个创建stringstream:

Source := TStringStream.Create(Trim(Memo1.Text) , TEncoding.Unicode) ;

有时效果很好,有时会给我以下错误:" Los surrogate char没有前面的高代理字符索引:8。查看字符串是否正确编码。 有什么想法吗?

1 个答案:

答案 0 :(得分:4)

使用标准化库而不是尝试制作自己的加密解决方案。例如,有几种可用于Delphi的AES加密实现(例如Eldos which is included in the NativeXML library)。 将您的字符串(MyString)写入流并加密它:

var
  lSourceStream: TStringStream;
  lDestinationStream: TMemoryStream;
begin
  lSourceStream := TStringStream.Create(MyString);
  lDestinationStream := TMemoryStream.Create;
  AESencrypt(lSourceStream,lDestinationStream);
  lDestinationStream.SaveToFile(<filename>);
end;
相关问题