UnityはデバイスIDに基づいて暗号化する

4203 ワード

1まず、デバイスのIDを取得します.
2取得したIDを検証コードのような識別しやすい数字にする処理を行う.
3この数字を暗号化する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Test : MonoBehaviour {

    public string filename2;
    private string deviceUniID;
    int num;

    string miyaoadd;
    string corectmiyao;

    public Text yanzhengma;
    public InputField m_input;
	// Use this for initialization
	void Start () {
        num = 1;
        //string m = "-----------------             ---------------";
        //filenames = Application.dataPath + @"\StreamingAssets\         .txt";
        filename2 = Application.dataPath + @"\StreamingAssets\         .txt";
        deviceUniID = SystemInfo.deviceUniqueIdentifier;
        TextEncrypt(deviceUniID);//     
        yanzhengma.text = num.ToString();
        
        corectmiyao = JimiJisuan(num).ToString();
        
        miyaoadd = Readline(1);
        Debug.Log(corectmiyao);

        JianceOne();
        //WriteALine(filenames, deviceUniID, 2);
        //WriteALine(filenames, m, 1);
        //WriteALine(filenames, num.ToString(), 2);
        m_input.onValueChanged.AddListener(delegate (string c)
        {
            miyaoadd = c;
            JianceTwo();
     });
    }
	
	// Update is called once per frame
	void Update () {
		
	}
    private void TextEncrypt(string content)//     
    {
        char[] data = content.ToCharArray();
        

        for (int d = 0; d < data.Length; d++)
        {
           
            if (data[d] == '1')
            {
                num = num +13;
              
            }
            if (data[d] == '2')
            {
                num = num +362;
              
            }
            if (data[d] == '3')
            {
                num = num+34;
            }
            if (data[d] == '4')
            {
                num = num+6;
               
            }
          
            if (data[d] == '6')
            {
                num = num +345;
               
            }
            if (data[d] == '7')
            {
                num = num + 33;
            }
            if (data[d] == '8')
            {
                num = num +25;
               
            }
            if (data[d] == '9')
            {
                num = num +21;
            }
            
        }
        
    }

    
    void WriteALine(string FileName, string txt, int lineNumber)//   1:     (  +   ),  2:        ,  3,     
    {
        string[] str = File.ReadAllLines(FileName);
        int strLinesLength = str.Length;
        string[] strNew = new string[strLinesLength + 1];

        bool haveAddLine = false;//       

        for (int i = 0; i < strLinesLength + 1; ++i)
        {
            if (i == lineNumber - 1) //     ,            
            {
                strNew[i] = txt;
                haveAddLine = true;
            }
            else if (!haveAddLine)//        
            {
                strNew[i] = str[i];
            }
            else//    
            {
                strNew[i] = str[i - 1];
            }
        }
        File.WriteAllLines(FileName, strNew);
    }
    public string Readline(int linenum)//     
    {
        
        string[] strs = File.ReadAllLines(filename2);
        if (strs.Length==0)
        {
            return "0";
        }
        else
        {
            return strs[linenum - 1];
        }
     }
    public int JimiJisuan(int sd)//        
    {
        int miyao = sd * 3  - 126;
        miyao = miyao * 2 + 26;
        miyao = miyao - sd;
        return miyao;
    }
    public void JianceOne( )
    {
        if (miyaoadd != corectmiyao)
        {
           
        }
        else
        {
            SceneManager.LoadScene(1);
        }

    }
    public void JianceTwo( )
    {
        if (miyaoadd != corectmiyao)
        {
            
        }
        else
        {
            WriteALine(filename2, miyaoadd.ToString(), 1);
            SceneManager.LoadScene(1);
        }

    }
}