需要帮助将laravel Crypt转换为C#

前端之家收集整理的这篇文章主要介绍了需要帮助将laravel Crypt转换为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,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,buffer.Length))).ToString();
}

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

我正在使用上面的代码,当我从Laravel解密任何东西时,它都有效.问题是当我从c#加密一个字符串时,我无法在PHP中解密它.

有时在解密文本后面有“值”.加密输出,并在PHP中解密它.

解决方法

将填充更改为PaddingMode.PKCS7可以正常工作!如果其他人仍然需要这个,完整的代码发布在下面.
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,};
    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,buffer.Length))).ToString();
}
原文链接:https://www.f2er.com/csharp/99222.html

猜你在找的C#相关文章