RSA加密解密

来源:互联网 发布:电视write mac address 编辑:程序博客网 时间:2024/05/17 00:09


public static final String KEY_ALGORTHM = "RSA";


/**
* 私钥解密

* @param data
* @param key
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
*/
public static String decryptByPrivateKey(byte[] data, String key) {
try {
byte[] keyBytes = Base64Coder.decode(key);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
// 根据公钥,对Cipher对象进行初始化
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] resultBytes = cipher.doFinal(data);
return new String(resultBytes);


}
catch (InvalidKeySpecException e) {
LOGGER.error("RSA InvalidKeySpecException:", e);
}
catch (NoSuchAlgorithmException e) {
LOGGER.error("RSA NoSuchAlgorithmException:", e);
}
catch (IllegalBlockSizeException e) {
LOGGER.error("RSA IllegalBlockSizeException:", e);
}
catch (BadPaddingException e) {
LOGGER.error("RSA BadPaddingException:", e);
}
catch (NoSuchPaddingException e) {
LOGGER.error("RSA NoSuchPaddingException:", e);
}
catch (InvalidKeyException e) {
LOGGER.error("RSA InvalidKeyException:", e);
}
return null;
}


/**
* 用公钥加密

* @param data
*            加密数据
* @param key
*            密钥
* @return
* @throws Exception
*/
public static byte[] encryptByPublicKey(byte[] data, String key) throws Exception {
// 对公钥解密
byte[] keyBytes = Base64Coder.decode(key);
// 取公钥
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM);
Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);


// 对数据解密
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}


/**
* 用私钥加密
* @param data
*            加密数据
* @param key
*            密钥
* @return
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] data, String key) throws Exception {
// 解密密钥
byte[] keyBytes = Base64Coder.decode(key);
// 取私钥
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM);
Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
// 对数据加密
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data);
}


/**
* 用公钥解密

* @param data
*            加密数据
* @param key
*            密钥
* @return
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] data, String key) throws Exception {
// 对私钥解密
byte[] keyBytes = Base64Coder.decode(key);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM);
Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);


// 对数据解密
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, publicKey);


return cipher.doFinal(data);
}


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


/**
* 初始化密钥

* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORTHM);
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();


Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}


/**
* 取得公钥,并转化为String类型

* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
byte[] encoded = key.getEncoded();
return Base64Coder.encode2String(encoded);
}


/**
* 取得私钥,并转化为String类型

* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
byte[] encoded = key.getEncoded();
return Base64Coder.encode2String(encoded);
}
原创粉丝点击