如何使用aescbc在php5中加密并在windows存储8.1和c#中解密


How to encrypt in php5 and decrypt in windows store 8.1 and c# using aescbc

大家好,我用AesCbc方法在PHP中的一个字符串中加密了单词hello。这是我的密码。

base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,'1234567890123456',pkcs7_pad('hello', 16),MCRYPT_MODE_CBC))

结果是

67fHA+Z12z2jlwOLTBeCPA==

然后我把这个结果发送到我的windows商店应用程序,这是我用来解密它的函数

        public string AES_Decrypt(string input, string pass)
    {
        SymmetricKeyAlgorithmProvider SAP = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbc);
        CryptographicKey AES;
        HashAlgorithmProvider HAP = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
        CryptographicHash Hash_AES = HAP.CreateHash();
        string decrypted = "";
        try
        {
            byte[] hash = new byte[32];
            Hash_AES.Append(CryptographicBuffer.CreateFromByteArray(System.Text.Encoding.UTF8.GetBytes(pass)));
            byte[] temp;
            CryptographicBuffer.CopyToByteArray(Hash_AES.GetValueAndReset(), out temp);
            Array.Copy(temp, 0, hash, 0, 16);
            Array.Copy(temp, 0, hash, 15, 16);
            AES = SAP.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(hash));
            IBuffer Buffer = CryptographicBuffer.DecodeFromBase64String(input);
            byte[] Decrypted;
            CryptographicBuffer.CopyToByteArray(CryptographicEngine.Decrypt(AES,Buffer,null), out Decrypted);
            decrypted = System.Text.Encoding.UTF8.GetString(Decrypted, 0, Decrypted.Length);
            return decrypted;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

结果是这个

7��t�'a2H'0��g

应该是"你好"的时候。那么我的代码哪里出错了呢?

键在PHP代码中没有散列。所以,不要在C#中这样做:

AES = SAP.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(
    System.Text.Encoding.UTF8.GetBytes(pass)
));