package test.加密;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @NAME: ChenSiYu
* @DATE: 2022/4/27
**/
public class 获取windows的mac地址 {
/**
* windows的
* 根据mac地址进行加密
* @param mac
* @return
* @throws NoSuchAlgorithmException
*/
public static final String getOnlyId(String mac) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] md5 = md.digest(mac.getBytes());
StringBuilder id = new StringBuilder();
for (int i = 0; i < md5.length; i++) {
String hex = Integer.toHexString(md5[i] & 0xFF);
if (hex.length() < 2) {
hex = "0" + hex;
}
id.append(hex);
}
return id.toString().toUpperCase();
}
/**
* windows
* 获取本机的mac地址
* @return 返回mac地址
* @throws IOException
*/
public static final String getMacAddress() throws IOException {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
byte[] ha = ni.getHardwareAddress();
StringBuilder mac = new StringBuilder();
for (int i = 0; i < ha.length; i++) {
String hex = Integer.toHexString(ha[i] & 0xFF);
if (hex.length() < 2) {
hex = "0" + hex;
}
mac.append(hex).append("-");
}
return mac.deleteCharAt(mac.length() - 1).toString().toUpperCase();
}
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
System.out.println(getOnlyId(getMacAddress()));
}
}
03-10
3169
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-07
120
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)