Delphi DEC library (Rijndael) encryption

前端 未结 3 679
猫巷女王i
猫巷女王i 2021-02-09 07:01

I am trying to use the DEC 3.0 library (Delphi Encryption Compedium Part I) to encrypt data in Delphi 7 and send it to a PHP script through POST, where I am dec

3条回答
  •  故里飘歌
    2021-02-09 07:32

    A 256 bit key I found is 32 charachters, or 32 bytes. Not 24. This may be the issue.

    [EDIT]

    I combined everyone's ideas (ansistring, etc) into one single idea with a fix.

    Also, you are using codestring( -- it should be Encodestring(

    I pasted working Encrypt and Decrypt source below:


    function EncryptMsgData(MsgData, Key: AnsiString): AnsiString;
    var RCipher: TCipher_Rijndael;
    begin
      RCipher:= TCipher_Rijndael.Create('', nil);
      RCipher.Init(Pointer(Key)^,Length(Key),nil);
      RCipher.Mode:= cmCBC;
      Result:= RCipher.EncodeString(MsgData);
      RCipher.Free;
    end;
    
    function DecryptMsgData(MsgData, Key: AnsiString): AnsiString;
    var RCipher: TCipher_Rijndael;
    begin
      RCipher:= TCipher_Rijndael.Create('',nil);
      RCipher.Init(Pointer(Key)^,Length(Key),nil);
      RCipher.Mode:= cmCBC;
      Result:= RCipher.DecodeString(MsgData);
      RCipher.Free;
    end;
    

    Use that with a 32 charachter key and you get proper encryption and decryption.

    In order to store and use the encrypted data as a string you may want to use Base64Encode(

    But do not forget to Base64Decode prior to decrypting.

    This is the same technique needed for Blowfish. Sometimes the charachters actually are like a backspace, and perform the function rather than showing on screen. Base64Encode basically converts the charachters to something you can display in text.

    Prior to transferring the encoded data across the internet or to another application in the same or another language, you MUST base64encode and decode in order to not loose data. Don't forget it in PHP too!

提交回复
热议问题