Search code examples
delphicryptographyrijndael

decrypting using rijndael. DCPcrypt library. Delphi


I have a IV (initialization vector) and key, also a cryptogram. I need do decrypt the cryptogram. From the internet i found DCPcrypt Cryptographic Component Library v2. So, now i've reached to coding.

procedure TForm1.Button1Click(Sender: TObject);
var
key:Ansistring;
ivector,indata,outdata:string;
begin
  key := 'abc12345679';  //<--key for decrypting
  dcp_rijndael1.InitStr(key,TDCP_sha1);  //I don't understand why i need hashing!?

  ivector := edit2.Text;  //initialization vector
  dcp_rijndael1.SetIV(ivector);
  dcp_rijndael1.BlockSize := Length(ivector); //'This variable should be the same size as the block size' says the documentation

  indata := edit1.Text;  //getting the cryptogram

  dcp_rijndael1.CipherMode := cmCBC;

  dcp_rijndael1.DecryptCBC(indata,outdata,Length(indata));
  label3.Caption := outdata;                                    //output to label
end;

This code gives me an error. "Local Variables" window shows indata, outdata, ivector, key variables as 'Inaccessible value'. Or maybe is there another way to do it. This seems pretty straight forward, though. Thanks in advance.

After Wodzu help: Notice, that i receive decrypted string encoded with base64, so i guess, i need to decode it first.

var
  Form1: TForm1;
  StringToEncrypt, StringToDecrypt, DecryptedString: string;
  vector:string;

procedure TForm1.Button2Click(Sender: TObject);
begin
  vector := '1234567812345678';        //Length 16
  stringtodecrypt := '2YOXZ20Z7B3TRI/Ut8iH/GpEZWboE2tnnWU';
  stringtodecrypt := Decode64(stringtodecrypt);  //after encrypted string is sent over internet, it is encoded with base64, so i need to decode it.
  SetLength(DecryptedString, 36);  //36 is the length of the output
  DCP_rijndael1.Init('MyKey:128bit', 128, @Vector[1]);
  DCP_rijndael1.SetIV(Vector);
  DCP_rijndael1.BlockSize := Length(Vector); //Should this be also 128
  DCP_rijndael1.DecryptCBC(StringToDecrypt[1], DecryptedString[1], Length(StringToDecrypt)*2);  //Here i get hieroglyph as a result. Why is length multiplied with 2?
  decryptedstring := Encode64(decryptedstring);  //Here i get less hieroglyph, but would like to get correct decrypted string. I doubt the necessity of encoding

  ShowMessage(DecryptedString);

end;

I can't make this code to decrypt data that somebody else is encrypting (with PHP) (after encrypting the data is encoded with base64). Note! encrypted text length is not the same as the decrypted text length!


Solution

  • If your having trouble with the code i posted before try this version with streams.

    procedure TForm1.Decrypt(const aKey: AnsiString; aPVector: Pointer;
      var aInData, aOutData: TMemoryStream);
    var
      Cipher : TDCP_rijndael;
    begin
      Cipher := TDCP_rijndael.Create(nil);
      try
        Cipher.Init(aKey, Length(aKey)*8, aPVector);
        Cipher.CipherMode := cmCBC;
        Cipher.DecryptStream(aInData, aOutData, aInData.Size);
      finally
        Cipher.Burn;
        Cipher.Free;
      end;
    end;
    

    and here is how to use it:

    var
      din, dout: TMemoryStream;
      Vector: array of byte;
    begin
    
    
    SetLength(Vector, 16);
    
    Vector[1] := 1;
    Vector[2] := 2;
    Vector[3] := 9;
    Vector[4] := 0;
    Vector[5] := 6;
    Vector[6] := 1;
    Vector[7] := 6;
    Vector[8] := 7;
    Vector[9] := 5;
    Vector[10] := 8;
    Vector[11] := 3;
    Vector[12] := 1;
    Vector[13] := 7;
    Vector[14] := 3;
    Vector[15] := 3;
    Vector[16] := 8;
    
    din := TMemoryStream.Create;
    dout := TMemoryStream.Create;
    try
      din.LoadFromFile('Encrypted.DAT');
      din.Position := 0;
      decrypt('4tkF4tGN1KSiwc4E', addr(Vector[1]), din, dout);
      dout.SaveToFile('Decrypted.DAT');
    finally
      din.Free;
      dout.Free;
    end;
    

    and a version for strings:

    procedure TForm1.Decrypt(const aKey: AnsiString; aPVector: Pointer;
       const aInData: AnsiString; var aOutData: AnsiString);
    var
      Cipher : TDCP_rijndael;
    begin
      Cipher := TDCP_rijndael.Create(nil);
      try
        Cipher.Init(aKey, Length(aKey)*8, aPVector);
        Cipher.CipherMode := cmCBC;
        aOutData := Cipher.DecryptString(aInData);
      finally
        Cipher.Burn;
        Cipher.Free;
      end;
    end;
    

    if you need any more help let me know.