Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need help Converting laravel Crypt to C#

public static string Encrypt(this string plainText)
{
    RijndaelManaged aes = new RijndaelManaged();
    aes.KeySize = 256;
    aes.BlockSize = 128;
    aes.Padding = PaddingMode.Zeros;
    aes.Mode = CipherMode.CBC;

    aes.Key = Encoding.Default.GetBytes(key);
    aes.GenerateIV();

    ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key, aes.IV);
    byte[] buffer = Encoding.ASCII.GetBytes(plainText);

    String encryptedText = Convert.ToBase64String(Encoding.Default.GetBytes(Encoding.Default.GetString(AESEncrypt.TransformFinalBlock(buffer, 0, buffer.Length))));

    String mac = "";
    using (var hmacsha256 = new HMACSHA256(Encoding.Default.GetBytes(key)))
    {
        hmacsha256.ComputeHash(Encoding.Default.GetBytes(Convert.ToBase64String(aes.IV) + encryptedText));

        mac = ByteArrToString(hmacsha256.Hash);
    }

    var keyValues = new Dictionary<string, object>
    {
        { "iv", Convert.ToBase64String(aes.IV) },
        { "value", encryptedText },
        { "mac", mac },
    };
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    //return serializer.Serialize(keyValues);
    return Convert.ToBase64String(Encoding.ASCII.GetBytes(serializer.Serialize(keyValues)));
}



public static string Decrypt(this string cipherText)
{
    RijndaelManaged aes = new RijndaelManaged();
    aes.KeySize = 256;
    aes.BlockSize = 128;
    aes.Padding = PaddingMode.Zeros;
    aes.Mode = CipherMode.CBC;

    aes.Key = Encoding.Default.GetBytes(key);

    dynamic payload = GetJsonPayload(cipherText);

    //return Encoding.Default.GetString(Convert.FromBase64String(cipherText));

    //cipherText = Convert.ToBase64String(Encoding.Default.GetBytes(payload["value"]));
    aes.IV = Convert.FromBase64String(payload["iv"]);

    ICryptoTransform AESDecrypt = aes.CreateDecryptor(aes.Key, aes.IV);
    byte[] buffer = Convert.FromBase64String(payload["value"]);

    return (Encoding.Default.GetString(AESDecrypt.TransformFinalBlock(buffer, 0, buffer.Length))).ToString();
}

https://github.com/laravel/framework/blob/5.1/src/Illuminate/Encryption/Encrypter.php

i am using the code above, it works when i decrypt anything from Laravel. problem is when i encrypt a string from c#, i cannot decrypt it in php.

sometimes there are "values" after the decrypted text. encrypting the output, and decrypting it in php works.

C# screenshot

like image 245
Joe Avatar asked Nov 16 '15 03:11

Joe


1 Answers

changing the padding to PaddingMode.PKCS7 works! in case other people still need this one, full code are posted below.

public static string Encrypt(this string plainText)
{
    RijndaelManaged aes = new RijndaelManaged();
    aes.KeySize = 256;
    aes.BlockSize = 128;
    aes.Padding = PaddingMode.PKCS7;
    aes.Mode = CipherMode.CBC;

    aes.Key = Encoding.Default.GetBytes(key);
    aes.GenerateIV();

    ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key, aes.IV);
    byte[] buffer = Encoding.ASCII.GetBytes(plainText);

    String encryptedText = Convert.ToBase64String(Encoding.Default.GetBytes(Encoding.Default.GetString(AESEncrypt.TransformFinalBlock(buffer, 0, buffer.Length))));

    String mac = "";
    using (var hmacsha256 = new HMACSHA256(Encoding.Default.GetBytes(key)))
    {
        hmacsha256.ComputeHash(Encoding.Default.GetBytes(Convert.ToBase64String(aes.IV) + encryptedText));

        mac = ByteArrToString(hmacsha256.Hash);
    }

    var keyValues = new Dictionary<string, object>
    {
        { "iv", Convert.ToBase64String(aes.IV) },
        { "value", encryptedText },
        { "mac", mac },
    };
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    //return serializer.Serialize(keyValues);
    return Convert.ToBase64String(Encoding.ASCII.GetBytes(serializer.Serialize(keyValues)));
}



public static string Decrypt(this string cipherText)
{
    RijndaelManaged aes = new RijndaelManaged();
    aes.KeySize = 256;
    aes.BlockSize = 128;
    aes.Padding = PaddingMode.PKCS7;
    aes.Mode = CipherMode.CBC;

    aes.Key = Encoding.Default.GetBytes(key);

    dynamic payload = GetJsonPayload(cipherText);

    //return Encoding.Default.GetString(Convert.FromBase64String(cipherText));

    //cipherText = Convert.ToBase64String(Encoding.Default.GetBytes(payload["value"]));
    aes.IV = Convert.FromBase64String(payload["iv"]);

    ICryptoTransform AESDecrypt = aes.CreateDecryptor(aes.Key, aes.IV);
    byte[] buffer = Convert.FromBase64String(payload["value"]);

    return (Encoding.Default.GetString(AESDecrypt.TransformFinalBlock(buffer, 0, buffer.Length))).ToString();
}
like image 146
Joe Avatar answered Sep 22 '22 14:09

Joe