Unity数据的加密与解密

好久没有更新博客了大笑

在这里分享在Unity中对数据的加密与解密的处理


加密与解密一般可以应用到文件配置,但是我主要运用到客户端与服务器端交互时候

【加密】

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Text;
using System.Security.Cryptography;

public class Encryption : MonoBehaviour 
{
    /// <summary>
    /// 需要加密内容
    /// </summary>
    public InputField inputCtt;

    /// <summary>
    /// 加密结果
    /// </summary>
    public InputField inputRes;

    /// <summary>
    /// 32位任意数值,作为是加密解码约定数字
    /// </summary>
    private string keyValue = "01234567890123456789012345678901";

	public void OnBtnEncryption()
    {
        if (inputCtt.text.Length != 0)
        {
            string str = ConductEncryption(inputCtt.text, keyValue);
            inputRes.text = str;
        }
        else
        {
            Debug.Log("请输入加密内容");
        }
    }


    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="_input">在输入框中需要加密内容</param>
    /// <param name="_keyValue"></param>
    /// <returns></returns>
    private string ConductEncryption(string _input,string _keyValue)
    {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes(_keyValue);

        //加密格式
        RijndaelManaged encryption = new RijndaelManaged();
        encryption.Key = keyArray;
        encryption.Mode = CipherMode.ECB;
        encryption.Padding = PaddingMode.PKCS7;

        //生成加密锁
        ICryptoTransform cTransform = encryption.CreateEncryptor();
        byte[] _EncryptArray = UTF8Encoding.UTF8.GetBytes(_input);
        byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length);
        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }
}

【解密】

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Text;
using System.Security.Cryptography;


public class Decrypt : MonoBehaviour 
{
    /// <summary>
    /// 获得需要解密的字符串
    /// </summary>
    public InputField valueDense;

    /// <summary>
    /// 32位任意数值,作为是加密解码约定数字
    /// </summary>
    private string keyValue = "01234567890123456789012345678901";

    public void OnBtnDecrypt()
    {
        if (valueDense.text.Length != 0)
        {
            string str = ConductDecrypt(valueDense.text,keyValue);
            valueDense.text = str;
        }
        else
        {
            Debug.Log("请输入需要解密的值");
        }
    }

    private string ConductDecrypt(string _valueDense, string _keyValue)
    {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes(_keyValue);

        RijndaelManaged decipher = new RijndaelManaged();
        decipher.Key = keyArray;
        decipher.Mode = CipherMode.ECB;
        decipher.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = decipher.CreateDecryptor();
        byte[] _EncryptArray = Convert.FromBase64String(_valueDense);
        byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length);
        return UTF8Encoding.UTF8.GetString(resultArray);
    }
	
}

【检测】

在第一个输入框上输入需要加密的内容,当点击加密按钮后,加密后的数据便输出在第二个输入框中

再点击解密按钮后,便有解密后的数据输出



已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页