RSA 加密 (非對稱加密)
公鑰與私鑰為一組,保留好,即可一直加密解密
package com.marks.zweb.util.ras;
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* RSA非對稱加密算法工具類
*
* @author lixk
*/
public class RSAUtis {
//非對稱密鑰
private static final String KEY_ALGORITHM = "RSA";
//密鑰長度 512-65536位之間
private static final int KEY_SIZE = 512;
//字元編碼
private static final String CHARSET = "UTF-8";
/**
* 生成密鑰對
*
* @return KeyPair 密鑰對
*/
public static KeyPair getKeyPair() throws Exception {
return getKeyPair(null);
}
/**
* 生成密鑰對
* @param password 生成密鑰對的密碼
* @return
* @throws Exception
*/
private static KeyPair getKeyPair(String password) throws Exception {
//執行個體化密鑰生成器
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
//初始化密鑰生成器
if(password == null){
keyPairGenerator.initialize(KEY_SIZE);
}else {
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes(CHARSET));
keyPairGenerator.initialize(KEY_SIZE, secureRandom);
}
//生成密鑰對
return keyPairGenerator.generateKeyPair();
}
/**
* 取得私鑰
*
* @param keyPair 密鑰對
* @return byte[] 私鑰
*/
private static byte[] getPrivateKeyBytes(KeyPair keyPair) {
return keyPair.getPrivate().getEncoded();
}
/**
* 取得Base64編碼的私鑰
*
* @param keyPair 密鑰對
* @return String Base64編碼的私鑰
*/
public static String getPrivateKey(KeyPair keyPair) {
return Base64.getEncoder().encodeToString(getPrivateKeyBytes(keyPair));
}
/**
* 取得公鑰
*
* @param keyPair 密鑰對
* @return byte[] 公鑰
*/
private static byte[] getPublicKeyBytes(KeyPair keyPair) {
return keyPair.getPublic().getEncoded();
}
/**
* 取得Base64編碼的公鑰
*
* @param keyPair 密鑰對
* @return String Base64編碼的公鑰
*/
public static String getPublicKey(KeyPair keyPair) {
return Base64.getEncoder().encodeToString(getPublicKeyBytes(keyPair));
}
/**
* 私鑰加密
*
* @param data 待加密資料
* @param privateKey 私鑰位元組數組
* @return byte[] 加密資料
*/
private static byte[] encryptByPrivateKey(byte[] data, byte[] privateKey) throws Exception {
//執行個體化密鑰工廠
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//生成私鑰
PrivateKey key = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey));
//資料加密
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
}
/**
* 私鑰加密
*
* @param data 待加密資料
* @param privateKey Base64編碼的私鑰
* @return String Base64編碼的加密資料
*/
public static String encryptByPrivateKey(String data, String privateKey) throws Exception {
byte[] key = Base64.getDecoder().decode(privateKey);
return Base64.getEncoder().encodeToString(encryptByPrivateKey(data.getBytes(CHARSET), key));
}
/**
* 公鑰加密
*
* @param data 待加密資料
* @param publicKey 公鑰位元組數組
* @return byte[] 加密資料
*/
private static byte[] encryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {
//執行個體化密鑰工廠
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//生成公鑰
PublicKey key = keyFactory.generatePublic(new X509EncodedKeySpec(publicKey));
//資料加密
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
}
/**
* 公鑰加密
*
* @param data 待加密資料
* @param publicKey Base64編碼的公鑰
* @return String Base64編碼的加密資料
*/
public static String encryptByPublicKey(String data, String publicKey) throws Exception {
byte[] key = Base64.getDecoder().decode(publicKey);
return Base64.getEncoder().encodeToString(encryptByPublicKey(data.getBytes(CHARSET), key));
}
/**
* 私鑰解密
*
* @param data 待解密資料
* @param privateKey 私鑰位元組數組
* @return byte[] 解密資料
*/
private static byte[] decryptByPrivateKey(byte[] data, byte[] privateKey) throws Exception {
//執行個體化密鑰工廠
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//生成私鑰
PrivateKey key = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey));
//資料解密
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(data);
}
/**
* 私鑰解密
*
* @param data Base64編碼的待解密資料
* @param privateKey Base64編碼的私鑰
* @return String 解密資料
*/
public static String decryptByPrivateKey(String data, String privateKey) throws Exception {
byte[] key = Base64.getDecoder().decode(privateKey);
return new String(decryptByPrivateKey(Base64.getDecoder().decode(data), key), CHARSET);
}
/**
* 公鑰解密
*
* @param data 待解密資料
* @param publicKey 公鑰位元組數組
* @return byte[] 解密資料
*/
private static byte[] decryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {
//執行個體化密鑰工廠
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//産生公鑰
PublicKey key = keyFactory.generatePublic(new X509EncodedKeySpec(publicKey));
//資料解密
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(data);
}
/**
* 公鑰解密
*
* @param data Base64編碼的待解密資料
* @param publicKey Base64編碼的公鑰
* @return String 解密資料
*/
public static String decryptByPublicKey(String data, String publicKey) throws Exception {
byte[] key = Base64.getDecoder().decode(publicKey);
return new String(decryptByPublicKey(Base64.getDecoder().decode(data), key), CHARSET);
}
/**
* 測試加解密方法
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//生成密鑰對,一般生成之後可以放到配置檔案中
KeyPair keyPair = RSAUtis.getKeyPair();
//公鑰
String publicKey = RSAUtis.getPublicKey(keyPair);
//私鑰
String privateKey = RSAUtis.getPrivateKey(keyPair);
System.out.println("公鑰:\n" + publicKey);
System.out.println("私鑰:\n" + privateKey);
publicKey="MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIfARmjDT+Ya4C2H4ClKm3z/JndMZ5cKBCoO4VNfyFcJ9sLIwZKlM9W099m4mGFgBm+IpBpDhWWFQwQCEuv7aV8CAwEAAQ==";
privateKey="MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAh8BGaMNP5hrgLYfgKUqbfP8md0xnlwoEKg7hU1/IVwn2wsjBkqUz1bT32biYYWAGb4ikGkOFZYVDBAIS6/tpXwIDAQABAkAyQFbQCRzWjTRXJmkwX9HXH4xZtQrTun14JDH87wP2U8vDO3x9s7WkDdx8yd4jbEeuNyTBr1D3TIAjvAzIu7xBAiEAwqmVlnFokP0uJbdY6MnKC7iyyLo8b93Bb8AFJExobJkCIQCyhp1T+dp2yBzI/TRj8HQNbciHlKl6V0sHI3FMRxkItwIgGb5JnRK0bHA/GRfNN0PwZ2puomMWuQiPm1K+m1VLfgkCIQCMCl7Frfv6K1WxMDZ1ovxWQGUKPcqIJou7SMkqTadMlwIgaU6W5G34s+LaeqauF69jDWv9POqyRwOTkp0rhXedY9o=";
String data = "RSA 加解密測試!";
{
System.out.println("\n===========私鑰加密,公鑰解密==============");
String s1 = RSAUtis.encryptByPrivateKey(data, privateKey);
System.out.println("加密後的資料:" + s1);
String s2 = RSAUtis.decryptByPublicKey(s1, publicKey);
System.out.println("解密後的資料:" + s2 + "\n");
}
{
System.out.println("\n===========公鑰加密,私鑰解密==============");
String s1 = RSAUtis.encryptByPublicKey(data, publicKey);
System.out.println("加密後的資料:" + s1);
String s2 = RSAUtis.decryptByPrivateKey(s1, privateKey);
System.out.println("解密後的資料:" + s2 + "\n\n");
}
}
}