Unity 根据设备ID进行加密

本文介绍了一个使用Unity实现设备唯一标识符转换为验证码,并对其进行加密的过程。通过特定算法,设备ID被转化为易识别的数字,再经过加密处理,生成了用于验证的密钥。文中详细展示了代码实现步骤,包括读取配置文件、加密计算和场景切换等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
        }

    }
}

 

This asset obfuscates your code to make it harder for bad guys to reverse engineer your projects. Specifically designed for Unity, it seamlessly links in with its build process. The top priority of this package is to work straight out of the box with no extra steps required. While other obfuscators can stop a game from working, Beebyte's obfuscator looks for specific Unity related code that must be protected. The contents of your source files are unchanged, the obfuscation targets the compiled assembly. Features: - Supports IL2CPP - Supports Assembly Definition Files (Unity 2017.3+) - Removes Namespaces without any conflicts - Recognises Unity related code that must not be changed - Renames Classes (including MonoBehaviours) - Renames Methods - Renames Parameters - Renames Fields - Renames Properties - Renames Events - String literal obfuscation - Adds fake methods - Easy and extensive customisation using the Unity inspector window - Consistent name translations are possible across multiple builds and developers - Semantically secure cryptographic naming convention for renamed members The asset works for both Unity Free and Unity Pro version 4.2.0 onwards (including Unity 5 & 2017 & 2018). Build targets include Standalone, Android, iOS, WebGL, UWP. Other platforms are not guaranteed or supported but may become supported at a future date. IL2CPP builds are much harder to reverse engineer but strings and member information (class, method names etc) are visible in the global-metadata.dat file. Obfuscation will apply to this file adding further security. Why not complement your security with the Anti-Cheat Toolkit - a great third party asset. For more information about the Obfuscator, please see the FAQ
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值