<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How to correctly handle cryptography in ANSI and Unicode flavor</title>
	<atom:link href="http://www.cromis.net/blog/2009/11/how-to-correctly-handle-cryptography-in-ansi-and-unicode-flavor/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cromis.net/blog/2009/11/how-to-correctly-handle-cryptography-in-ansi-and-unicode-flavor/</link>
	<description>A blog about Delphi programming and all things technical</description>
	<lastBuildDate>Tue, 31 Jan 2012 09:32:12 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
	<item>
		<title>By: Outlook Express Contacts to MS Outlook - Free Software, Shareware Downloads and More</title>
		<link>http://www.cromis.net/blog/2009/11/how-to-correctly-handle-cryptography-in-ansi-and-unicode-flavor/comment-page-1/#comment-285</link>
		<dc:creator>Outlook Express Contacts to MS Outlook - Free Software, Shareware Downloads and More</dc:creator>
		<pubDate>Sun, 14 Feb 2010 09:28:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.cromis.net/blog/?p=471#comment-285</guid>
		<description>[...] How to rightly hoop cryptography in ANSI as good as Unicode flavor [...]</description>
		<content:encoded><![CDATA[<p>[...] How to rightly hoop cryptography in ANSI as good as Unicode flavor [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xiaoyi</title>
		<link>http://www.cromis.net/blog/2009/11/how-to-correctly-handle-cryptography-in-ansi-and-unicode-flavor/comment-page-1/#comment-257</link>
		<dc:creator>Xiaoyi</dc:creator>
		<pubDate>Sat, 16 Jan 2010 03:46:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.cromis.net/blog/?p=471#comment-257</guid>
		<description>oh, it really works now. Very thanks for your help.</description>
		<content:encoded><![CDATA[<p>oh, it really works now. Very thanks for your help.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Iztok Kacin</title>
		<link>http://www.cromis.net/blog/2009/11/how-to-correctly-handle-cryptography-in-ansi-and-unicode-flavor/comment-page-1/#comment-256</link>
		<dc:creator>Iztok Kacin</dc:creator>
		<pubDate>Fri, 15 Jan 2010 20:02:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.cromis.net/blog/?p=471#comment-256</guid>
		<description>You had several problems in your code. This is the corrected version

&lt;pre lang=&quot;delphi&quot;&gt;
procedure TForm1.Button1Click(Sender: TObject);
var
  AStream : TFileStream;
  Len: WORD;
  Key: TBytes;
  InBuffer: TBytes;
  OutBuffer: TBytes;
begin
  AStream := TFileStream.Create(&#039;c:\test.dat&#039;, fmCreate);
  AStream.Size := 0;
  AStream.Position := 0;
  try
    InBuffer := GetBytesFromUnicodeString(Memo1.Text);
    Key := GetBytesFromUnicodeString(&#039;Secret Password&#039;);
    OutBuffer := XTeaEncryptBytes(InBuffer, Key);
    Len := Length(OutBuffer);
    AStream.Write(Len, SizeOf(WORD));
    AStream.Write(OutBuffer[0], Len);
  finally
    AStream.Free;
  end
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  AStream : TFileStream;
  Len : WORD;
  Key: TBytes;
  InBuffer: TBytes;
  OutBuffer: TBytes;
begin
  AStream := TFileStream.Create(&#039;c:\test.dat&#039;, fmOpenRead);
  try
    AStream.Position := 0;
    AStream.Read(Len, SizeOf(WORD));
    Key := GetBytesFromUnicodeString(&#039;Secret Password&#039;);

    if Len &gt; 0 then
    begin
      SetLength(InBuffer, Len);
      AStream.Read(InBuffer[0], Len);
      OutBuffer := XTeaDecryptBytes(InBuffer, Key);
      Memo1.Text := GetUnicodeString(OutBuffer);
    end;
  finally
    AStream.Free;
  end;
end;
&lt;/pre&gt;

Problems:

1. You didn&#039;t actually encrypt or decrypt anything. GetBytesFromUnicodeString just returns bytes from a string because all crypto function work only on bytes.
2. When passing buffers to stream as untyped parameters pass the first element of the buffer and not the variable. The corrected code now works.</description>
		<content:encoded><![CDATA[<p>You had several problems in your code. This is the corrected version</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">procedure</span> TForm1<span style="color: #000066;">.</span><span style="color: #006600;">Button1Click</span><span style="color: #000066;">&#40;</span>Sender<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">TObject</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  AStream <span style="color: #000066;">:</span> TFileStream<span style="color: #000066;">;</span>
  Len<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">WORD</span><span style="color: #000066;">;</span>
  Key<span style="color: #000066;">:</span> TBytes<span style="color: #000066;">;</span>
  InBuffer<span style="color: #000066;">:</span> TBytes<span style="color: #000066;">;</span>
  OutBuffer<span style="color: #000066;">:</span> TBytes<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  AStream <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TFileStream<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'c:\test.dat'</span><span style="color: #000066;">,</span> fmCreate<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  AStream<span style="color: #000066;">.</span><span style="color: #006600;">Size</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">;</span>
  AStream<span style="color: #000066;">.</span><span style="color: #006600;">Position</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">try</span>
    InBuffer <span style="color: #000066;">:</span><span style="color: #000066;">=</span> GetBytesFromUnicodeString<span style="color: #000066;">&#40;</span>Memo1<span style="color: #000066;">.</span><span style="color: #006600;">Text</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    Key <span style="color: #000066;">:</span><span style="color: #000066;">=</span> GetBytesFromUnicodeString<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Secret Password'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    OutBuffer <span style="color: #000066;">:</span><span style="color: #000066;">=</span> XTeaEncryptBytes<span style="color: #000066;">&#40;</span>InBuffer<span style="color: #000066;">,</span> Key<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    Len <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #000066;">Length</span><span style="color: #000066;">&#40;</span>OutBuffer<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    AStream<span style="color: #000066;">.</span><span style="color: #000066;">Write</span><span style="color: #000066;">&#40;</span>Len<span style="color: #000066;">,</span> <span style="color: #000066;">SizeOf</span><span style="color: #000066;">&#40;</span><span style="color: #000066; font-weight: bold;">WORD</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    AStream<span style="color: #000066;">.</span><span style="color: #000066;">Write</span><span style="color: #000066;">&#40;</span>OutBuffer<span style="color: #000066;">&#91;</span><span style="color: #0000ff;">0</span><span style="color: #000066;">&#93;</span><span style="color: #000066;">,</span> Len<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">finally</span>
    AStream<span style="color: #000066;">.</span><span style="color: #006600;">Free</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TForm1<span style="color: #000066;">.</span><span style="color: #006600;">Button2Click</span><span style="color: #000066;">&#40;</span>Sender<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">TObject</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  AStream <span style="color: #000066;">:</span> TFileStream<span style="color: #000066;">;</span>
  Len <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">WORD</span><span style="color: #000066;">;</span>
  Key<span style="color: #000066;">:</span> TBytes<span style="color: #000066;">;</span>
  InBuffer<span style="color: #000066;">:</span> TBytes<span style="color: #000066;">;</span>
  OutBuffer<span style="color: #000066;">:</span> TBytes<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  AStream <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TFileStream<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'c:\test.dat'</span><span style="color: #000066;">,</span> fmOpenRead<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">try</span>
    AStream<span style="color: #000066;">.</span><span style="color: #006600;">Position</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">;</span>
    AStream<span style="color: #000066;">.</span><span style="color: #000066;">Read</span><span style="color: #000066;">&#40;</span>Len<span style="color: #000066;">,</span> <span style="color: #000066;">SizeOf</span><span style="color: #000066;">&#40;</span><span style="color: #000066; font-weight: bold;">WORD</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    Key <span style="color: #000066;">:</span><span style="color: #000066;">=</span> GetBytesFromUnicodeString<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Secret Password'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> Len &gt; <span style="color: #0000ff;">0</span> <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #000000; font-weight: bold;">begin</span>
      <span style="color: #000066;">SetLength</span><span style="color: #000066;">&#40;</span>InBuffer<span style="color: #000066;">,</span> Len<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
      AStream<span style="color: #000066;">.</span><span style="color: #000066;">Read</span><span style="color: #000066;">&#40;</span>InBuffer<span style="color: #000066;">&#91;</span><span style="color: #0000ff;">0</span><span style="color: #000066;">&#93;</span><span style="color: #000066;">,</span> Len<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
      OutBuffer <span style="color: #000066;">:</span><span style="color: #000066;">=</span> XTeaDecryptBytes<span style="color: #000066;">&#40;</span>InBuffer<span style="color: #000066;">,</span> Key<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
      Memo1<span style="color: #000066;">.</span><span style="color: #006600;">Text</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> GetUnicodeString<span style="color: #000066;">&#40;</span>OutBuffer<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">finally</span>
    AStream<span style="color: #000066;">.</span><span style="color: #006600;">Free</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>Problems:</p>
<p>1. You didn&#8217;t actually encrypt or decrypt anything. GetBytesFromUnicodeString just returns bytes from a string because all crypto function work only on bytes.<br />
2. When passing buffers to stream as untyped parameters pass the first element of the buffer and not the variable. The corrected code now works.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xiaoyi</title>
		<link>http://www.cromis.net/blog/2009/11/how-to-correctly-handle-cryptography-in-ansi-and-unicode-flavor/comment-page-1/#comment-255</link>
		<dc:creator>Xiaoyi</dc:creator>
		<pubDate>Fri, 15 Jan 2010 19:18:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.cromis.net/blog/?p=471#comment-255</guid>
		<description>I wrote a simple program with your function:
  function GetUnicodeString(const Value: TBytes): TTeaUnicodeString;
  function GetAnsiString(const Value: TBytes): TTeaAnsiString;

But the program cannot load the text that saved before properly. I traced the program and i think it maybe a bug of D2010(maybe related with reference count of dynamic array). I have Update4/5 installed. May you have a look at my program:

&lt;pre lang=&quot;delphi&quot;&gt;
procedure TForm1.Button1Click(Sender: TObject);
var
  AStream : TFileStream;
  Len : WORD;
  Buffer : TBytes;
begin
  AStream := TFileStream.Create(&#039;c:\1.1&#039;, fmCreate);
  AStream.Size := 0;
  AStream.Position := 0;
  try
    Buffer := GetBytesFromUnicodeString(Memo1.Lines[0]);
    Len := Length(Buffer);
    AStream.Write(Len, sizeof(Len));
    AStream.Write(Buffer, Len);
  finally
    AStream.Free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  AStream : TFileStream;
  Len : WORD;
  Buffer : TBytes;
begin
  AStream := TFileStream.Create(&#039;c:\1.1&#039;, fmOpenRead);
  try
    AStream.Position := 0;
    AStream.Read(Len, sizeof(Len));
    if Len &gt; 0 then
    begin
      SetLength(Buffer, Len);
      AStream.Read(Buffer, Len);
      SetLength(Buffer, Len);
      Memo1.Text := GetUnicodeString(Buffer);
    end;
  finally
    AStream.Free;
  end;
end;
&lt;/pre&gt;

Step 1: save
Step 2: close program
Step 3: run the program again and load, the content is gone</description>
		<content:encoded><![CDATA[<p>I wrote a simple program with your function:<br />
  function GetUnicodeString(const Value: TBytes): TTeaUnicodeString;<br />
  function GetAnsiString(const Value: TBytes): TTeaAnsiString;</p>
<p>But the program cannot load the text that saved before properly. I traced the program and i think it maybe a bug of D2010(maybe related with reference count of dynamic array). I have Update4/5 installed. May you have a look at my program:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">procedure</span> TForm1<span style="color: #000066;">.</span><span style="color: #006600;">Button1Click</span><span style="color: #000066;">&#40;</span>Sender<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">TObject</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  AStream <span style="color: #000066;">:</span> TFileStream<span style="color: #000066;">;</span>
  Len <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">WORD</span><span style="color: #000066;">;</span>
  Buffer <span style="color: #000066;">:</span> TBytes<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  AStream <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TFileStream<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'c:\1.1'</span><span style="color: #000066;">,</span> fmCreate<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  AStream<span style="color: #000066;">.</span><span style="color: #006600;">Size</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">;</span>
  AStream<span style="color: #000066;">.</span><span style="color: #006600;">Position</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">try</span>
    Buffer <span style="color: #000066;">:</span><span style="color: #000066;">=</span> GetBytesFromUnicodeString<span style="color: #000066;">&#40;</span>Memo1<span style="color: #000066;">.</span><span style="color: #006600;">Lines</span><span style="color: #000066;">&#91;</span><span style="color: #0000ff;">0</span><span style="color: #000066;">&#93;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    Len <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #000066;">Length</span><span style="color: #000066;">&#40;</span>Buffer<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    AStream<span style="color: #000066;">.</span><span style="color: #000066;">Write</span><span style="color: #000066;">&#40;</span>Len<span style="color: #000066;">,</span> <span style="color: #000066;">sizeof</span><span style="color: #000066;">&#40;</span>Len<span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    AStream<span style="color: #000066;">.</span><span style="color: #000066;">Write</span><span style="color: #000066;">&#40;</span>Buffer<span style="color: #000066;">,</span> Len<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">finally</span>
    AStream<span style="color: #000066;">.</span><span style="color: #006600;">Free</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TForm1<span style="color: #000066;">.</span><span style="color: #006600;">Button2Click</span><span style="color: #000066;">&#40;</span>Sender<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">TObject</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  AStream <span style="color: #000066;">:</span> TFileStream<span style="color: #000066;">;</span>
  Len <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">WORD</span><span style="color: #000066;">;</span>
  Buffer <span style="color: #000066;">:</span> TBytes<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  AStream <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TFileStream<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'c:\1.1'</span><span style="color: #000066;">,</span> fmOpenRead<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">try</span>
    AStream<span style="color: #000066;">.</span><span style="color: #006600;">Position</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">;</span>
    AStream<span style="color: #000066;">.</span><span style="color: #000066;">Read</span><span style="color: #000066;">&#40;</span>Len<span style="color: #000066;">,</span> <span style="color: #000066;">sizeof</span><span style="color: #000066;">&#40;</span>Len<span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> Len &amp;gt<span style="color: #000066;">;</span> <span style="color: #0000ff;">0</span> <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #000000; font-weight: bold;">begin</span>
      <span style="color: #000066;">SetLength</span><span style="color: #000066;">&#40;</span>Buffer<span style="color: #000066;">,</span> Len<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
      AStream<span style="color: #000066;">.</span><span style="color: #000066;">Read</span><span style="color: #000066;">&#40;</span>Buffer<span style="color: #000066;">,</span> Len<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
      <span style="color: #000066;">SetLength</span><span style="color: #000066;">&#40;</span>Buffer<span style="color: #000066;">,</span> Len<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
      Memo1<span style="color: #000066;">.</span><span style="color: #006600;">Text</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> GetUnicodeString<span style="color: #000066;">&#40;</span>Buffer<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">finally</span>
    AStream<span style="color: #000066;">.</span><span style="color: #006600;">Free</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>Step 1: save<br />
Step 2: close program<br />
Step 3: run the program again and load, the content is gone</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
