使用C#解密PHP加密字符串

时间:2013-11-29 16:42:46

标签: c# php

我有这个PHP函数来加密字符串

function addpadding($string, $blocksize = 16)
{
    $len = strlen($string);
    $pad = $blocksize - ($len % $blocksize);
    $string .= str_repeat(chr($pad), $pad);
    return $string;
}
function AESEncrypt($message, $keystring, $IVstring){
    $IV_UTF = mb_convert_encoding($IVstring, 'UTF-8');
    $KEY_UTF = $keystring;

    return base64_encode( mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $KEY_UTF, addpadding($message), MCRYPT_MODE_CBC, $IV_UTF));
}

现在我需要使用C#解密由此函数加密的字符串,但由于我是C#的新手,除了在此加密函数上编写解密函数之外,我无法做任何事情(这与以上PHP函数):

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

string enctext = EncryptString("test", "qwertyuiopasdfghjklzxcvbnmqwerty", _"1234567890123456");

这是我试图做的事情(但没有运气):

 // Decrypt a byte array into a byte array using a key and an IV 
        private byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
        {
            byte[] decryptedData;
            //string plaintext = null;
            //MemoryStream ms = new MemoryStream(cipherData);

            RijndaelManaged alg = new RijndaelManaged();
   alg.KeySize = 256;
            alg.BlockSize = 128;
            alg.Key = Key;
            alg.IV = IV;
            alg.Mode = CipherMode.CBC;
            alg.Padding = PaddingMode.Zeros;

            //Array.Copy(Key, 0, IV, 0, IV.Length);

            ICryptoTransform decryptor = alg.CreateDecryptor(alg.Key, alg.IV);

            using(MemoryStream ms = new MemoryStream(cipherData))
            {
                using (CryptoStream csDecrypt = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader sw = new StreamReader(csDecrypt))
                    {
                        sw.ReadToEnd();
                        sw.Close();
                    }

                    csDecrypt.Close();
                    decryptedData = ms.ToArray();
                }
            }

            //byte[] decryptedData = System.Text.Encoding.Unicode.GetBytes(plaintext);
            return decryptedData; 
        }

感谢您的帮助!

0 个答案:

没有答案