lockbox-3

DCPCrypt to Lockbox3 migration


I'm a newbie on encrypting/decrypting routines. I'm trying to use Lockbox3, which I want migrate my app to, in order to decrypt a string encrypted by using DCPCrypt. Let's say I have this function to encrypt:

function TfrmMain.Encrypt(value: string): string;
var
  CipherR : TDCP_rijndael;
  m, sm, Key, IV: string;
  Data: string;
begin
  Key := PadWithZeros(m, KeySize);
  IV := PadWithZeros(sm, BlockSize);
  Data := PadWithZeros(value, BlockSize);
  m := 'SOMEWORDSOMEWORD';
  sm := 'WORDSOMEWORDSOME';

  Key := PadWithZeros(m, KeySize);
  IV := PadWithZeros(sm, BlockSize);
  CipherR := TDCP_rijndael.Create(Self);
  if Length(m) <= 16 then
    CipherR.Init(Key[1], 128, @IV[1])
  else if Length(m) <= 24 then
    CipherR.Init(Key[1], 192, @IV[1])
  else
    CipherR.Init(Key[1], 256, @IV[1]);
  CipherR.EncryptCBC(Data[1], Data[1], Length(Data));
  CipherR.Free;
  FillChar(Key[1], Length(Key), 0);
  code := Base64EncodeStr(Data);
  Result := code;
end;

I would like now to decrypt strings encrypted this way using Lockbox3, but I should use the values used in encrypt function as m and sm and I know how I can do it - if I can. I thought to use the sm value to set Codec1.Password but this doesn't work.

Any idea? Thanks to all for any advice.


Solution

  • How about ...

    function TfrmMain.Encrypt( const value: string): string;
    var
      Codec: TCodec;
      Lib  : TCyptographicLibrary;
      Ciphertext: ansistring;
    begin
    Codec := TCodec.Create( nil);
    Lib   := TCyptographicLibrary.Create( nil);
    Codec.CryptoLibrary := Lib;
    Codec.StreamCipherId := BlockCipher_ProgId;
    Codec.BlockCipherId := 'native.AES-256'; // AES-256 cipher
    Codec.ChainModeId := CBC_ProgId;         // CBC chaining
    Codec.Password := 'WORDSOMEWORDSOME';
    Codec.EncryptString( Value, Ciphertext); // Assumes UNICODE supporting compiler.
    result := Ciphertext; // Implicit utf8string --> unicode string coersion here.
    Codec.Burn;
    Lib.Free;
    Codec.Free;
    end;
    
    function TfrmMain.ShowCiphertextOfSomeWords;
    begin
    ShowMessage( 'base64 of ciphertext = ' + Encrypt( 'SOMEWORDSOMEWORD'))
    end;
    

    All this is shown in the bundled Demo program and online help. The bundled demo program and the online help should be your first point of reference. Please examine these before going to Stackoverflow or forums, as answerers will tend to repeat what was already written for your benefit.