文章转载至:https://blog.csdn.net/bashendixie5/article/details/123257332,做了一些修改
废话不多说,直接看代码;
package com.报路径;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
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.junit.Test;
import org.springframework.util.Base64Utils;
public class RSATest {
static class RSAUtil {
/**
* 算法
*/
public static final String KEY_ALGORITHM = "RSA";
/**
* 公钥
*/
private static String PUBLIC_KEY = "RSAPublicKey";
/**
* 私钥
*/
private static String PRIVATE_KEY = "RSAPrivateKey";
/**
* RSA密钥长度
* 默认1024位
* 密钥长度必须位64的倍数
* 范围在512-65536之间
*/
private static int KEY_SIZE = 512;
/**
* 设置密钥长度
* @param keySize
*/
public static void setKeySize(int keySize){
if(keySize > 512 && keySize%64 == 0){
KEY_SIZE = keySize;
} else if(keySize > 0 && keySize <1016){
KEY_SIZE = (8+KEY_SIZE)*64;
}
}
/**
* 设置私钥
* @param privateKey
*/
public static void setPrivateKey(String privateKey) {
PRIVATE_KEY = privateKey;
}
/**
* 设置公钥
* @param publicKey
*/
public static void setPublicKey(String publicKey) {
PUBLIC_KEY = publicKey;
}
/**
* 初始化密钥
* @return 密钥Map
*/
public static Map<String, Object> initKey() {
try {
// 实例化密钥对生成器
KeyPairGenerator gen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
// 初始化密钥对生成器
gen.initialize(KEY_SIZE);
// 生成密钥对
KeyPair keyPair = gen.generateKeyPair();
// 公钥
RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
// 私钥
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
// 封装密钥
Map<String, Object> map = new HashMap<String, Object>(2);
map.put(PUBLIC_KEY, rsaPublicKey);
map.put(PRIVATE_KEY, rsaPrivateKey);
return map;
} catch(Exception e) {
}
return null;
}
/**
* 取得公钥
* @param map
* @return
*/
public static byte[] getPublicKey(Map<String, Object> map) {
Key key = (Key)map.get(PUBLIC_KEY);
return key.getEncoded();
}
/**
* 取得公钥
* @param map
* @return
*/
public static byte[] getPrivateKey(Map<String, Object> map) {
Key key = (Key)map.get(PRIVATE_KEY);
return key.getEncoded();
}
/**
* 私钥解密
* @param data 待解密数据
* @param key 私钥
* @return byte[] 解密数据
*/
public static byte[] decryptByPrivateKey(byte[] data, byte[] key) {
try {
//取得私钥
PKCS8EncodedKeySpec pkcs = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//生成私钥
PrivateKey privateKey = keyFactory.generatePrivate(pkcs);
//对数据解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
} catch(Exception e) {
}
return null;
}
/**
* 公钥解密
* @param data
* @param key
* @return
*/
public static byte[] decryptByPublicKey(byte[] data, byte[] key) {
try {
//取得公钥
X509EncodedKeySpec pkcs = new X509EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//生成公钥
PublicKey publicKey = keyFactory.generatePublic(pkcs);
//对数据解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
} catch(Exception e) {
}
return null;
}
/**
* 公钥加密
* @param data
* @param key
* @return
*/
public static byte[] encryptByPublicKey(byte[] data, byte[] key) {
try {
//取得公钥
X509EncodedKeySpec pkcs = new X509EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//生成公钥
PublicKey publicKey = keyFactory.generatePublic(pkcs);
//对数据加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
} catch(Exception e) {
}
return null;
}
/**
* 私钥加密
* @param data
* @param key
* @return
*/
public static byte[] encryptByPrivateKey(byte[] data, byte[] key) {
try {
//取得私钥
PKCS8EncodedKeySpec pkcs = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//生成公钥
PrivateKey privateKey = keyFactory.generatePrivate(pkcs);
//对数据加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data);
} catch(Exception e) {
}
return null;
}
}
@Test
public void digest_RSA()
{
byte[] publicKey;
byte[] privateKey;
try {
RSAUtil.setKeySize(640);
RSAUtil.setPublicKey("公钥");
RSAUtil.setPrivateKey("私钥");
Map<String, Object> map = RSAUtil.initKey();
publicKey = RSAUtil.getPublicKey(map);
privateKey = RSAUtil.getPrivateKey(map);
System.out.println("公钥:"+ Base64Utils.encodeToString(publicKey));
System.out.println("私钥:"+ Base64Utils.encodeToString(privateKey));
String input = "这是待加密的数据";
byte[] data = input.getBytes();
//私钥加密,公钥解密
byte[] edata1 = RSAUtil.encryptByPrivateKey(data, privateKey);
System.out.println("私钥加密:"+ Base64Utils.encodeToString(edata1));
byte[] edata2 = RSAUtil.decryptByPublicKey(edata1, publicKey);
System.out.println("公钥解密:"+ new String(edata2));
//公钥加密,私钥解密
byte[] edata3 = RSAUtil.encryptByPublicKey(data, publicKey);
System.out.println("公钥加密:"+ Base64Utils.encodeToString(edata3));
byte[] edata4 = RSAUtil.decryptByPrivateKey(edata3, privateKey);
System.out.println("私钥解密:"+ new String(edata4));
} catch(Exception e) {
}
}
}
代码用于记录学习,无侵权意向,侵删!!!