How can I decrypt an encrypted MCRYPT_RIJNDAEL_256 value in C#, that was encrypted by mcrypt in PHP?

前端 未结 3 1489
不知归路
不知归路 2021-02-19 01:15

I am trying to read a Base64-Encoded value from a Database table managed on the Linux side. In that table there is a column called first_name. On the Linux side I can decrypt t

3条回答
  •  醉话见心
    2021-02-19 01:48

    I hit the same problem when comunicating with a legacy system. I had to come up with my own solution as Rijndael with a Blocksize of 256 seems to be not supported by dotnet core.

    Here is my solution using the bouncy castle library:

        public static byte[] Rijandael256Decrypt(byte[] inputBytes, byte[] keyBytes)
        {
            // set up
            IBufferedCipher cipher = new PaddedBufferedBlockCipher(new RijndaelEngine(256), new ZeroBytePadding());
            KeyParameter keyParam = new KeyParameter(keyBytes);
            cipher.Init(false, keyParam);
            int sizeAtLeastRequired = cipher.GetOutputSize(inputBytes.Length);
            byte[] outputBytes = new byte[sizeAtLeastRequired];
    
            // decrypt            
            int length = cipher.ProcessBytes(inputBytes, outputBytes, 0);
            length += cipher.DoFinal(outputBytes, length);
    
            // resize output
            Array.Resize(ref outputBytes, length);
            return outputBytes;
        }
    

提交回复
热议问题