以个实现网卡加密的算法
/** 创建日期 2010-2-6
*/
import *;
import java.util.*;
import java.util.regex.*;
class HardWareID {
String IPCONFIG_COMMAND_WIN = "ipconfig /all";
boolean realMac = true;
String unique = "";
public static String getMacAddress() {
HardWareID hwid = new HardWareID();
return hwid.getUnique().trim();
}
private String getUnique() {
String os = System.getProperty("os.name");
if (os.startsWith("Windows")) {
return getUniqueWindows();
}else {
return "";
}
}
private String getUniqueWindows() {
// 运行控制台命令,返回结果字符串
String ipConfigResponse = null;
try {
ipConfigResponse = runConsoleCommand(IPCONFIG_COMMAND_WIN);
}
catch (IOException e) {
e.printStackTrace();
}
// 按行分割结果字符串,并循环判断每个字符串直道找出 Mac 地址
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
// 获取每行 ":" 后的字符串
int macAddressPosition = line.indexOf(":");
if (macAddressPosition <= 0) {
continue;
}
String macAddressCandidate = line.substring(macAddressPosition + 1).
trim();
// 检查 macAddressCandidate 中内容是否为真实网卡 Mac 地址,根据 Mac 地址计算出唯一标识。
if (isMacAddWin(macAddressCandidate)) {
if (realMac == true) {
generateUnique(macAddressCandidate);
}
else {
realMac = true;
}
}
}
return unique;
}
/**
* 运行控制台命令,返回结果字符串
* @param command String
* @return String
* @throws IOException
*/
private String runConsoleCommand(String command) throws IOException {
Process p = Runtime.getRuntime().exec(command);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
while (true) {
int c = stdoutStream.read();
if (c == -1) {
break;
}
buffer.append( (char) c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/**
* 对输入参数进行检查,符合正则表达式的为 windows 平台下有效 Mac 地址
* @param macAddressCandidate String
* @return boolean
*/
private boolean isMacAddWin(String macAddressCandidate) {
Pattern macPattern = ("[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}");
Matcher m = macPattern.matcher(macAddressCandidate);
return m.matches();
}
/**
* 对输入参数进行检查,符合长度的为 MAC OS X 下有效网卡 Mac 地址
* @param macAddressCandidate String
* @return boolean
*/
private boolean isMacAddOSX(String macAddressCandidate) {
if (macAddressCandidate.length() != 17) {
return false;
}
else {
return true;
}
}
/**
* 产生 Unique
* @param macAddress String
*/
private void generateUnique(String macAddress) {
if (unique == "") {
unique += macAddress;
}
else {
unique += "#";
unique += macAddress;
}
}
}
class RSAcardID {
protected int li_e=3, li_d=7, li_n=33,lul_temp =0;
// li_e 设置指数e,加密密钥 ,li_d设置指数d,解密密钥, li_n设置N:两个素数得乘积
public String ls_str; //网卡号对应的十进制数(加密的明文字符串)
public String sle_textMW=""; //用来存放密文
public String sle_textSW="";//用来存放明文
public RSAcardID(String CardID) {
ls_str=CardID;
}
public String cardIDJiaMI(){
try{
long lul_x, lul_y ;// lul_x: 加密明文; lul_y: 加密密文
do
{
if(ls_str.length()>=2)
{
lul_temp = Integer.parseInt(ls_str.substring(0,2)) ;
if ( lul_temp >= li_n ) // 将明文分组,且每组均小于N(N=33)
{
lul_temp = Integer.parseInt(ls_str.substring(0,1)) ;
ls_str =ls_str.substring(1);
}
else
ls_str = ls_str.substring(2);
}
else
{
lul_temp = Integer.parseInt(ls_str);
ls_str=null;
}
lul_y = 1 ;
//System.out.print("lul_tempJ="+lul_temp);
//System.out.println(" ls_strJ="+ls_str);
for (int j = 1;j<= li_e ; j++ ) // 进行乘方运算
lul_y = lul_y * lul_temp ;
lul_y = lul_y%33;// 根据加密公式计算密文
sle_textMW = sle_textMW + lul_y ; // sle_text中存放的是加密后的密文
} while(ls_str!=null);
System.out.println("密文:"+sle_textMW);
}catch(Exception NumberFormatException){};
return sle_textMW;
}
public String cardIDJieMI(){
try{
long lul_x0,lul_x1;
ls_str=sle_textMW;
do
{
if(ls_str.length()>=2)
{
lul_temp = Integer.parseInt(ls_str.substring(0,2)) ;
if ( lul_temp >= li_n ) // 将明文分组,且每组均小于N(N=33)
{
lul_temp = Integer.parseInt(ls_str.substring(0,1)) ;
ls_str =ls_str.substring(1);
}
else
ls_str = ls_str.substring(2);
}
else
{
lul_temp = Integer.parseInt(ls_str);
ls_str=null;
}
//System.out.print("lul_tempS="+lul_temp);
// System.out.print(" ls_strS="+ls_str);
lul_x0=1; lul_x1=1 ;
// 假如解密密钥是7,则先进行数的4次方运算取模,在进行数的3次方运算取模
for (int k = 1 ;k<4;k++)
lul_x0 = lul_x0 * lul_temp ;
lul_x0 =lul_x0%33 ;
for(int j= 1; j<=li_d-3;j++)
lul_x1 = lul_x1 * lul_temp ;
lul_x1 =lul_x1%33 ;
lul_x0 =(lul_x0 * lul_x1)%33; // 根据解密公式计算明文
sle_textSW = sle_textSW + lul_x0; // sle_tex中存放的是解密后的明文
//System.out.print(" lul_x ="+ lul_x0);
//System.out.println(" Ssle_text="+sle_textSW);
} while(ls_str!=null);
System.out.println("明文:"+sle_textSW);
}catch(Exception NumberFormatException){};
return sle_textSW;
}
}
public class CardID {
public static void main(String[] args) {
String cardID=HardWareID.getMacAddress();
System.out.println(cardID);
RSAcardID k=new RSAcardID("46791184320");
k.cardIDJiaMI(); k.cardIDJieMI();
}
}