tags:

views:

399

answers:

4

I've got this php code and I'd like to get the exact equivalent C#

$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_192, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
$encryptedData = mcrypt_encrypt(MCRYPT_RIJNDAEL_192, $key, $salt . $message . $nonce, MCRYPT_MODE_CBC, $iv);
$base64Data = base64_encode($salt . $iv . $encryptedData);
$urlEncodedData = rawurlencode($base64Data);

all contributions gratefully received!

+3  A: 

Too many variables to convert directly; I recommend that you examine what it does and rewrite it in C# to adhere to the intent of the original code.

OmegaMan
A: 

Sorry, but to be honest - the question is rather silly.

All this code does is some nasty crypting and encoding. PHP is full of global functions from modules that do the thing. For C# You need to find proper libs/classes that have methods for crypting etc. It will probably ahve different logic and parameters. I bet You'll even get some output differences due to different implementations.

I suppose this is the closest thing You'll find to an answer to Your question

naugtur
A: 

Nay sayers and doom mongers, behold!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;

namespace EncryptData
{
    class EncryptData
    {
    private static readonly Encoding ASCII_ENCODING = new System.Text.ASCIIEncoding();

    private static string md5(string text)
    {
        return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(ASCII_ENCODING.GetBytes(text))).Replace("-", "").ToLower();
    }

    public abstract string nonce();

    public abstract string salt();

    public readonly string EncryptedData;

    public EncryptData(string message)
    {

        // set up encrytion object
        RijndaelManaged aes192 = new RijndaelManaged();
        aes192.KeySize = 192;
        aes192.BlockSize = 192;
        aes192.Padding = PaddingMode.None;
        aes192.Mode = CipherMode.CBC;
        aes192.Key = ASCII_ENCODING.GetBytes(md5(SECRET_KEY));
        aes192.GenerateIV();

        string localSalt = salt();
        string localNonce = nonce();

        // form the string for encrypting
        // and put into byte array
        string textToEncrypt = localSalt + message+ localNonce;
        byte[] plainTextBytes = ASCII_ENCODING.GetBytes(textToEncrypt);

        // encrypt the data            
        ICryptoTransform encryptor = aes192.CreateEncryptor();
        MemoryStream ms = new MemoryStream();

        CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
        cs.Write(plainTextBytes, 0, plainTextBytes.Length);

        // convert our encrypted data from a memory stream into a byte array.
        byte[] cypherTextBytes = ms.ToArray();

        // close memory stream
        ms.Close();

        byte[] combined = null;
        // combine data and convert to byte array
        combined = CombinedData(localSalt, aes192.IV, cypherTextBytes);
        // url encode data once converted to base64 string
        EncryptedData = HttpUtility.UrlEncode(base64CombinedData(combined), ASCII_ENCODING);


    }

    public byte[] CombinedData(string salt, byte[] IV, byte[] cypherTextBytes)
    {
        // convert salt string into byte array
        byte[] saltBytes = ASCII_ENCODING.GetBytes(salt);

        // catenate all the byte arrays into one
        // set up dest byte array with required size
        byte[] rv = new byte[saltBytes.Length + IV.Length + cypherTextBytes.Length];

        // copy in each byte array
        Buffer.BlockCopy(saltBytes, 0, rv, 0, saltBytes.Length);
        Buffer.BlockCopy(IV, 0, rv, saltBytes.Length, IV.Length);
        Buffer.BlockCopy(cypherTextBytes, 0, rv, saltBytes.Length + IV.Length, cypherTextBytes.Length);

        return rv;
    }

    public string base64CombinedData(byte[] rv)
    {
        return Convert.ToBase64String(rv);
    }

}
}
Rob
A: 

In the example provided by Rob I think he should have used PaddingMode.Zero instead of PaddingMode.None, as this is the way mcrypt_encrypt() adds paddings.

mcanti