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);
}
阅读全文
0 0
- RSA加密、解密
- RSA加密解密
- 加密解密---------->RSA算法
- RSA 加密解密总结
- RSA加密解密-实例
- RSA简单加密解密
- RSA加密解密算法
- RSA加密解密算法
- java RSA加密解密
- Java RSA加密解密
- RSA加密解密操作
- RSA加密与解密
- Java RSA加密解密
- RSA加密解密
- java RSA 加密/解密
- RSA加密解密
- RSA加密和解密
- java RSA加密解密
- Linux 磁盘命令
- 入职10个月有感
- Codeforce A
- RESTClient插件
- 联合索引
- RSA加密解密
- mysql中You can’t specify target table for update in FROM clause错误解决方法
- python科学计算--核心工具包一瞥(三)(Ipython 及Ipython Notebook安装,win10,64)
- Mysql中key和index区别
- Android studio 中 65k 方法数解决办法
- IOS Block 的使用 weakSelf strongSelf
- 1278: 正弦和余弦
- LightOJ
- 数据应用达人之SQL基础教程分享11-Scalar函数