C# decrypt Rijndael 加密字符串 using CakePHP 的 Security::rijndael


C# decrypt Rijndael encrypted string using CakePHP's Security::rijndael

>我正在尝试解密使用CakePHP的Security::rijndael()函数加密的字符串。这是我的代码:

$enc = bin2hex( Security::rijndael( $field, Configure::read('Security.key'), 'encrypt' ) );

请注意,加密后,我使用 bin2hex() 函数将其转换为可以存储在数据库中的十六进制字符串。

在 CakePHP 中,我可以使用以下方法轻松解密:

$dec = Security::rijndael( hex2bin( $field ), Configure::read( 'Security.key' ), 'decrypt' );

我正在尝试使用 C# 通过首先运行 hex2bin 来实现相同的解密。我尝试使用我在网上找到的 hex2bin() 示例:

private string hex2bin( string hexvalue ) {
    string binaryval = "";
    binaryval = Convert.ToString( Convert.ToInt64( hexvalue, 16 ), 2 );
    return binaryval;
}

。但这不断返回错误:对于 UInt64 来说,值太大或太小,我无法超越这一点。

我正在寻找有关如何在 C# 中反转该过程的想法。

谢谢。

您必须将字符串切成一系列字节,一次解码一个:

private byte[] hex2bin( string hexvalue )
{
    byte [] result = new byte[hexvalue.Length / 2];;
    for (int pos = 0; pos < hexvalue.Length; po+=2)
       byte[pos/2] =  Convert.Byte( hexvalue.SubString(pos, 2),  16 );
   return result;
}

结果应该是一个字节数组,而不是一个字符串。