[Lazarus] Encryption compatible with .net

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Sun Nov 15 11:16:25 CET 2015


On 2015-11-15 05:30, Gabriele Cappelletto wrote:
> what's the code to decrypt it with DCPCrypt?


Here is one usage example... This is actually the DES3 self-test code

===========================
procedure test;
const
  Key: array[0..23] of byte =
    ($01,$23,$45,$67,$89,$ab,$cd,$ef,$fe,$dc,$ba,$98,
     $76,$54,$32,$10,$89,$ab,$cd,$ef,$01,$23,$45,$67);    // whatever
your key is
  PlainText: array[0..7] of byte=
    ($01,$23,$45,$67,$89,$ab,$cd,$e7);
  CipherText: array[0..7] of byte=
    ($de,$0b,$7c,$06,$ae,$5e,$0e,$d5);
var
  Cipher: TDCP_3des;
  Block: array[0..7] of byte;
begin
  dcpFillChar(Block, SizeOf(Block), 0); // initialize block to 0's
  Cipher:= TDCP_3des.Create(nil);
  Cipher.Init(Key,Sizeof(Key)*8,nil);
  Cipher.EncryptECB(PlainText,Block); // encrypt to Block
  Result:= CompareMem(@Block, at CipherText,Sizeof(CipherText));
  Cipher.DecryptECB(Block,Block); // decrypt the data in Block
  Result:= Result and CompareMem(@Block, at PlainText,Sizeof(PlainText));
  Cipher.Free;
end;

===========================


Here is another example where DCPCrypt is used to decrypt a file. It
used a different cipher and hash, but the usage would be the same for
DES or DES3.


==========================
procedure TDecryptDCPCrypt.DecryptFile(const pSrcFile, pDestFile: string);
var
  SrcFileStream, DestFileStream: TFileStream;
  DestPath: string;
  lSrc, lDest: string;
begin
  lSrc := tiFixPathDelim(pSrcFile);
  lDest := tiFixPathDelim(pDestFile);

  if FileExists(lSrc) then
  begin
    SrcFileStream  := TFileStream.Create(lSrc, fmOpenRead);
    DestFileStream := TFileStream.Create(lDest, fmCreate);

    try
      DestPath := ExtractFilePath(lDest);
      if not DirectoryExists(DestPath) then
        CreateDir(DestPath);

      try
        FCipher.InitStr(cDecryptKey, TDCP_haval);
        FCipher.DecryptStream(SrcFileStream, DestFileStream,
LongWord(SrcFileStream.Size));
        FCipher.Burn;
      except
        on E: Exception do
        begin
          Log(E.Message, lsError);
          raise ESlideDecryptionFailed.Create('Failed to decrypt slide <'
              + lSrc + '>' + tiLineEnd +  'Error: ' + E.Message);
        end;
      end;  { try.. except }

    finally
      SrcFileStream.Free;
      DestFileStream.Free;
    end;  { try..finally }
  end  { if }
  else
    Log('pSrcFile does NOT exist!', lsError);
end;
==========================

Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp




More information about the Lazarus mailing list