“DON'TFORGET19371213”!,中国人不会忘记这个苦难的日子!
具体步骤,之前写的推文有。
写的时间不长,在下班的空余时间写的,当跑解密方法的时候,如果字符串较长,这个方法效率就低下了,周末再改改吧,还有输入要解密的密文的时候,记得在最后加入“|”字符,也算是一个Bug吧。
运行:
?
?
?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Come : MonoBehaviour
{
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);//解密按钮监听
}
void JiaMi()
{
//获取输入的字符串
var m_Mingwen = GameObject.Find("InputField (1)").gameObject.transform.GetComponentInChildren<InputField>().text;
//部分摩斯密码字典
Dictionary<string, string> mingwen = new Dictionary<string, string>()
{
{ "A",".—"},
{ "B","—..."},
{ "C","—.—."},
{ "D","—.."},
{ "E","."},
{ "F","..—."},
{ "G","— —."},
{ "H","...."},
{ "I",".."},
{ "J",".— — —"},
{ "K","—.—"},
{ "L",".—.."},
{ "M","— —"},
{ "N","—."},
{ "O","— — —"},
{ "P",".— —."},
{ "Q","— —.—"},
{ "R",".—."},
{ "S","..."},
{ "T","—"},
{ "U","..—"},
{ "V","...—"},
{ "W",".— —"},
{ "X","—..—"},
{ "Y","—.— —"},
{ "Z","— —.."},
{ "0","— — — — —"},
{ "1",".— — — —"},
{ "2","..— — —"},
{ "3","...— —"},
{ "4","....—"},
{ "5","....."},
{ "6","—...."},
{ "7","— —..."},
{ "8","— — —.."},
{ "9","— — — —."},
{ "'",". — — —."},
{ " ",null},
};
List<string> miwen = new List<string>();
foreach (char c in m_Mingwen)
{
mingwen.TryGetValue(c.ToString(), out string value);//第一次查找输入的密文在明文字典中的value
miwen.Add(value);//空列表添加value
if (value != null)
{
string lastMiwen = string.Join("|", miwen);//实例一个字符串添加列表中的值
m_Miwen.GetComponent<Text>().text = lastMiwen;//赋值给Unity文本内容
}
}
}
void JieMi()
{
var m_Mingwen = GameObject.Find("InputField (1)").gameObject.transform.GetComponentInChildren<InputField>().text;
Dictionary<string, string> mingwen = new Dictionary<string, string>()
{
{ ".—|","A"},
{ "—...|","B"},
{ "—.—.|","C"},
{ "—..|","D"},
{ ".|","E"},
{ "..—.|","F"},
{ "— —.|","G"},
{ "....|","H"},
{ "..|","I"},
{ ".— — —|","J"},
{ "—.—|","K"},
{ ".—..|","L"},
{ "— —|","M"},
{ "—.|","N"},
{ "— — —|","O"},
{ ".— —.|","P"},
{ "— —.—|","Q"},
{ ".—.|","R"},
{ "...|","S"},
{ "—|","T"},
{ "..—|","U"},
{ "...—|","V"},
{ ".— —|","W"},
{ "—..—|","X"},
{ "—.— —|","Y"},
{ "— —..|","Z"},
{ "— — — — —|","0"},
{ ".— — — —|","1"},
{ "..— — —|","2"},
{ "...— —|","3"},
{ "....—|","4"},
{ ".....|","5"},
{ "—....|","6"},
{ "— —...|","7"},
{ "— — —..|","8"},
{ "— — — —.|","9"},
{ " |"," "},
{ ". — — —.|","'"},
};
List<string> miwen = new List<string>();
List<string> miwen_1 = new List<string>();
foreach (char c in m_Mingwen)
{
miwen_1.Add(c.ToString());
string lastMiwen_1 = string.Join("", miwen_1);
mingwen.TryGetValue(lastMiwen_1.ToString(), out string value);//第一次查找输入的密文在明文字典中的value
miwen.Add(value);//空列表添加value
if (value!=null)
{
miwen_1.Clear();
}
string lastMiwen = string.Join("", miwen);//实例一个字符串添加列表中的值
m_Miwen_1.GetComponent<Text>().text = lastMiwen;//赋值给Unity文本内容
}
}
}
|