RSA 典型非对称加密算法,既可用于数据加密,也可用于数字签名.
"私钥加密,公钥解密" "公钥加密,私钥解密"
package rsa;
import java.security.Key;
/*
* KeyFactory用来生成公钥或私钥,或者说通过密钥规范还原密钥
*/
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;
/*
* spec:规格,说明
* java.security.spec 提供了密钥规范和算法参数规范的类和接口
* 以下两个类都是EncodedKeySpec的子类
* X509EncodedKeySpec用于转换公钥编码密钥, 以编码格式来表示公钥,以X.509标准作为密钥规范管理的编码格式
* PKCS8EncodedKeySpec用于转换私钥编码密钥,以编码格式来表示私钥,以PKCS#8标准作为密钥规范管理的编码格式
*
*/
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
public class RSACoder {
//非对称加密算法
public static final String KRY_ALGORITHM = "RSA";
//公钥
private static final String PUBLIC_KEY = "RSAPublicKey";
//私钥
private static final String PRIVATE_KEY = "RSAPrivatekey";
/**
* 密钥长度
* 默认1024位
* 密钥长度必须为64的倍数
* 范围在512-65536之间
*/
private static final int KEY_SIZE = 1024+256;
/**
* 加解密流程:
* PKCS8EncodedKeySpec或X509EncodedKeySpec转换密钥材料
* 根据加密算法实例化工厂,
* 利用工厂生成密钥
* 实例化Cipher,并传入加密算法
* 对数据进行加解密
* @param data
* @param key
* @return
* @throws Exception
*/
//私钥解密
public static byte[] decryptByPrivateKey(byte[] data,byte[] key) throws Exception{
//实例化PKCS8EncodedKeySpec对象,由私钥密钥字节数组获得密钥规范(实际使用过程中密钥以字节数组的形式保存和传输)
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
//实例化KeyFactory,并指定算法
KeyFactory keyFactory = KeyFactory.getInstance(KRY_ALGORITHM);
//生成私钥
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
//对数据解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE,privateKey);
return cipher.doFinal(data);
}
//公钥解密
public static byte[] decryptByPublicKey(byte[] data,byte[] key)throws Exception{
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KRY_ALGORITHM);
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
//公钥加密
public static byte[] encryptByPublicKey(byte[] data,byte[] key)throws Exception{
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KRY_ALGORITHM);
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
//私钥加密
public static byte[] encryptByPrivateKey(byte[] data,byte[] key)throws Exception{
PKCS8EncodedKeySpec pkcsKeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KRY_ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(pkcsKeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
public static byte[] getPrivateKey(Map<String,Object> keyMap){
Key key = (Key) keyMap.get(PRIVATE_KEY);
return key.getEncoded();
}
public static byte[] getPublicKey(Map<String,Object> keyMap){
Key key = (Key) keyMap.get(PUBLIC_KEY);
return key.getEncoded();
}
/**
* 初始化密钥
*
*/
public static Map<String, Object> initKey()throws Exception{
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KRY_ALGORITHM);
keyPairGen.initialize(KEY_SIZE);
KeyPair keyPair = keyPairGen.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;
}
}