Tips(新手向,老手可直接略过):
1.安装Unity->添加许可证->新建一个3D项目->安装Vs 2019,配置基本环境。
2.在Hierarchy视图下右键选择Create Empty,在Project视图下的Assets目录里的Scenes文件夹中右键选择Create找到C# Script点击创建,改名为Come,将他挂在刚才创建的Hierarchy视图下的GameObject上(拖拽和add component都可以)。
3.在Hierarchy视图下右键选择UI点击Panel,右键点击刚创建的Panel选择UI->inputField,右键点击刚创建的Panel选择UI->Button,重命名为JiamiBtn,右键点击刚创建的Panel选择UI->Button,重命名为JiemiBtn,再按刚才的步骤添加text(一定要注意命名和我的完全一样,下图参考),画UI的时候记得把尺寸调大点不然可能出现显示不完全的Bug。
?
?
4.将我的源码复制到C#脚本里面(下方源码)
5.点击GameObject在右边查看挂上的脚本,并拖拽刚才画的UI到相应位置,(画重点,看清楚,名字一定要一一对应)。
6.点击运行。效果如下。
?
?
?源码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Come : MonoBehaviour
{
private int OffSet =5;//偏移量
public Button m_JiamiBtn;//加密按钮
public Button m_JiemiBtn;//解密按钮
public GameObject m_Miwen;//加密后的密文
public GameObject m_Miwen_1;//密文解密后的明文
void Update()
{
m_JiamiBtn.onClick.AddListener(JiaMi);//加密按钮监听
m_JiemiBtn.onClick.AddListener(Jiemi);//解密按钮监听
}
/// <summary>
/// 加密
/// </summary>
void JiaMi()
{
//获取输入的字符串
var m_Mingwen = GameObject.Find("InputField (1)").gameObject.transform.GetComponentInChildren<InputField>().text;
#region 明文字典
//明文字典
Dictionary<int, string> Mingwen = new Dictionary<int, string>(){
{ 0,"a"},
{ 1,"b"},
{ 2,"c"},
{ 3,"d"},
{ 4,"e"},
{ 5,"f"},
{ 6,"g"},
{ 7,"h"},
{ 8,"i"},
{ 9,"j"},
{ 10,"k"},
{ 11,"l"},
{ 12,"m"},
{ 13,"n"},
{ 14,"o"},
{ 15,"p"},
{ 16,"q"},
{ 17,"r"},
{ 18,"s"},
{ 19,"t"},
{ 20,"u"},
{ 21,"v"},
{ 22,"w"},
{ 23,"x"},
{ 24,"y"},
{ 25,"z"},
};
//明文字典_1
Dictionary<string, int> Mingwen_1 = new Dictionary<string, int>(){
{ "a",0},
{ "b",1},
{ "c",2},
{ "d",3},
{ "e",4},
{ "f",5},
{ "g",6},
{ "h",7},
{ "i",8},
{ "j",9},
{ "k",10},
{ "l",11},
{ "m",12},
{ "n",13},
{ "o",14},
{ "p",15},
{ "q",16},
{ "r",17},
{ "s",18},
{ "t",19},
{ "u",20},
{ "v",21},
{ "w",22},
{ "x",23},
{ "y",24},
{ "z",25},
};
#endregion
//空列表用来存储密文
List<string> Miwen = new List<string>();
//遍历输入的字符串
foreach (char c in m_Mingwen)
{
Mingwen_1.TryGetValue(c.ToString(), out int value);//第一次查找输入的密文在明文字典中的value
var lastvalue = value + OffSet;//加上偏移量
Mingwen.TryGetValue(lastvalue, out string endValue);//第二次查找在明文字典中的value对应明文字典_1中的value
if (endValue == null)
{
lastvalue -= 26;
Mingwen.TryGetValue(lastvalue, out string changeValue);
Miwen.Add(changeValue);//空列表添加changeValue
}
Miwen.Add(endValue);//空列表添加endvalue
string lastMiwen = string.Join("", Miwen);//实例一个字符串添加列表中的值
m_Miwen.GetComponent<Text>().text = lastMiwen;//赋值给Unity文本内容
Debug.Log(lastMiwen);
}
Debug.Log("123");
}
/// <summary>
/// 解密
/// </summary>
void Jiemi()
{
//获取输入的字符串
var m_Mingwen = GameObject.Find("InputField (1)").gameObject.transform.GetComponentInChildren<InputField>().text;
#region 明文字典
//明文字典
Dictionary<int, string> Mingwen = new Dictionary<int, string>(){
{ 0,"a"},
{ 1,"b"},
{ 2,"c"},
{ 3,"d"},
{ 4,"e"},
{ 5,"f"},
{ 6,"g"},
{ 7,"h"},
{ 8,"i"},
{ 9,"j"},
{ 10,"k"},
{ 11,"l"},
{ 12,"m"},
{ 13,"n"},
{ 14,"o"},
{ 15,"p"},
{ 16,"q"},
{ 17,"r"},
{ 18,"s"},
{ 19,"t"},
{ 20,"u"},
{ 21,"v"},
{ 22,"w"},
{ 23,"x"},
{ 24,"y"},
{ 25,"z"},
};
//明文字典_1
Dictionary<string, int> Mingwen_1 = new Dictionary<string, int>(){
{ "a",0},
{ "b",1},
{ "c",2},
{ "d",3},
{ "e",4},
{ "f",5},
{ "g",6},
{ "h",7},
{ "i",8},
{ "j",9},
{ "k",10},
{ "l",11},
{ "m",12},
{ "n",13},
{ "o",14},
{ "p",15},
{ "q",16},
{ "r",17},
{ "s",18},
{ "t",19},
{ "u",20},
{ "v",21},
{ "w",22},
{ "x",23},
{ "y",24},
{ "z",25},
};
#endregion
//空列表用来存储明文
List<string> Miwen = new List<string>();
//遍历输入的字符串
foreach (char c in m_Mingwen)
{
Mingwen_1.TryGetValue(c.ToString(), out int value);//第一次查找输入的密文在明文字典中的value
var lastvalue = value - OffSet;//减去偏移量
Mingwen.TryGetValue(lastvalue, out string endValue);//第二次查找在明文字典中的value对应明文字典_1中的value
//解决超出范围Bug
if (endValue == null)
{
lastvalue += 26;
Mingwen.TryGetValue(lastvalue, out string changeValue);
Miwen.Add(changeValue);//空列表添加changeValue
}
Miwen.Add(endValue);//空列表添加endvalue
string lastMiwen = string.Join("", Miwen);//实例一个字符串添加列表中的值
m_Miwen_1.GetComponent<Text>().text = lastMiwen;//赋值给Unity文本内容
Debug.Log(lastMiwen);
}
}
}
?遇到问题请在下方留言区留言,也欢迎各路大神指指点点,具体算法如何实现代码里面的注释打的很详细了,自己看,要改约定的公钥直接在代码里面改初始值即可,或者你想搞智能点也可以再画一个InputField,按照代码里面一样的方法获取到他的文本内容,再赋值给offset,就可以实时改变公钥了。
|