GetAndroidSNIEMI.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using UnityEngine;
  8. public class GetAndroidSNIEMI : MonoBehaviour
  9. {
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. //AndroidJavaObject tm = new AndroidJavaObject("android.telephony.TelephonyManager");
  14. //string imei = tm.Call<string>("getDeviceId");
  15. //Debug.Log("IMEI: " + imei);
  16. // UILogManager.Instance.text3.text += tm;
  17. //AndroidJavaObject jo = new AndroidJavaObject("android.os.Build");
  18. //string serial = jo.GetStatic<string>("SERIAL");
  19. //Debug.Log("本机的SN码为:" + serial);
  20. // UILogManager.Instance.text3.text = serial;
  21. StartCoroutine(PutDeviceID());
  22. }
  23. private IEnumerator PutDeviceID()
  24. {
  25. yield return new WaitForSeconds(3);
  26. string id = GetDeviceUniqueIdMD5();
  27. Debug.Log("DeviceId " + id);
  28. UILogManager.Instance.text3.text = id;
  29. }
  30. public static string GetDeviceUniqueIdMD5()
  31. {
  32. string id = SystemInfo.deviceUniqueIdentifier;
  33. //return Encrypt(id, Encoding.UTF8);
  34. return EncryptMD5_16(id);
  35. }
  36. /// <summary>
  37. /// 获取字符串MD5值
  38. /// </summary>
  39. /// <param name="sText"></param>
  40. /// <param name="encoding">编码方式 utf8</param>
  41. /// <param name="uppercase">true 大写;false 小写</param>
  42. /// <returns></returns>
  43. private static string Encrypt(string sText, Encoding encoding, bool uppercase = false)
  44. {
  45. byte[] buffer = encoding.GetBytes(sText);
  46. string s = Encrypt(buffer);
  47. //if (!uppercase)
  48. // s = s.ToLower();
  49. return s;
  50. }
  51. /// <summary>
  52. /// 获取字符串MD5值
  53. /// </summary>
  54. /// <param name="buffer"></param>
  55. /// <returns></returns>
  56. private static string Encrypt(byte[] buffer)
  57. {
  58. //使用MD5这个抽象类的Creat()方法创建一个虚拟的MD5类的对象。
  59. using (MD5 md5 = MD5.Create())
  60. {
  61. //使用MD5实例的ComputerHash()方法处理字节数组。
  62. byte[] bufferNew = md5.ComputeHash(buffer);
  63. string s = BitConverter.ToString(bufferNew, 0, bufferNew.Length);
  64. return s.Replace("-", "");
  65. }
  66. }
  67. /// <summary>
  68. /// 获取流MD5值
  69. /// </summary>
  70. /// <param name="inputStream"></param>
  71. /// <returns></returns>
  72. private static string Encrypt(Stream inputStream)
  73. {
  74. using (MD5 mi = MD5.Create())
  75. {
  76. //开始加密
  77. byte[] bufferNew = mi.ComputeHash(inputStream);
  78. string s = BitConverter.ToString(bufferNew, 0, bufferNew.Length);
  79. return s.Replace("-", "");
  80. }
  81. }
  82. /// <summary>
  83. /// MD5 16位加密
  84. /// </summary>
  85. /// <param name="_encryptContent">需要加密的内容</param>
  86. /// <returns></returns>
  87. private static string EncryptMD5_16(string _encryptContent)
  88. {
  89. var md5 = new MD5CryptoServiceProvider();
  90. string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(_encryptContent)), 4, 8);
  91. t2 = t2.Replace("-", "");
  92. return t2;
  93. }
  94. }