CryptographicException:playfabserver

时间:2019-06-28 07:27:24

标签: c# unity3d server mesh

我将网格转换为二进制 我用Key,Iv加密并上传了文件

我从playfabserver下载了它,解密后将二进制文件转换为网格文件

此错误发生在解密中 CryptographicException:要解密的数据长度无效。

这是发送到服务器的损失吗?

此处加密源 https://www.codeproject.com/Articles/5719/Simple-encrypting-and-decrypting-data-in-C

当然,无需服务器即可成功加密和解密网格文件。

上传下载处于活动状态

public void Upload()
    {
        keyName = inputField.text;
        if (!string.IsNullOrEmpty(keyName)&&scirptCamCon.TargetObj1)
        {
            Byte[] by = ReturnMesh();
            Byte[] mIv = Encrpyt.GetIv();
            Byte[] mKey = Encrpyt.GetKey();

            Byte[] Encrtpted = EncDec.Encrypt(by, mIv, mKey);
            PlayFabServerAPI.SetTitleData( new PlayFab.ServerModels.SetTitleDataRequest
            {
                  Key = keyName+"2",
                  Value = Encoding.UTF8.GetString(mKey)
            },
              Onsuccess => { Debug.Log(Encoding.UTF8.GetString(mKey)); },
              Onfail => { Onfail.GenerateErrorReport(); }
            );

            PlayFabServerAPI.SetTitleInternalData(new PlayFab.ServerModels.SetTitleDataRequest
            {
                Key = keyName + "1",
                Value = Encoding.UTF8.GetString(mIv)
            },
              Onsuccess => { Debug.Log(Encoding.UTF8.GetString(mIv)); },
              Onfail => { Onfail.GenerateErrorReport(); }
            );
            UploadFileToCDN(keyName, by);
            inputField.gameObject.SetActive(false);
        }
    }   


    public void UploadFileToCDN(string key, byte[] content, string contentType = "binary/octet-stream")
    {
        GetUploadUrl(key, contentType, presignedUrl =>
        {
           PutFile(presignedUrl, content);
        });
    }

    void GetUploadUrl(string key, string contentType, Action<string> onComplete)
    {
        PlayFabAdminAPI.GetContentUploadUrl(new GetContentUploadUrlRequest()
        {
            ContentType = contentType,
            Key = key
        }, result => onComplete(result.URL),
        error => Debug.LogError(error.GenerateErrorReport()));
    }

    public void PutFile(string putURL, byte[] payload)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(putURL);
        request.Method = "PUT";
        request.ContentType = "binary/octet-stream";

        if (payload != null)
        {
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(payload, 0, payload.Length);
            dataStream.Close();
        }
        else
        {
            Debug.LogWarning(string.Format("ERROR: Byte arrry was empty or null"));
            return;
        }

        Debug.Log("Starting HTTP PUT...");  
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        if (response.StatusCode == HttpStatusCode.OK)
        {
            Debug.Log("...HTTP PUT Successful");
        }
        else
        {
            Debug.LogWarning(string.Format("ERROR: [{0}] -- {1}", response.StatusCode, response.StatusDescription));
        }
    }

    public void DownloadFileFromCDN(string key)
    {
        GetDownloadUrl(key, presignedUrl =>
        {
            GetFile(presignedUrl);
        });
    }

    void GetDownloadUrl(string key, Action<string> onComplete)
    {
        if(scirptCamCon.TargetObj1)
        scirptCamCon.TargetObj1.name = key;
        PlayFabClientAPI.GetContentDownloadUrl(new GetContentDownloadUrlRequest()
        {
            Key = key,
            ThruCDN = true 
        }, result => onComplete(result.URL),
        error => Debug.LogError(error.GenerateErrorReport()));
    }

    void GetFile(string preauthorizedUrl)
    {
        StartCoroutine(FileRecevier(preauthorizedUrl));
    }

    IEnumerator FileRecevier(string preauthorizedUrl)
    {
        string keyVale = keyName + "2";
        string ivVale = keyName + "1";
        byte[] thisiv=new byte[16];
        byte[] thiskey= new byte[16];
        UnityWebRequest www = UnityWebRequest.Get(preauthorizedUrl);
        PlayFabServerAPI.GetTitleData(new PlayFab.ServerModels.GetTitleDataRequest(),
       result => {
           if (result.Data == null || !result.Data.ContainsKey(keyVale)) Debug.Log("No key");
           else Debug.Log("KeyValue: " + result.Data[keyVale]);
           thiskey = Encoding.UTF8.GetBytes(result.Data[keyVale]);
       },                   //키값을 출력하는 방법이 result.data[keyname]을 
       error => {
           Debug.Log("Got error getting titleData:");
           Debug.Log(error.GenerateErrorReport());
       }
        );
        PlayFabServerAPI.GetTitleInternalData(new PlayFab.ServerModels.GetTitleDataRequest(),
        result => {
            if (result.Data == null || !result.Data.ContainsKey(ivVale)) Debug.Log("No value");
            else Debug.Log("ivVale: " + result.Data[ivVale]);
            thisiv = Encoding.UTF8.GetBytes(result.Data[ivVale]);
        },
        error => {
            Debug.Log("Got error getting titleData:");
            Debug.Log(error.GenerateErrorReport());
        }
    );
        yield return www.SendWebRequest();
        yield return new WaitForSeconds(2.0f);

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("SucessfulDownload");
            results = www.downloadHandler.data;
            if (scirptCamCon.TargetObj1)
            {
                Debug.Log(thiskey.Length);
                Debug.Log(thisiv.Length);
                DestroyImmediate(scirptCamCon.TargetObj1.GetComponent<MeshCollider>());
                results = EncDec.Decrypt(results, thiskey, thisiv);
                MeshSerializer.DeserializeMesh(results, scirptCamCon.TargetObj1.GetComponent<MeshFilter>().mesh);
                scirptCamCon.TargetObj1.AddComponent<MeshCollider>();

            }
            else
                Debug.Log("Null obj.");
        }
    }

CryptographicException:要解密的数据长度无效。

1 个答案:

答案 0 :(得分:0)

我使用相同的键,相同的iv,相同的数据 奇迹般的结果是原始的

到服务器文件的转换非常繁琐