ChangePass.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using LitJson;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class ChangePass : MonoBehaviour
  9. {
  10. public GameObject savesuccess;
  11. public GameObject terror;
  12. public GameObject terror2;
  13. public GameObject terror3;
  14. public InputField inputold;
  15. public InputField inputnew1;
  16. public InputField inputnew2;
  17. private void OnEnable() {
  18. inputnew1.text="";
  19. inputnew2.text="";
  20. inputold.text="";
  21. terror.SetActive(false);
  22. terror2.SetActive(false);
  23. terror3.SetActive(false);
  24. }
  25. public static passcheck checkpass(string pass)
  26. {
  27. int ct =0;
  28. string msg = "密码格式缺少:";
  29. var pwdRegex1 = "(?=.*[0-9])"; // 数字
  30. Regex reg1 = new Regex(pwdRegex1);
  31. bool isMatch1 = reg1.IsMatch(pass);
  32. if(!isMatch1)
  33. {
  34. msg += "数字 ";
  35. }
  36. else
  37. {
  38. ct++;
  39. }
  40. var pwdRegex2 = "(?=.*[a-z])"; // 小写字母
  41. Regex reg2 = new Regex(pwdRegex2);
  42. bool isMatch2 = reg2.IsMatch(pass);
  43. if(!isMatch2)
  44. {
  45. msg += "小写字母 ";
  46. }
  47. else
  48. {
  49. ct++;
  50. }
  51. var pwdRegex3 = "(?=.*[A-Z])"; // 大写字母
  52. Regex reg3= new Regex(pwdRegex3);
  53. bool isMatch3 = reg3.IsMatch(pass);
  54. if(!isMatch3)
  55. {
  56. msg += "大写字母 ";
  57. }
  58. else
  59. {
  60. ct++;
  61. }
  62. var pwdRegex4 = "[!@#$%^&*(),.?\\\":{}|<>]"; // 特殊字符
  63. Regex reg4= new Regex(pwdRegex4);
  64. bool isMatch4 = reg4.IsMatch(pass);
  65. if(!isMatch4)
  66. {
  67. msg += "特殊字符 ";
  68. }
  69. else
  70. {
  71. ct++;
  72. }
  73. msg+="中"+(3-ct)+"个";
  74. if(pass.Length<8)
  75. {
  76. msg = "密码必须大于8位";
  77. }
  78. else if(ct>=3)
  79. {
  80. bool hasConsecutiveNumbersOrLetters = checkSame(pass);
  81. if (hasConsecutiveNumbersOrLetters)
  82. {
  83. ct=0;
  84. msg = "字符串包含连续的数字或字母。";
  85. }
  86. if(pass.Contains("oper")||pass.Contains("admin"))
  87. {
  88. ct=0;
  89. msg = "严禁在口令中包含英文单词或拼音";
  90. }
  91. if(pass.Contains("ftp@ftp.net"))
  92. {
  93. ct=0;
  94. msg = "严禁使用系统或应用的默认口令";
  95. }
  96. bool isTwoAdjacentKeys = IsTwoAdjacentKeys(pass);
  97. if(isTwoAdjacentKeys)
  98. {
  99. ct=0;
  100. msg = "位于键盘相邻位置的字符超过2位";
  101. }
  102. bool containsSimilar = ContainsSimilarString(pass);
  103. if(containsSimilar)
  104. {
  105. ct=0;
  106. msg = "含有与账号名、主机名、系统名、厂商名相同或相似的字符串";
  107. }
  108. bool isphone = NameJudgeFlage(pass,phoneNumber);
  109. bool isidCard = NameJudgeFlage(pass,idCard);
  110. if(isphone||isidCard)
  111. {
  112. ct=0;
  113. msg = "严禁含有与局房、人员姓名、生日、证件号码、电话号码、手机号码及门牌号码等";
  114. }
  115. }
  116. passcheck p= new passcheck();
  117. p.ct = ct;
  118. p.msg=msg;
  119. return p;
  120. }
  121. public class passcheck
  122. {
  123. public int ct;
  124. public string msg;
  125. }
  126. public void changepass()
  127. {
  128. terror.SetActive(false);
  129. terror2.SetActive(false);
  130. terror3.SetActive(false);
  131. int ct = checkpass(inputnew1.text).ct;
  132. if(inputold.text==inputnew1.text)
  133. {
  134. terror3.GetComponent<Text>().text = "新密码不能与旧密码一样";
  135. terror3.SetActive(true);
  136. return;
  137. }
  138. if(ct>=3&&inputnew1.text.Length>=8)
  139. {
  140. string oldpass = AesEncryption.Encrypt(inputold.text);
  141. string encrypted1 = AesEncryption.Encrypt(inputnew1.text);
  142. string encrypted2 = AesEncryption.Encrypt(inputnew2.text);
  143. if(encrypted1==encrypted2)
  144. {
  145. DataManager.Instance.setPass(oldpass,encrypted1,(msg) => {
  146. JsonData d = JsonMapper.ToObject(msg);
  147. if(d["data"]==null)
  148. {
  149. terror.SetActive(true);
  150. }
  151. else
  152. {
  153. terror2.SetActive(false);
  154. terror.SetActive(false);
  155. savesuccess.SetActive(true);
  156. Invoke("savecloes",2f);
  157. }
  158. });
  159. }
  160. else
  161. {
  162. terror2.SetActive(true);
  163. }
  164. }
  165. else
  166. {
  167. terror3.GetComponent<Text>().text = checkpass(inputnew1.text).msg;
  168. terror3.SetActive(true);
  169. }
  170. }
  171. private static readonly Regex _noConsecutiveNumbersRegex = new Regex(@"\d{3,}|[a-z]{3,}|[A-Z]{3,}");
  172. public static bool IsValidPassword(string password)
  173. {
  174. return _noConsecutiveNumbersRegex.IsMatch(password);
  175. }
  176. void savecloes()
  177. {
  178. savesuccess.SetActive(false);
  179. MainCenterManager.Instance.showMain();
  180. }
  181. static string NameJudge = "^[\u4e00-\u9fa5a-zA-Z0-9]{1,20}$";//字母、数字、汉字
  182. static string local = @"^[a-zA-Z]:((\\+[^\/:*?""<>|]+)+)\s*$"; //本地地址
  183. static string expression = "([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://|[wW]{3}.|[wW][aA][pP].|[fF][tT][pP].|[fF][iI][lL][eE].)[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";//网络地址
  184. static string email = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";//验证Email地址
  185. static string phoneNumber = @"^1(3[0-9]|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$";
  186. static string idCard = @"^\d{15}|\d{18}$";//验证身份证号(15位或18位数字)
  187. /// <summary>
  188. /// 正则表达式判断
  189. /// </summary>
  190. /// <param name="str">输入内容</param>
  191. /// <param name="regularExpression">正则表达式</param>
  192. /// <returns>是否符合输入格式</returns>
  193. public static bool NameJudgeFlage(string str, string regularExpression)
  194. {
  195. Regex reg = new Regex(regularExpression);//判断
  196. return reg.IsMatch(str);
  197. }
  198. // 定义键盘上的相邻字符
  199. private static readonly string[] adjacentKeys = new string[]
  200. {
  201. "qwertyuiop", "asdfghjkl", "zxcvbnm",
  202. "qaz", "wsx", "edc", "rfv", "tgb", "nhy", "ujm", "ik", "ol", "p;",
  203. "12", "34", "56", "78", "90",
  204. "QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM",
  205. "QAZ", "WSX", "EDC", "RFV", "TGB", "NHY", "UJM", "IK", "OL", "P;",
  206. "12", "34", "56", "78", "90","ab","cd","ef","gh","ij","kl","mn","op","qr","st","uv","wx","yz"
  207. };
  208. // 正则表达式匹配两个相邻键盘字符
  209. private static readonly string pattern = string.Join("|", adjacentKeys).Replace("q", "[qQ]").Replace("w", "[wW]").Replace("s", "[sS]").Replace("d", "[dD]");
  210. public static bool IsTwoAdjacentKeys(string input)
  211. {
  212. return Regex.IsMatch(input, pattern);
  213. }
  214. //不能相同字符(如111、aaa)连续3位或3位以上
  215. public static bool checkSame(string str) {
  216. Regex _checkSame = new Regex(@"/(\w)\1{3,}/");
  217. return _checkSame.IsMatch(str);
  218. }
  219. // 正则表达式匹配规则,可以根据需要进行调整
  220. private static readonly string pattern2 = @"(账号名|主机名|系统名|厂商名)";
  221. // 检查字符串是否包含特定的字符串
  222. public static bool ContainsSimilarString(string input)
  223. {
  224. return Regex.IsMatch(input, pattern2, RegexOptions.IgnoreCase);
  225. }
  226. }