您的位置:首页 > 其它

RSA非对称加密网卡MAC

2016-02-20 15:11 441 查看
最近有个工作任务是要对产品的使用权限进行验证:产品只能运行在具有权限的某台服务器上,所以在产品服务器启动时需要根据服务器mac校验其是否具有使用权限。而权限文件需要通过加密后才能给客户。

1、获取用户计算机的mac地址

package com.mac;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class MacObtainUtil {

/**
* 获取计算机的所有网卡编号,十六进制字符串表示,每两个之间用“-”隔开
*
* @return
* @throws SocketException
*/
public static List<String> getComputerMac() throws SocketException {

List<String> macList = new ArrayList<>();

Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
StringBuffer sbf = new StringBuffer();
byte[] macs = en.nextElement().getHardwareAddress();
if (macs == null || macs.length == 0) {
continue;
}
for (byte b : macs) {
sbf.append(byteToHexStr(b));
sbf.append("-");
}
macList.add(sbf.substring(0, sbf.length() - 1));
}

return macList;
}

private static String byteToHexStr(byte b) {
StringBuffer s = new StringBuffer();
int v = b & 0xFF;
String hv = Integer.toHexString(v);

for (int i = 0; i < 2 - hv.length(); i++) {
s.append("0");
}

s.append(hv);
return s.toString();
}
}


2、RSA产生密钥对、加密数据以及解密数据的工具类

package com.rsa.encrypt;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

public class RSAUtil {

// 非对称密钥算法
public static final String KEY_ALGORITHM = "RSA";

/**
* 密钥长度,DH算法的默认密钥长度是1024 密钥长度必须是64的倍数,在512到65536位之间
*/
private static final int KEY_SIZE = 512;
// 公钥
public static final String PUBLIC_KEY = "RSAPublicKey";

// 私钥
public static final String PRIVATE_KEY = "RSAPrivateKey";

/**
* <p>
* 创建秘钥对,保存在map中并返回map, key通过Base64加密
* <p>
* 公钥key RSAUtil.PUBLIC_KEY
* <p>
* 私钥key RSAUtil.PRIVATE_KEY
*
* @throws NoSuchAlgorithmException
*/
public static Map<String, String> createKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
// 初始化密钥生成器
keyPairGenerator.initialize(KEY_SIZE);
// 生成密钥对
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// 将密钥转换成字符串存储在map中
Map<String, String> keyMap = new HashMap<String, String>();
keyMap.put(PUBLIC_KEY, Base64.encodeBase64String(publicKey.getEncoded()));
keyMap.put(PRIVATE_KEY, Base64.encodeBase64String(privateKey.getEncoded()));

return keyMap;
}

/**
* 私钥加密
*
* @param data待加密数据
* @param key
*            密钥
* @return String 加密后数据
*/
public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception {

// 取得私钥
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
// 生成私钥
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
// 数据加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data);
}

/**
* 公钥解密
* @param data 待解密数据
* @param key 密钥
* @return byte[] 解密数据
* */
public static byte[] decryptByPublicKey(byte[] data,byte[] key) throws Exception{

//实例化密钥工厂
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
//初始化公钥
X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec(key);
//产生公钥
PublicKey pubKey=keyFactory.generatePublic(x509KeySpec);
//数据解密
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, pubKey);
return cipher.doFinal(data);
}
}


3、测试类

package test;

import java.util.Map;

import org.apache.commons.codec.binary.Base64;

import com.rsa.encrypt.RSAUtil;

public class RSATest {

public static void main(String[] args) {

String data = "123456789";

try {
Map<String, String> keyMap = RSAUtil.createKeyPair();
String publicKey = keyMap.get(RSAUtil.PUBLIC_KEY);
String privateKey = keyMap.get(RSAUtil.PRIVATE_KEY);

byte[] encryptData = RSAUtil.encryptByPrivateKey(Base64.decodeBase64(data),
Base64.decodeBase64(privateKey));
System.out.println("加密后的数据为:" + Base64.encodeBase64String(encryptData));
System.out.println("-------------------");

byte[] decryptData = RSAUtil.decryptByPublicKey(encryptData, Base64.decodeBase64(publicKey));
System.out.println("解密后的数据为:" + Base64.encodeBase64String(decryptData));

} catch (Exception e) {
e.printStackTrace();
}

}

}

控制台输出

加密后的数据为:IXKaLY8F4xmEhUVt7sitfQE3e+gtvo0PszAFx5GJ1pXWyQAQtF14EKSxtaMiRjbkq/8JSZRPa/fqbTM5dmF4ag==
----------------
解密后的数据为:12345678
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: