1

PHPサーバーに投稿するC#サーバーがあります。PHP 側の JSON 文字列の先頭からちょうど 16 文字が欠落しています。PHP の復号化は次のようになります。

function Decrypt($data_base64)
{
    global $key;
    global $iv_size;

    $ciphertext_dec = base64_decode($data_base64);

    $iv_dec = substr($ciphertext_dec, 0, $iv_size);
    $ciphertext_dec = substr($ciphertext_dec, $iv_size);

    $plaintext_utf8_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,
        $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);

    return  $plaintext_utf8_dec;
}

そしてC#の投稿:

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

        var started = new StartStopObject() { action = "online" };
        string jsonser1 = new JavaScriptSerializer().Serialize(started);
        Post(Encrypt(jsonser1));

    private string Encrypt(string plainStr)
    {
        aesCrypt.GenerateIV();
        byte[] encrypted;
        ICryptoTransform crypto = aesCrypt.CreateEncryptor(aesCrypt.Key, aesCrypt.IV);
        using (System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream())
        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, crypto, CryptoStreamMode.Write))
        {
            using (System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt))
            {
                swEncrypt.Write(plainStr);
            }
            encrypted = msEncrypt.ToArray();
        }
        return Convert.ToBase64String(encrypted);
    }

    public void Post(string data)
    {
        byte[] buffer = Encoding.UTF8.GetBytes("var1=" + data);
        HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(posturl);
        WebReq.Method = "POST";
        WebReq.ContentType = "application/x-www-form-urlencoded";
        WebReq.ContentLength = buffer.Length;
        System.IO.Stream PostData = WebReq.GetRequestStream();
        PostData.Write(buffer, 0, buffer.Length);
        PostData.Close();
    }

PHP の vardump と echo は次を示します。

array(1) { ["var1"]=> string(128) "UahqVaE2nrxrTAijsZmjXL8QF9YmcRXdcRUREaFp7LKlhy6StrXqMc7TDmCF4qRT8fZZOZ5ovY/vHySzP2u73cs66i7nG1ywXrGiZOHa4E9yiOFFruQegIy/6yqiPXf9" } e","email":null,"realm":null,"script":null,"followtag":null ,"autojoin":null}

ご覧のとおり、JSON 文字列の先頭から正確に 16 文字が欠落しています。( {"action":"online","email":null,"realm":null,"script":null,"followtag":null,"autojoin":null} )

4

1 に答える 1