RSA非对称加密技术(JAVA版实现加解密处理)介绍
非对称的加密技术就是指加密过程是不可逆,不能通过密文直接解密得到原文信息。RSA技术就是现在比较流行的非对称的加密技术,他实现主要通过公钥加密,解密的话需要私钥进行解密。Java的加解密具体实现如下:
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
/**
*
* RSA加密与解密工具类
*
* @author 李代锋
*
*
*/
public class RSAUtils {
/**
* 生成密钥对
*
* @param keySize
* 密钥长度
* @return
* @throws NoSuchAlgorithmException
*/
public static KeyPair generateKeyPair(int keySize) {
try {
KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
SecureRandom random =new SecureRandom();
//初始化密钥对生成器,密钥大小为${keySize}位
pairgen.initialize(keySize, random);
return pairgen.generateKeyPair();
}catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
/**
* 将密钥采用Base64加密并返回加密后的密文
*
* @param key
* 密钥
* @return
*/
public static String keyEncrypt(Key key) {
return new String(Base64.encodeBase64(key.getEncoded()));
}
/**
* 利用公钥进行加密
*
* @param str
* @param publicKey(encodeBase64后的公钥)
* @return
* @throws Exception
*/
public static String encrypt(String str, String publicKey)throws Exception {
// 对公钥进行base64编码
byte[] decoded = Base64.decodeBase64(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA")
.generatePublic(new X509EncodedKeySpec(decoded));
// RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
}
public static String encrypt(String str,byte[] decoded)throws Exception {
// 对公钥进行base64编码
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA")
.generatePublic(new X509EncodedKeySpec(decoded));
// RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
}
/**
* 直接利用公钥加密
*
* @param str
* 明文
* @param publicKey
* 公钥
* @return
* @throws Exception
*/
public static String encrypt(String str, Key publicKey)throws Exception {
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA")
.generatePublic(new X509EncodedKeySpec(publicKey.getEncoded()));
// RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
}
/**
* 利用私钥解密
*
* @param str
* 加密字符串
* @param privateKey
* 私钥(encodeBase64后的私钥)
* @return 明文
* @throws Exception
* 解密过程中的异常信息
*/
public static String decrypt(String str, String privateKey)throws Exception {
// 64位解码加密后的字符串
byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
// base64编码的私钥
byte[] decoded = Base64.decodeBase64(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA")
.generatePrivate(new PKCS8EncodedKeySpec(decoded));
// RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(inputByte));
}
public static String decrypt(String str,byte[] decoded)throws Exception {
// 64位解码加密后的字符串
byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA")
.generatePrivate(new PKCS8EncodedKeySpec(decoded));
// RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(inputByte));
}
/**
* 直接利用私钥解密
*
* @param str
* 密文
* @param privateKey
* 私钥
* @return
* @throws Exception
*/
public static String decrypt(String str, Key privateKey)throws Exception {
// 64位解码加密后的字符串
byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA")
.generatePrivate(new PKCS8EncodedKeySpec(privateKey.getEncoded()));
// RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(inputByte));
}
public static void main(String[] args) {
try {
String srcStr ="江山如此多娇,引无数程序员竞折腰,哈哈哈哈";
// 获取非对称密钥
KeyPair keyPair = RSAUtils.generateKeyPair(4192);
System.out.println("公钥:" +keyEncrypt(keyPair.getPublic()));
System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
System.out.println("私钥:" +keyEncrypt(keyPair.getPrivate()));
System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
String encriptMsg = RSAUtils.encrypt(srcStr, keyPair.getPublic());
System.out.println("原始信息:" + srcStr);
System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
System.out.println("加密后的密文为:"+encriptMsg);
System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
System.out.println("密文解密:" + RSAUtils.decrypt(encriptMsg, keyPair.getPrivate()));
}catch (Exception e) {
e.printStackTrace();
}
}
}
网友评论