工作中用到的加密算法。
/**
* 加密网卡MAC地址
*
* @param mac 网卡MAC地址
* @return 加密后的网卡MAC地址
*/
public static String encryptMac(String mac) {
StringBuffer buffer = new StringBuffer();
String[] segments = mac.split("-");
Random random = new Random();
for (String segment : segments) {
buffer.append("-");
for (int i = 0; i < segment.length(); i++) {
int rand = random.nextInt(10);
buffer.append((char)('A' + rand));
int initValue = String2Integer(segment.substring(i, i + 1), 16);
initValue ^= rand;
buffer.append((char)('A' + initValue));
}
}
return buffer.length() > 0 ? buffer.substring(1) : "";
}
/**
* 解密网卡MAC地址
*
* @param encryptMac 加密后的网卡MAC地址
* @return 网卡MAC地址
*/
private static String decryptMac(String encryptMac) {
StringBuffer buffer = new StringBuffer();
String[] segments = encryptMac.split("-");
for (String segment : segments) {
buffer.append("-");
for (int i = 0; i + 1 < segment.length(); i++) {
int rand = segment.charAt(i) - 'A';
int next = segment.charAt(++i) - 'A';
int originVal = next ^ rand;
buffer.append(Integer.toHexString(originVal));
}
}
System.out.println(buffer);
return buffer.length() > 0 ? buffer.substring(1) : "";
}
加密方法测试:decrpyt("00-21-9b-3d-29-0c")
解密方法测试:不解释
/**
* 加密网卡MAC地址
*
* @param mac 网卡MAC地址
* @return 加密后的网卡MAC地址
*/
public static String encryptMac(String mac) {
StringBuffer buffer = new StringBuffer();
String[] segments = mac.split("-");
Random random = new Random();
for (String segment : segments) {
buffer.append("-");
for (int i = 0; i < segment.length(); i++) {
int rand = random.nextInt(10);
buffer.append((char)('A' + rand));
int initValue = String2Integer(segment.substring(i, i + 1), 16);
initValue ^= rand;
buffer.append((char)('A' + initValue));
}
}
return buffer.length() > 0 ? buffer.substring(1) : "";
}
/**
* 解密网卡MAC地址
*
* @param encryptMac 加密后的网卡MAC地址
* @return 网卡MAC地址
*/
private static String decryptMac(String encryptMac) {
StringBuffer buffer = new StringBuffer();
String[] segments = encryptMac.split("-");
for (String segment : segments) {
buffer.append("-");
for (int i = 0; i + 1 < segment.length(); i++) {
int rand = segment.charAt(i) - 'A';
int next = segment.charAt(++i) - 'A';
int originVal = next ^ rand;
buffer.append(Integer.toHexString(originVal));
}
}
System.out.println(buffer);
return buffer.length() > 0 ? buffer.substring(1) : "";
}
加密方法测试:decrpyt("00-21-9b-3d-29-0c")
解密方法测试:不解释