using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; using System.Text; using UnityEngine; public class GetAndroidSNIEMI : MonoBehaviour { // Start is called before the first frame update void Start() { //AndroidJavaObject tm = new AndroidJavaObject("android.telephony.TelephonyManager"); //string imei = tm.Call("getDeviceId"); //Debug.Log("IMEI: " + imei); // UILogManager.Instance.text3.text += tm; //AndroidJavaObject jo = new AndroidJavaObject("android.os.Build"); //string serial = jo.GetStatic("SERIAL"); //Debug.Log("本机的SN码为:" + serial); // UILogManager.Instance.text3.text = serial; StartCoroutine(PutDeviceID()); } private IEnumerator PutDeviceID() { yield return new WaitForSeconds(3); string id = GetDeviceUniqueIdMD5(); Debug.Log("DeviceId " + id); UILogManager.Instance.text3.text = id; } public static string GetDeviceUniqueIdMD5() { string id = SystemInfo.deviceUniqueIdentifier; //return Encrypt(id, Encoding.UTF8); return "3AB5B554F94FAA8E"; // return EncryptMD5_16(id); } /// /// 获取字符串MD5值 /// /// /// 编码方式 utf8 /// true 大写;false 小写 /// private static string Encrypt(string sText, Encoding encoding, bool uppercase = false) { byte[] buffer = encoding.GetBytes(sText); string s = Encrypt(buffer); //if (!uppercase) // s = s.ToLower(); return s; } /// /// 获取字符串MD5值 /// /// /// private static string Encrypt(byte[] buffer) { //使用MD5这个抽象类的Creat()方法创建一个虚拟的MD5类的对象。 using (MD5 md5 = MD5.Create()) { //使用MD5实例的ComputerHash()方法处理字节数组。 byte[] bufferNew = md5.ComputeHash(buffer); string s = BitConverter.ToString(bufferNew, 0, bufferNew.Length); return s.Replace("-", ""); } } /// /// 获取流MD5值 /// /// /// private static string Encrypt(Stream inputStream) { using (MD5 mi = MD5.Create()) { //开始加密 byte[] bufferNew = mi.ComputeHash(inputStream); string s = BitConverter.ToString(bufferNew, 0, bufferNew.Length); return s.Replace("-", ""); } } /// /// MD5 16位加密 /// /// 需要加密的内容 /// private static string EncryptMD5_16(string _encryptContent) { var md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(_encryptContent)), 4, 8); t2 = t2.Replace("-", ""); return t2; } }