与对称加密算法不同,非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey);
公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密;
如果用私有密钥对数据进行加密,那么只有用对应的公开密钥才能解密。
加密算法会用到Cipher,位于javax.crypto包下,算法通常要复杂一些,而且在实现加密的前提下,还会出现一些增强加密的手段,
和摘要算法不同,不同加密算法的代码差别较大,根据各自的项目需求学习即可,这里就不深入展开,下面是RSA加密的实现方式。
RSA加密
用到的Base64和Hex在前面文章已经给出代码,Common-codes包也有提供类似的代码
package com.sea.common.util.digest; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; /** * * @author Mr.css 2018年5月23日 下午10:36:29 * */ public class RSA { /** * 获取密钥对 */ public static KeyPair getKeyPair() { return getKeyPair(1024); } public static KeyPair getKeyPair(int initialize) { KeyPairGenerator keyPairGen; try { keyPairGen = KeyPairGenerator.getInstance("RSA"); keyPairGen.initialize(initialize); return keyPairGen.generateKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } } /** * 获取公钥 */ public static String getPublicKey(KeyPair keyPair) { return Base64.byteArrayToBase64(keyPair.getPublic().getEncoded()); } /** * 获取私钥 */ public static String getPrivateKey(KeyPair keyPair) { return Base64.byteArrayToBase64(keyPair.getPrivate().getEncoded()); } public static String encryptBase64(String data, String publicKey) throws Exception { return Base64.byteArrayToBase64(encrypt(data.getBytes(), publicKey)); } public static String decryptBase64(String encryptedData, String privateKey) throws Exception { return new String(decrypt(Base64.base64ToByteArray(encryptedData), privateKey)); } public static String encryptHex(String data, String publicKey) throws Exception { return Hex.encodeToString(encrypt(data.getBytes(), publicKey)); } public static String decryptHex(String encryptedData, String privateKey) throws Exception { return new String(decrypt(Hex.decode(encryptedData.toCharArray()), privateKey)); } /** * 加密 * * @param data 数据 * @param publicKey 公钥 */ public static byte[] encrypt(byte[] data, String publicKey) throws Exception { byte[] keyBytes = Base64.base64ToByteArray(publicKey); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory factory = KeyFactory.getInstance("RSA"); PublicKey key = factory.generatePublic(x509EncodedKeySpec); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(data); } /** * 解密 * * @param data 数据 * @param privateKey 私钥 */ public static byte[] decrypt(byte[] data, String privateKey) throws Exception { byte[] keyBytes = Base64.base64ToByteArray(privateKey); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory factory = KeyFactory.getInstance("RSA"); PrivateKey key = factory.generatePrivate(pkcs8EncodedKeySpec); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, key); return cipher.doFinal(data); } public static void main(String[] args) throws Exception { KeyPair keyPair = RSA.getKeyPair(1024); String publicKey = RSA.getPublicKey(keyPair); System.out.println(publicKey); String privateKey = RSA.getPrivateKey(keyPair); System.out.println(privateKey); String encodedText = RSA.encryptBase64("abcdefgh", publicKey); System.out.println(encodedText); String decodedText = RSA.decryptBase64(encodedText, privateKey); System.out.println(decodedText); } }