【问题标题】:Decrypting a PHP encrypted string in C#?在 C# 中解密 PHP 加密字符串?
【发布时间】:2018-06-26 17:32:03
【问题描述】:

是否可以在 C# 中解密使用 PHP 加密的字符串?这是我在 PHP 中用来加密它的代码:

$string = "Hello. This is a test string.";

$key = "testPassword";
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));

如果可能,只需使用 C# 解密即可。

【问题讨论】:

  • 希望你们的孩子不要再像这样嵌套所有这些加密/散列方法。只需使用mcrypt 之类的东西来使用通用站点密码(高熵,如果您想要良好的安全性则复杂)来加密密码。首先,密钥的 md5 不能像好的密钥那样安全。
  • 这个加密方法取自这个问题:stackoverflow.com/questions/1289061/… 97 票赞成,这似乎是最好的方法。

标签: c# php encryption


【解决方案1】:

解密部分回答here

C#

    public static string EncryptString(string message, string KeyString, string IVString)
    {
        byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
        byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);

        string encrypted = null;
        RijndaelManaged rj = new RijndaelManaged();
        rj.Key = Key;
        rj.IV = IV;
        rj.Mode = CipherMode.CBC;

        try
        {
            MemoryStream ms = new MemoryStream();

            using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
            {
                using (StreamWriter sw = new StreamWriter(cs))
                {
                    sw.Write(message);
                    sw.Close();
                }
                cs.Close();
            }
            byte[] encoded = ms.ToArray();
            encrypted = Convert.ToBase64String(encoded);

            ms.Close();
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
            return null;
        }
        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine("A file error occurred: {0}", e.Message);
            return null;
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: {0}", e.Message);
        }
        finally
        {
            rj.Clear();
        }

        return encrypted;
    }

解码base64:

/// <summary>
/// The method create a Base64 encoded string from a normal string.
/// </summary>
/// <param name="toEncode">The String containing the characters to encode.</param>
/// <returns>The Base64 encoded string.</returns>
public static string EncodeTo64(string toEncode)
{

    byte[] toEncodeAsBytes

          = System.Text.Encoding.Unicode.GetBytes(toEncode);

    string returnValue

          = System.Convert.ToBase64String(toEncodeAsBytes);

    return returnValue;

}




/// <summary>
/// The method to Decode your Base64 strings.
/// </summary>
/// <param name="encodedData">The String containing the characters to decode.</param>
/// <returns>A String containing the results of decoding the specified sequence of bytes.</returns>
public static string DecodeFrom64(string encodedData)
{

    byte[] encodedDataAsBytes

        = System.Convert.FromBase64String(encodedData);

    string returnValue =

       System.Text.Encoding.Unicode.GetString(encodedDataAsBytes);

    return returnValue;

}

【讨论】:

  • 请在您的答案中包含一些代码 - 尤其是不在 stackoverflow 上的代码。没有人知道该链接将来是否仍然有效。
【解决方案2】:

C#
首先,您需要使用 System.Security.Cryptography。

public String AES_encrypt(String Input)
{
    var aes = new RijndaelManaged();
    aes.KeySize = 256;
    aes.BlockSize = 256;
    aes.Padding = PaddingMode.PKCS7;
    aes.Key = Convert.FromBase64String("yourkey");//your key
    aes.IV = Convert.FromBase64String("youriv");//your iv
    var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
    byte[] xBuff = null;
    using (var ms = new MemoryStream())
    {
        using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
        {
            byte[] xXml = Encoding.UTF8.GetBytes(Input);
            cs.Write(xXml, 0, xXml.Length);
        }
        xBuff = ms.ToArray();
    }
    String Output = Convert.ToBase64String(xBuff);
    return Output;
}

public String AES_decrypt(String Input)
{
    RijndaelManaged aes = new RijndaelManaged();
    aes.KeySize = 256;
    aes.BlockSize = 256;
    aes.Mode = CipherMode.CBC;
    aes.Padding = PaddingMode.PKCS7;
    aes.Key = Convert.FromBase64String("yourkey");//your key
    aes.IV = Convert.FromBase64String("youriv");//your iv
    var decrypt = aes.CreateDecryptor();
    byte[] xBuff = null;
    using (var ms = new MemoryStream())
    {
        using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
        {
            byte[] xXml = Convert.FromBase64String(Input);
            cs.Write(xXml, 0, xXml.Length);
        }
        xBuff = ms.ToArray();
    }
    String Output = Encoding.UTF8.GetString(xBuff);
    return Output;
}


PHP

function addpadding($string,$blocksize=32)
{
    $len = strlen($string);
    $pad = $blocksize - ($len % $blocksize);
    $string .= str_repeat(chr($pad),$pad);
    return $string;
}

function encrypt($string = "")
{
    $key = base64_decode("yourkey");//your key
    $iv = base64_decode("youriv");//your iv
    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, addpadding($string), MCRYPT_MODE_CBC, $iv));
}

function strippadding($string)
{
    $slast = ord(substr($string, -1));
    $slastc = chr($slast);
    $pcheck = substr($string, -$slast);
    if(preg_match("/$slastc{".$slast."}/", $string)){
    $string = substr($string, 0, strlen($string)-$slast);
        return $string;
    } else {
        return false;
    }
}
function decrypt($string)
{
    $key = base64_decode("your key");//your key
    $iv = base64_decode("your iv");//
    $string = base64_decode($string);
    return strippadding(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string, MCRYPT_MODE_CBC, $iv));
}

【讨论】:

    猜你喜欢
    • 2010-09-18
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多