question

YuKim avatar image
YuKim asked

There was an error encrypting the file being sent or received through the CDN.

I make mesh Upload Donwload program in unity with playfab CDN

I tested encrypting file without CDN

it is succesfully.

Uploading file and Downloading file it was work fine.

Problems arise when downloading via CDN with encryption

Debug ok is to compare files stored on the server with files stored on your computer

this is code part of CDN

using System;
using PlayFab;
using PlayFab.ClientModels;
using PlayFab.AdminModels;
using UnityEngine;
using B83.MeshTools;
using System.IO;
using UnityEngine.Networking;
using System.Collections;
using System.Net;
using UnityEngine.UI;
using System.Text;
using System.Linq;
 
public class CDN : MonoBehaviour
{
    Byte[] Encrtpted;
    Byte[] mIv;
    Byte[] mKey;

    public void Download()
    {
        if (!string.IsNullOrEmpty(keyName))
        {
            DownloadFileFromCDN(keyName);
        }
    }

    public void Upload()
    {
        keyName = inputField.text;
        if (!string.IsNullOrEmpty(keyName)&&scirptCamCon.TargetObj1)
        {
            Byte[] by = ReturnMesh();
            mIv = Encrpyt.GetIv();
            mKey = Encrpyt.GetKey();
           
            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, Encrtpted);
            inputField.gameObject.SetActive(false);
        }
    }   

 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[0];
        byte[] thiskey = new byte[0];
        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]);
           byte[] newKey =  Encoding.UTF8.GetBytes(result.Data[keyVale]);
             thiskey = new byte[newKey.Length];
             Array.Copy(newKey, thiskey, newKey.Length);
         },                   //키값을 출력하는 방법이 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]);
            byte[] newIv = Encoding.UTF8.GetBytes(result.Data[ivVale]);
            thisiv = new byte[newIv.Length];
            Array.Copy(newIv, thisiv, newIv.Length);
        },
        error => {
            Debug.Log("Got error getting titleData:");
            Debug.Log(error.GenerateErrorReport());
        }
    );
        yield return www.SendWebRequest();
        yield return new WaitForSeconds(3.0f);


        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("SucessfulDownload");
            results = www.downloadHandler.data;


            //파일 변환 부분
            if (scirptCamCon.TargetObj1)
            {
                DestroyImmediate(scirptCamCon.TargetObj1.GetComponent<MeshCollider>());


                if (results.SequenceEqual(Encrtpted))
                    Debug.Log("Ok");
                if (thiskey.SequenceEqual(mKey))
                    Debug.Log("Ok");
                if (thisiv.SequenceEqual(mIv))
                    Debug.Log("Ok");


                byte[] newby = EncDec.Decrypt(results, thiskey, thisiv);


                Mesh me = MeshSerializer.DeserializeMesh(newby, scirptCamCon.TargetObj1.GetComponent<MeshFilter>().mesh);
                scirptCamCon.TargetObj1.AddComponent<MeshCollider>();
                scirptCamCon.TargetObj1.GetComponent<MeshFilter>().mesh = me;


            }
            else
                Debug.Log("선택된 오브젝트가 업습니다.");
        }
    }

}
I do not get an error without a CDN and I do not know why the file is interpreted as the same.

unity3d
11.png (24.9 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

Hi,

Technically speaking, if Uploading and Downloading file works fine with our CDN, then it should also works fine with encrypted file because they are all just bits. I think the main problem is happening on the process of encryption or decryption, you should focus on that more.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.