unity 字符串加密脚本工具

一、废话少数,上代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;

public class StringEncryption 
{
    /// <summary>
    /// 字符串加密密钥(注意:密钥只能是4位)
    /// </summary>
    static string encryptKey = "1234";

    /// <summary>
    /// 加密字符串
    /// </summary>
    /// <param name="str">要加密的字符串</param>
    /// <returns></returns>
    public static string Encrypt(string str)
    {
        //加密字符串
        try
        {
            byte[] key = Encoding.Unicode.GetBytes(encryptKey);//密钥
            byte[] data = Encoding.Unicode.GetBytes(str);//待加密字符串

            DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();//加密、解密对象
            MemoryStream MStream = new MemoryStream();//内存流对象

            //用内存流实例化加密流对象
            CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
            CStream.Write(data, 0, data.Length);//向加密流中写入数据
            CStream.FlushFinalBlock();//将数据压入基础流
            byte[] temp = MStream.ToArray();//从内存流中获取字节序列
            CStream.Close();//关闭加密流
            MStream.Close();//关闭内存流

            return Convert.ToBase64String(temp);//返回加密后的字符串
        }
        catch
        {
            return str;
        }
    }

    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="str">加密过的字符串</param>
    /// <returns></returns>
    public static string Decrypt(string str)
    {
        //解密字符串
        try
        {
            byte[] key = Encoding.Unicode.GetBytes(encryptKey);//密钥
            byte[] data = Convert.FromBase64String(str);//待解密字符串

            DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();//加密、解密对象
            MemoryStream MStream = new MemoryStream();//内存流对象

            //用内存流实例化解密流对象
            CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
            CStream.Write(data, 0, data.Length);//向加密流中写入数据
            CStream.FlushFinalBlock();//将数据压入基础流
            byte[] temp = MStream.ToArray();//从内存流中获取字节序列
            CStream.Close();//关闭加密流
            MStream.Close();//关闭内存流

            return Encoding.Unicode.GetString(temp);//返回解密后的字符串
        }
        catch
        {
            return str;
        }
    }
}

二、介绍
从代码不难看出这是核心,且一共只有两个方法,解密和解密
三、扩展
在加密解密的基础上,写了一个编辑器扩展类,请将此类放在editor文件夹下
在这里插入图片描述
使用方法
在这里插入图片描述
可以直接导入文本文件,也可以自己输入内容来进行加密
在这里插入图片描述
在这里插入图片描述
好了,相信大家已经晓得了,上代码

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

public class StringEncryptionEditor : EditorWindow
{
    [MenuItem("Tools/文本文件加密")]
    public static void MenuItem()
    {
        window = EditorWindow.GetWindow(typeof(StringEncryptionEditor));
        window.Show();
    }

    static EditorWindow window;
    public string InputStr;//用户输入的内容
    public string TxtStr;//路径下的内容
    public string EnvryptInputStr;//加密后的内容
     

    public string inputPath;  //用户输入的文件路径\

  
    public StringEncryption TxTEncryption = new StringEncryption();
    private bool IsPath = false;


    private GUILayoutOption width100 = GUILayout.Width(100);
    private GUILayoutOption hight50 = GUILayout.Height(50);
    private GUILayoutOption hight30 = GUILayout.Height(30);
    private Vector2 Scroview = new Vector2();


    private void OnEnable()
    {
        inputPath = PlayerPrefs.GetString("TxTEncryptionEditor_InputPath");
        InputStr = PlayerPrefs.GetString("TxTEncryptionEditor_InputStr");

    }
    private void OnDisable()
    {
        PlayerPrefs.SetString("TxTEncryptionEditor_InputPath", inputPath);
        PlayerPrefs.SetString("TxTEncryptionEditor_InputStr", InputStr);
    }
    private void OnGUI()
    {
        GUIStyle gUIStyle = new GUIStyle();
        gUIStyle.alignment =  TextAnchor.MiddleCenter;
        EditorGUILayout.BeginHorizontal(gUIStyle, hight50);
        if (GUILayout.Button("使用路径", width100, hight30))
        {
            IsPath = true;
            GetPathTxt();
        }
        if (GUILayout.Button("自定义输入", width100, hight30))
        {
            IsPath = false;
        }
        EditorGUILayout.EndHorizontal();
        if (IsPath)
        {
            EditorGUILayout.BeginHorizontal(hight50);
            if (GUILayout.Button("选择文件", width100, hight30))
            {
                inputPath = EditorUtility.OpenFilePanel("选择文本文件", inputPath, "txt");
                GetPathTxt();
            }

            GUILayout.Label("文件路径:" + inputPath);
            EditorGUILayout.EndHorizontal();
            if (!string.IsNullOrEmpty(inputPath))
            {
                Scroview = EditorGUILayout.BeginScrollView(Scroview, false, true, GUILayout.Height(100));
                stringBuilder.Clear();
                stringBuilder.Append(GUILayout.TextArea(TxtStr));
                EditorGUILayout.EndScrollView();
            }
            ShowEncryptionText(TxtStr);
        }
        else
        {
            GUILayout.Label("请输入需要加密的内容");
            InputStr = EditorGUILayout.TextArea(InputStr, GUILayout.Height(80));
            ShowEncryptionText(InputStr);
        }



    }
    /// <summary>
    /// 获取路径下txt的内容
    /// </summary>
    private void GetPathTxt()
    {
        if (!string.IsNullOrEmpty(inputPath))
        {
            TxtStr = File.ReadAllText(inputPath);
        }
        else
        {
            TxtStr = null;
            Debug.LogError("请选择要加密的txt文本文件");
        }
    }
    StringBuilder stringBuilder = new StringBuilder();
    /// <summary>
    /// 显示加密后的内容
    /// </summary>
    private void ShowEncryptionText(string str)
    {
        if (string.IsNullOrEmpty(str))
        {
            return;
        }
        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("复制加密文本", width100, hight30))
        {
            UnityEngine.GUIUtility.systemCopyBuffer = stringBuilder.ToString(); ;
        }
        GUILayout.Label("加密后:");
        EditorGUILayout.EndHorizontal();
        Scroview = EditorGUILayout.BeginScrollView(Scroview, false, true, GUILayout.Height(100));
        stringBuilder.Clear();
        stringBuilder.Append( GUILayout.TextArea(StringEncryption.Encrypt(str)));
        EditorGUILayout.EndScrollView();
   
     
    }


}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值