C#RIJNDAEL解密

时间:2014-06-30 10:29:07

标签: c# php encryption rijndael

我尝试解密JDownloader CNL Feature的请求参数。

http://jdownloader.org/knowledge/wiki/glossary/cnl2

在此样本中,iv和密钥是' 31323334353637383930393837363534'我尝试解密这个值' DRURBGEf2ntP7Z0WDkMP8e1ZeK7PswJGeBHCg4zEYXZSE3Qqxsbi5EF1KosgkKQ9SL8qOOUAI' 要加密的示例中的php代码如下

我知道我需要将密钥从十六进制解码为字符串,这意味着正确的密钥是1234567890987654

function base16Encode($arg){
$ret="";
for($i=0;$i<strlen($arg);$i++){
    $tmp=ord(substr($arg,$i,1));    
    $ret.=dechex($tmp); 
    }
   return $ret;
}

$key="1234567890987654";
$transmitKey=base16Encode($key);
$link="http://rapidshare.com/files/285626259/jDownloader.dmg\r\nhttp://rapidshare.com/files/285622259/jDownloader2.dmg";
$cp = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', '');
@mcrypt_generic_init($cp, $key,$key);
$enc = mcrypt_generic($cp, $link);   
mcrypt_generic_deinit($cp); 
mcrypt_module_close($cp);
$crypted=base64_encode($enc);

echo $crypted;

我最后一次尝试解密的是以下c#代码,但我有一些长期输入的问题。

public static String DecryptRJ(string input, string iv, string key )
    {
        key = key.DecodeBase16(); // Extension method
        byte[] initVectorBytes = Encoding.UTF8.GetBytes(iv);
        byte[] cipherTextBytes = Encoding.UTF8.GetBytes(input);
        byte[] keyBytes = Encoding.UTF8.GetBytes(key);

        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.Mode = CipherMode.CBC;
        symmetricKey.BlockSize = 256;
        symmetricKey.KeySize = 256;
        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);

        MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
        CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);

        byte[] plainTextBytes = new byte[cipherTextBytes.Length];
        int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
        memoryStream.Close();
        cryptoStream.Close();

        return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
    }

另一个信息此PHP代码工作正常,可以解码和解密正确。

function decrypt($data, $_key){
echo '<br><hr><br>';
out($data);
$plain=base64_decode($data);
out($plain);
echo 'init';
//$e = mcrypt_decrypt ( $_cp , $_key , $plain , 'cbc' );
$e = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $_key, $plain, 'cbc', $_key);  
out($e);
echo 'end';

}

1 个答案:

答案 0 :(得分:0)

好了,我现在可以解密加密的样本数据(参见php代码或http://jdownloader.org/knowledge/wiki/glossary/cnl2)Code C#就是这个

public static string DecryptDLCData(string data, string _key, Encoding encoding = null)
    {
        if (encoding == null)
            encoding = Encoding.Default;
        data = data.DecodeBase64(encoding);
        RijndaelManaged rijndaelCipher = new RijndaelManaged();
        rijndaelCipher.Mode = CipherMode.CBC;
        rijndaelCipher.Padding = PaddingMode.Zeros;
        rijndaelCipher.KeySize = 256;
        rijndaelCipher.BlockSize = 128;

        byte[] pwdBytes = Encoding.Default.GetBytes(_key);

        byte[] keyBytes = new byte[16];

        int len = pwdBytes.Length;
        if (len > keyBytes.Length) len = keyBytes.Length;

        Array.Copy(pwdBytes, keyBytes, len);

        rijndaelCipher.Key = keyBytes;
        rijndaelCipher.IV = keyBytes;

        var transform = rijndaelCipher.CreateDecryptor();

        byte[] plainText = Encoding.Default.GetBytes(data);

        byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
        return Encoding.UTF8.GetString(cipherBytes);
    }