title: Issues with AES Encryption using SynCrypto
body: <p>Am trying to encrypt a file using <a href="https://github.com/synopse/SynPDF/blob/master/SynCrypto.pas" rel="nofollow">SynCrypto.pas</a> with AES 256, but it fails if I try to encrypt a file whose size is not a multiple of 16 bytes. The decrypted data contains junk.</p>
  
  <p>Example:</p>
  
  <ul>
  <li><p>Original string in txt file</p>
  
  <blockquote>
    <p>we are testing the file</p>
  </blockquote></li>
  <li><p>Encrypted String</p>
  
  <blockquote>
    <p>[ù[„|wáî}f  *!4ìÙw¬•ü¨s</p>
  </blockquote></li>
  <li><p>Decrypted String</p>
  
  <blockquote>
    <p>we are testing tÝp?J</p>
  </blockquote></li>
  </ul>
  
  <p>Here is my Encryption Code</p>
  
  <pre><code>procedure TForm1.Button1Click(Sender: TObject);
  var
    A: TAES;
    Key: TSHA256Digest;
    s, B: TAESBlock;
    ks: integer;
    st: RawByteString;
    InStream, OutStream: TFileStream;
    SuperNo, TheSize, StreamSize: Int64;
  
  begin
    InStream := TFileStream.Create('test.txt', fmOpenRead);
    OutStream := TFileStream.Create('out.txt', fmCreate);
  
    InStream.Position := 0;
    OutStream.Position := 0;
    st := '1234essai';
    ks := 256;
    SHA256Weak(st, Key);
    A.EncryptInit(Key, ks);
    StreamSize := InStream.Size;
    while InStream.Position &lt; StreamSize do
    begin
      TheSize := StreamSize - InStream.Position;
      if TheSize &lt; 16 then
      begin
        SuperNo := StreamSize - InStream.Position;
  
      end
      else
      begin
        SuperNo := 16;
      end;
      InStream.ReadBuffer(s, SuperNo);
  
      A.Encrypt(s, B);
      OutStream.WriteBuffer(B, SuperNo);
    end;
    A.Done;
    ShowMessage('Finish');
    InStream.Free;
    OutStream.Free;
  end;
  </code></pre>
  
  <p>Here is my Decryption Code</p>
  
  <pre><code>procedure TForm1.Button2Click(Sender: TObject);
  var
    A: TAES;
    Key: TSHA256Digest;
    s, B: TAESBlock;
    ks: integer;
    st: RawByteString;
    InStream, OutStream: TFileStream;
    SuperNo, TheSize, StreamSize: Int64;
  
  begin
    InStream := TFileStream.Create('out.txt', fmOpenRead);
    OutStream := TFileStream.Create('in.txt', fmCreate);
  
    InStream.Position := 0;
    OutStream.Position := 0;
    st := '1234essai';
    ks := 256;
    SHA256Weak(st, Key);
    A.DecryptInit(Key, ks);
    StreamSize := InStream.Size;
    while InStream.Position &lt; StreamSize do
    begin
      TheSize := StreamSize - InStream.Position;
      if TheSize &lt; 16 then
      begin
        SuperNo := StreamSize - InStream.Position;
  
      end
      else
      begin
        SuperNo := 16;
      end;
      InStream.ReadBuffer(s, SuperNo);
  
      A.Decrypt(s, B);
  
      OutStream.WriteBuffer(B, SuperNo);
    end;
    A.Done;
    ShowMessage('Finish');
    InStream.Free;
    OutStream.Free;
  end;
  </code></pre>
  
  <p>Using Delphi XE7. </p>