StrEncryUtils.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Text;
  3. namespace JiaZhiQuan.Common.Utils
  4. {
  5. public static class StrEncryUtils
  6. {
  7. /// <summary>
  8. /// 加密身份证号,只保留第一位和最后一位,中间替换成*
  9. /// </summary>
  10. /// <param name="id"></param>
  11. /// <returns></returns>
  12. public static string EncryUserIdNumber(this string id)
  13. {
  14. if (string.IsNullOrEmpty(id)) return "";
  15. int length = id.Length;
  16. StringBuilder sb = new StringBuilder();
  17. for (int i = 0; i < length - 2; i++)
  18. {
  19. sb.Append("*");
  20. }
  21. return $"{id.Substring(0, 1)}{sb}{id.Substring(length - 1, 1)}";
  22. }
  23. /// <summary>
  24. /// 产生随机字符串
  25. /// </summary>
  26. /// <returns>字符串位数</returns>
  27. public static string GetRandom(int length) {
  28. int number;
  29. char code;
  30. string checkCode = String.Empty;
  31. System.Random random = new Random();
  32. for (int i = 0; i < length ; i++) {
  33. number = random.Next();
  34. if (number % 2 == 0)
  35. code = (char)('0' + (char)(number % 10));
  36. else
  37. code = (char)('A' + (char)(number % 26));
  38. checkCode += code.ToString();
  39. }
  40. return checkCode;
  41. }
  42. }
  43. }