GetAndroidSNIEMI.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "3AB5B554F94FAA8E";
  35. // return EncryptMD5_16(id);
  36. }
  37. /// <summary>
  38. /// 获取字符串MD5值
  39. /// </summary>
  40. /// <param name="sText"></param>
  41. /// <param name="encoding">编码方式 utf8</param>
  42. /// <param name="uppercase">true 大写;false 小写</param>
  43. /// <returns></returns>
  44. private static string Encrypt(string sText, Encoding encoding, bool uppercase = false)
  45. {
  46. byte[] buffer = encoding.GetBytes(sText);
  47. string s = Encrypt(buffer);
  48. //if (!uppercase)
  49. // s = s.ToLower();
  50. return s;
  51. }
  52. /// <summary>
  53. /// 获取字符串MD5值
  54. /// </summary>
  55. /// <param name="buffer"></param>
  56. /// <returns></returns>
  57. private static string Encrypt(byte[] buffer)
  58. {
  59. //使用MD5这个抽象类的Creat()方法创建一个虚拟的MD5类的对象。
  60. using (MD5 md5 = MD5.Create())
  61. {
  62. //使用MD5实例的ComputerHash()方法处理字节数组。
  63. byte[] bufferNew = md5.ComputeHash(buffer);
  64. string s = BitConverter.ToString(bufferNew, 0, bufferNew.Length);
  65. return s.Replace("-", "");
  66. }
  67. }
  68. /// <summary>
  69. /// 获取流MD5值
  70. /// </summary>
  71. /// <param name="inputStream"></param>
  72. /// <returns></returns>
  73. private static string Encrypt(Stream inputStream)
  74. {
  75. using (MD5 mi = MD5.Create())
  76. {
  77. //开始加密
  78. byte[] bufferNew = mi.ComputeHash(inputStream);
  79. string s = BitConverter.ToString(bufferNew, 0, bufferNew.Length);
  80. return s.Replace("-", "");
  81. }
  82. }
  83. /// <summary>
  84. /// MD5 16位加密
  85. /// </summary>
  86. /// <param name="_encryptContent">需要加密的内容</param>
  87. /// <returns></returns>
  88. private static string EncryptMD5_16(string _encryptContent)
  89. {
  90. var md5 = new MD5CryptoServiceProvider();
  91. string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(_encryptContent)), 4, 8);
  92. t2 = t2.Replace("-", "");
  93. return t2;
  94. }
  95. }