根据网卡MAC地址,进行序列号生成.
改变char[] key 即可生成不同序列号
/**
*cxy1238
**/
package org.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demos {
public static void main(String args[]) {
SnMachine ss=new SnMachine();
ss.getSn();
}
}
class SnMachine {
private String sn;
private String tempStr = "";
private String tempNum = "";
private char[] key = { 'R', 'J', 'Q', 'X', 'W', 'M', 'E', 'Z', 'N', 'P' };
private int[] keyindex = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
private char[] numkey = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
private char[] filterStr = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z' };
private char[] filterStrtoNum = { '7', '3', '8', '1', '5', '8', '0', '4',
'2', '8', '2', '3', '5', '7', '3', '9', '5', '4', '1', '5', '6',
'4', '3', '2', '1', '9' };
/**
*
* @return
*/
public String getSn() {
try {
//过滤特殊字符
sn = getFilterSnOut(getMACAddress());
if (sn.equals("error")) {
return sn;
}
System.out.println("第一层过滤:"+sn);
sn = filter1(sn);
if (sn.equals("error")) {
return sn;
}
System.out.println("第二层过滤:"+sn);
sn = filter2(sn);
if (sn.equals("error")) {
return sn;
}
System.out.println("第三层过滤:"+sn);
sn = filter3(sn);
System.out.println("第四层过滤:"+sn);
sn=String.valueOf(filterhash(sn));
System.out.println("第五层过滤:"+sn);
sn=filterconvert(sn);
System.out.println("第六层过滤:"+sn);
return sn;
} catch (Exception ex) {
return "error";
}
}
/**
* 过滤不需要的符号
* @param 网卡MAC地址
* @return
*/
private String getFilterSnOut(String str) {
str = str.replace("-", "").replace(" ", "").toUpperCase();
if (str.length() != 12)// 判断长度是否大于12
{
return "error";
}
return str;
}
/**
* 过滤字母,将字母转换为数字
* 合并生成12位的数字
* @param parm
* @return
*/
private String filter1(String parm) {
int tempi = -1;
tempStr = "";
tempNum = "";
for (int i = 0; i < parm.length(); i++) {
tempi = -1;
for (int i2 = 0; i2 < filterStr.length; i2++) {
if (filterStr[i2] == parm.charAt(i)) {
tempi = i2;
}
}
if (tempi >= 0 && tempi <= 26) {
tempStr = tempStr + filterStrtoNum[tempi];
}
if (tempi == -1) {
tempNum = tempNum + parm.charAt(i);
}
}
tempStr = tempStr + tempNum;
if (tempStr.length() == 12)
return tempStr;
else
return "error";
}
/**
* 将String颠倒
* @param parm
* @return
*/
private String filter2(String parm) {
tempStr = "";
for (int i = parm.length() - 1; i >= 0; i--) {
tempStr = tempStr + parm.charAt(i);
}
return tempStr;
}
/**
* 生成key
* @param parm
* @return
*/
private String filter3(String parm) {
tempStr = "";
// 填写keyindex;
for (int i = 0; parm.length() > i; i++) {
for (int i2 = 0; numkey.length > i2; i2++) {
try {
Integer.valueOf(parm.charAt(i)).intValue();
} catch (Exception ex) {
return "error";
}
if (numkey[i2] == Integer.valueOf(parm.charAt(i)).intValue()) {
keyindex[i] = i2;
}
}
}
// 获取key
for (int i = 0; i < keyindex.length; i++) {
if (keyindex[i] != -1) {
tempStr = tempStr + key[keyindex[i]];
}
}
return tempStr;
}
/**
* hash转换
* @param s
* @return
*/
private int filterhash(String s) { //filter4
int i = 0 ;
char ac[] = s.toCharArray();
int j = 0 ;
for ( int k = ac.length; j < k; j ++ )
i = 31 * i + ac[j];
return Math.abs(i);
}
/**
* End filter
* @param s
* @return
*/
private String filterconvert(String s) { //end filter
if (s == null || s.length() == 0 )
return s;
byte abyte0[] = s.getBytes();
char ac[] = new char [s.length()];
int i = 0 ;
for ( int k = abyte0.length; i < k; i ++ ) {
int j = abyte0[i];
if (j >= 48 && j <= 57 )
j = ((j - 48 ) + 5 ) % 10 + 48 ;
else if (j >= 65 && j <= 90 )
j = ((j - 65 ) + 13 ) % 26 + 65 ;
else if (j >= 97 && j <= 122 )
j = ((j - 97 ) + 13 ) % 26 + 97 ;
ac[i] = ( char ) j;
}
return String.valueOf(ac);
}
/**
* 获取Mac地址
* @return
*/
private static String getMACAddress() {
String address = "";
String line="";
if (System.getProperty("os.name") != null && System.getProperty("os.name").startsWith("Windows")) {
try {
Process p = Runtime.getRuntime().exec("cmd.exe /c ipconfig /all");
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
}
}
line=null;
return address;
}
}
改变char[] key 即可生成不同序列号
/**
*cxy1238
**/
package org.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demos {
public static void main(String args[]) {
SnMachine ss=new SnMachine();
ss.getSn();
}
}
class SnMachine {
private String sn;
private String tempStr = "";
private String tempNum = "";
private char[] key = { 'R', 'J', 'Q', 'X', 'W', 'M', 'E', 'Z', 'N', 'P' };
private int[] keyindex = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
private char[] numkey = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
private char[] filterStr = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z' };
private char[] filterStrtoNum = { '7', '3', '8', '1', '5', '8', '0', '4',
'2', '8', '2', '3', '5', '7', '3', '9', '5', '4', '1', '5', '6',
'4', '3', '2', '1', '9' };
/**
*
* @return
*/
public String getSn() {
try {
//过滤特殊字符
sn = getFilterSnOut(getMACAddress());
if (sn.equals("error")) {
return sn;
}
System.out.println("第一层过滤:"+sn);
sn = filter1(sn);
if (sn.equals("error")) {
return sn;
}
System.out.println("第二层过滤:"+sn);
sn = filter2(sn);
if (sn.equals("error")) {
return sn;
}
System.out.println("第三层过滤:"+sn);
sn = filter3(sn);
System.out.println("第四层过滤:"+sn);
sn=String.valueOf(filterhash(sn));
System.out.println("第五层过滤:"+sn);
sn=filterconvert(sn);
System.out.println("第六层过滤:"+sn);
return sn;
} catch (Exception ex) {
return "error";
}
}
/**
* 过滤不需要的符号
* @param 网卡MAC地址
* @return
*/
private String getFilterSnOut(String str) {
str = str.replace("-", "").replace(" ", "").toUpperCase();
if (str.length() != 12)// 判断长度是否大于12
{
return "error";
}
return str;
}
/**
* 过滤字母,将字母转换为数字
* 合并生成12位的数字
* @param parm
* @return
*/
private String filter1(String parm) {
int tempi = -1;
tempStr = "";
tempNum = "";
for (int i = 0; i < parm.length(); i++) {
tempi = -1;
for (int i2 = 0; i2 < filterStr.length; i2++) {
if (filterStr[i2] == parm.charAt(i)) {
tempi = i2;
}
}
if (tempi >= 0 && tempi <= 26) {
tempStr = tempStr + filterStrtoNum[tempi];
}
if (tempi == -1) {
tempNum = tempNum + parm.charAt(i);
}
}
tempStr = tempStr + tempNum;
if (tempStr.length() == 12)
return tempStr;
else
return "error";
}
/**
* 将String颠倒
* @param parm
* @return
*/
private String filter2(String parm) {
tempStr = "";
for (int i = parm.length() - 1; i >= 0; i--) {
tempStr = tempStr + parm.charAt(i);
}
return tempStr;
}
/**
* 生成key
* @param parm
* @return
*/
private String filter3(String parm) {
tempStr = "";
// 填写keyindex;
for (int i = 0; parm.length() > i; i++) {
for (int i2 = 0; numkey.length > i2; i2++) {
try {
Integer.valueOf(parm.charAt(i)).intValue();
} catch (Exception ex) {
return "error";
}
if (numkey[i2] == Integer.valueOf(parm.charAt(i)).intValue()) {
keyindex[i] = i2;
}
}
}
// 获取key
for (int i = 0; i < keyindex.length; i++) {
if (keyindex[i] != -1) {
tempStr = tempStr + key[keyindex[i]];
}
}
return tempStr;
}
/**
* hash转换
* @param s
* @return
*/
private int filterhash(String s) { //filter4
int i = 0 ;
char ac[] = s.toCharArray();
int j = 0 ;
for ( int k = ac.length; j < k; j ++ )
i = 31 * i + ac[j];
return Math.abs(i);
}
/**
* End filter
* @param s
* @return
*/
private String filterconvert(String s) { //end filter
if (s == null || s.length() == 0 )
return s;
byte abyte0[] = s.getBytes();
char ac[] = new char [s.length()];
int i = 0 ;
for ( int k = abyte0.length; i < k; i ++ ) {
int j = abyte0[i];
if (j >= 48 && j <= 57 )
j = ((j - 48 ) + 5 ) % 10 + 48 ;
else if (j >= 65 && j <= 90 )
j = ((j - 65 ) + 13 ) % 26 + 65 ;
else if (j >= 97 && j <= 122 )
j = ((j - 97 ) + 13 ) % 26 + 97 ;
ac[i] = ( char ) j;
}
return String.valueOf(ac);
}
/**
* 获取Mac地址
* @return
*/
private static String getMACAddress() {
String address = "";
String line="";
if (System.getProperty("os.name") != null && System.getProperty("os.name").startsWith("Windows")) {
try {
Process p = Runtime.getRuntime().exec("cmd.exe /c ipconfig /all");
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
}
}
line=null;
return address;
}
}