title: Encrypt and decrypt a Unicode string
body: <p>Can someone give me the code to encrypt and decrypt a Unicode strings in delphi firemonkey Mobile?
  I've tried everything with xor with other libraries , and nothing.
  There are always characters that are not recognized as the euro symbol € .
  If someone could help me , would be appreciated.</p>
  
  <p>Edit:
  Thank you Hans, but always I have the same problem with stringstream . This code works perfectly in windows , but ios gives me this error : "No mapping for the Unicode character exists in the target multibyte code page"</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>I've also tried to create stringstream with this:</p>
  
  <pre><code>Source := TStringStream.Create(Trim(Memo1.Text) , TEncoding.Unicode) ;
  </code></pre>
  
  <p>and sometimes works well and sometimes gives me the following error:"Los surrogate char without a preceding high surrogate char at index: 8. chaeck that the string is encoded properly.
  Any ideas?</p>