-1

I try to decrypt request params for JDownloader CNL Feature.

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

In this sample the iv and the key is '31323334353637383930393837363534' and i try to decrypt this value 'DRurBGEf2ntP7Z0WDkMP8e1ZeK7PswJGeBHCg4zEYXZSE3Qqxsbi5EF1KosgkKQ9SL8qOOUAI' The php code in sample to encrypt is the following

I know i need to decode the key from hex to string, that means the correct key is 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;

My last try to decrypt is the following c# code but i have some troble with lenght of input.

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);
    }

One more Information this PHP Code works fine and can decode and decrypt correct.

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';

}

  • In Php MCRYPT the constant `MCRYPT_RIJNDAEL_128` means Rijndael with the 128-bit block size, i.e. AES. Therefore in C# you should set the BlockSize property to 128, or just use the AesManaged class. Also, you Php output is base64 encoded. UTF8 decoding in C# is incorrect, you need to use something like [Convert.FromBase64String()](http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.110).aspx). – President James K. Polk Jun 30 '14 at 13:26
  • Your suggestion with base64 is correct and i know it, but the string I receive (see sample) is DRurBGEf2ntP7Z0WDkMP8e1ZeK7PswJGeBHCg4zEYXZSE3Qqxsbi5EF1KosgkKQ9SL8qOOUAI and this is to short for a Base64 string... – Florian Gilde Jun 30 '14 at 13:54
  • When I run your PHP code I get `DRurBGEf2ntP7Z0WDkMP8e1ZeK7PswJGeBHCg4zEYXZSE3Qqxsbi5EF1KosgkKQ92unOQB3eMZ3Kq3S0ZZJDzc9RZXCdeAPUB+YCVgeaereuFcCi5y4TgvNKmaC9DbjkYAaoHN678+u/R8mmYhAXlw==` – President James K. Polk Jun 30 '14 at 21:05
  • Yes this is correct, and now i need to decrypt your result in C# – Florian Gilde Jul 01 '14 at 05:45
  • Start with replacing the `byte[] cipherTextBytes = Encoding.UTF8.GetBytes(input);` part with a code that Base64-decodes the input. – Oleg Estekhin Jul 01 '14 at 06:06
  • So i cant decode the string with base64 i get an exception that this input cant decoded... i dont know why, if i take the string in in PHP, i Can easy decode and it works... for example i have written php code that works function decrypt($data, $_key){ echo '


    '; 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'; }
    – Florian Gilde Jul 01 '14 at 14:54

1 Answers1

0

Ok now i can decrypt the encrypted sample data (see php code or http://jdownloader.org/knowledge/wiki/glossary/cnl2) Code C# is this

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);
    }