title: Understanding encrypting a file, using LockBox 3 and Delphi XE2
body: <p>So my current task involves taking a given string of text (Ex: ABC123) and encrypting it using LockBox3's EncryptString(source, target) function.  I can successfully encrypt the string and get the output to save to a .txt file.   </p>
  
  <p>The next step in this process is to then use LockBox3's EncryptFile(source, target) function to take the .txt containing my already-encrypted string and encrypting said file using AES-128 (same as the string encryption but with diff password)</p>
  
  <p>Basically, I can get the string to encrypt correctly and output to a  .txt file.  I then request that the user grab the .txt, and bring it into my program.  The program then attempts to take that file and encrypt it further.  When I do this, I get a file to output.. however when I go to decrypt said file the resulting .txt does not contain the original text.. or any text for that matter.  I am basically confused as to how I should be going about encrypting the .txt file.  Any suggestions? I apologize if this question/code is not specific enough.  Please let me know what else, if anything I need to make clear about the situation in order to better help you guys understand what I'm struggling with!  Thanks!</p>
  
  <p>EDIT1:</p>
  
  <p>Alright everyone, thanks for the suggestions.  To clarify:</p>
  
  <p>The stream I have in the decryption process is to be used later, so that after I have decrypted the file, I can read from it and decrypt the remaining encrypted (from the first step) string.</p>
  
  <p>To clarify further: </p>
  
  <p>My codec (Codec1) for encrypting the string is using AES-128 with CBC, with a tag of "0" and an AsymetricKeySize of 1024 (which, Im pretty sure is irrelevant for this type of encryption correct?)   My codec for encrypting FILES (Codec2 above) has the same settings, however the passwords for Codec1 and Codec2 are different.   Basically, I am using Codec1 to encrypt a string and write it to a .txt, and then I am using Codec2 to encrypt said file.. eventually decrypt it and use the Stream to read from said file and decrypt that string using Codec1 again.</p>
  
  <p>my file encryption/decryption code:</p>
  
  <p><strong>String Encryption:</strong></p>
  
  <pre><code>procedure TForm1.Button1Click(Sender: TObject);
  begin
    codec1.Password := WORD_1;
    //Begin encryption
    sPlainText := Serial_number.Number;         //Store Serial Number of machine 
    codec1.EncryptString(sPlainText,CipherText);   //Encrypt (base64)
    listbox2.Clear;
    listbox2.AddItem(Ciphertext, AnsiCipher);
    end;
  </code></pre>
  
  <p><strong>Write encrypted string to a file and save it:</strong></p>
  
  <pre><code>saveDialog := TSaveDialog.Create(self);
    saveDialog.Title := 'Choose location to save Authentication Code';
    saveDialog.InitialDir := 'C:\';
    saveDialog.DefaultExt := '';
    saveDialog.FilterIndex := 1;
    saveDialog.Execute();
    glb_fileName1 := saveDialog.FileName;
   //open stream and write cipher to a .txt of chosen location
    try
      Stream := TFileStream.Create(saveDialog.GetNamePath + saveDialog.FileName + '.txt', fmOpenReadWrite);
    except
      Stream := TFileStream.Create(saveDialog.GetNamePath + saveDialog.FileName + '.txt', fmCreate);
    end;
      for k := 1 to (Length(CipherText)) do
          buff[k] := byte(CipherText[k]);
      ptr := @buff[1];
      Stream.WriteBuffer(ptr^, Length(CipherText));
    Stream.Free;
    saveDialog.Free;
  </code></pre>
  
  <p><strong>Grab location of .txt for file encryption:</strong></p>
  
  <pre><code>procedure TForm1.Button4Click(Sender: TObject);
  var
    fileName : string;
    holder_obj : TSerial_number;
  begin
    holder_obj := Tserial_number.Create;
    listbox4.Clear;
  
  if OpenTextFileDialog1.Execute() then
     fileName := OpenTextFileDialog1.FileName;     
     listbox4.AddItem(filename, holder_obj);
  end;
  </code></pre>
  
  <p><strong>File Encryption:</strong></p>
  
  <pre><code>  Codec2.Password := WORD_2;
    sCrypt := glb_fileName1 + '_enc.txt';
    Codec2.EncryptFile(glb_fileName1+'.txt', sCrypt);
  </code></pre>
  
  <p><strong>Grab Encrypted File for decryption:</strong></p>
  
  <pre><code>procedure TForm1.Button3Click(Sender: TObject);
  var
    holder_obj : TSerial_number;
  begin
    holder_obj := Tserial_number.Create;
    listbox3.Clear;
  if OpenTextFileDialog1.Execute() then
     glb_fileName2 := OpenTextFileDialog1.FileName;
     listbox3.AddItem(glb_filename2, holder_obj);
  end;
  </code></pre>
  
  <p><strong>File Decryption (opening a stream to read from the decrypted file once I have it, so that I can decrypt the encrypted string it contains):</strong></p>
  
  <pre><code>procedure TForm1.Button5Click(Sender: TObject);
  var
    saveDialog : TSaveDialog;
  begin
    saveDialog := TSaveDialog.Create(self);
    saveDialog.Title := 'Choose location to save Decrypted Authentication Code';
    saveDialog.InitialDir := 'C:\';
    saveDialog.DefaultExt := '';
    saveDialog.Execute();
    glb_fileName1:= saveDialog.FileName;
   //open stream and write cipher to a .txt of chosen location
    try
      Stream := TFileStream.Create(saveDialog.GetNamePath + saveDialog.FileName + '.txt', fmOpenReadWrite);
    except
      Stream := TFileStream.Create(saveDialog.GetNamePath + saveDialog.FileName + '.txt', fmCreate);
    end;
    Stream.Free;
  
    Codec2.Password := WORD_2;
    Codec2.DecryptFile(glb_fileName2, saveDialog.FileName + '.txt');
    saveDialog.Free;
  end;
  </code></pre>