RSA加密例項
阿新 • • 發佈:2018-12-19
package com.example.secret; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.Cipher; import java.io.IOException; import java.security.*; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; /** * RSA加密工具類 * * @author sunziwen * 2018-07-31 20:27 * @version 1.0 **/ public class RSAUtil { /** * 獲取RSA公私鑰匙對 * @return * @throws NoSuchAlgorithmException */ public static KeyPair getKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPairGenerator.generateKeyPair(); return keyPair; } /** * 獲取公鑰(base64編碼) * @param keyPair * @return */ public static String getPublicKey(KeyPair keyPair) { PublicKey publicKey = keyPair.getPublic(); byte[] bytes = publicKey.getEncoded(); return byte2Base64(bytes); } /** * 獲取私鑰(Base64編碼) * @param keyPair * @return */ public static String getPrivateKey(KeyPair keyPair){ PrivateKey privateKey = keyPair.getPrivate(); byte[] bytes = privateKey.getEncoded(); return byte2Base64(bytes); } /** * 將Base64編碼後的公鑰轉換成PublicKey物件 * @param pubStr * @return * @throws Exception */ public static PublicKey string2PublicKey(String pubStr) throws Exception{ byte[] keyBytes = base642Byte(pubStr); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(keySpec); return publicKey; } /** * 將Base64編碼後的私鑰轉換成PrivateKey物件 * @param priStr * @return * @throws Exception */ public static PrivateKey string2PrivateKey(String priStr) throws Exception{ byte[] keyBytes = base642Byte(priStr); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } /** * 公鑰加密 * @param content * @param publicKey * @return * @throws Exception */ public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception{ Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] bytes = cipher.doFinal(content); return bytes; } /** * 私鑰解密 * @param content * @param privateKey * @return * @throws Exception */ public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{ Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] bytes = cipher.doFinal(content); return bytes; } /** * 位元組陣列轉Base64編碼 * @param bytes * @return */ public static String byte2Base64(byte[] bytes){ BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(bytes); } /** * Base64編碼轉陣列 * @param base64Key * @return * @throws IOException */ public static byte[] base642Byte(String base64Key) throws IOException { BASE64Decoder base64Decoder = new BASE64Decoder(); return base64Decoder.decodeBuffer(base64Key); } }
例項:
package com.example.secret; import java.security.KeyPair; import java.security.PrivateKey; import java.security.PublicKey; import org.junit.Test; public class TestRSA { public static void main(String args[]) throws Exception { KeyPair keyPair = RSAUtil.getKeyPair(); System.out.println("公鑰:"); String publicKey = RSAUtil.getPublicKey(keyPair); System.out.println(publicKey); System.out.println("私鑰:"); String privateKey = RSAUtil.getPrivateKey(keyPair); System.out.println(privateKey); String string = "勇敢行動才是我們面對生活的必然選擇"; PublicKey publicKey1 = RSAUtil.string2PublicKey(publicKey); byte[] bytes = RSAUtil.publicEncrypt(string.getBytes(),publicKey1 ); String msg = RSAUtil.byte2Base64(bytes); System.out.println("傳輸的加密後內容:"); System.out.println(msg); byte[] bytes1 = RSAUtil.privateDecrypt(RSAUtil.base642Byte(msg), RSAUtil.string2PrivateKey(privateKey)); System.out.println("解密後內容:"); System.out.println(new String(bytes1)); } @Test public void testRSA(){ try { //===============生成公鑰和私鑰,公鑰傳給客戶端,私鑰服務端保留================== //生成RSA公鑰和私鑰,並Base64編碼 KeyPair keyPair = RSAUtil.getKeyPair(); String publicKeyStr = RSAUtil.getPublicKey(keyPair); String privateKeyStr = RSAUtil.getPrivateKey(keyPair); System.out.println("RSA公鑰Base64編碼:" + publicKeyStr); System.out.println("RSA私鑰Base64編碼:" + publicKeyStr); //=================客戶端================= //hello, i am infi, good night!加密 String message = "hello, i am infi, good night!"; //將Base64編碼後的公鑰轉換成PublicKey物件 PublicKey publicKey = RSAUtil.string2PublicKey(publicKeyStr); //用公鑰加密 byte[] publicEncrypt = RSAUtil.publicEncrypt(message.getBytes(), publicKey); //加密後的內容Base64編碼 String byte2Base64 = RSAUtil.byte2Base64(publicEncrypt); System.out.println("公鑰加密並Base64編碼的結果:" + byte2Base64); //############## 網路上傳輸的內容有Base64編碼後的公鑰 和 Base64編碼後的公鑰加密的內容 ################# //===================服務端================ //將Base64編碼後的私鑰轉換成PrivateKey物件 PrivateKey privateKey = RSAUtil.string2PrivateKey(privateKeyStr); //加密後的內容Base64解碼 byte[] base642Byte = RSAUtil.base642Byte(byte2Base64); //用私鑰解密 byte[] privateDecrypt = RSAUtil.privateDecrypt(base642Byte, privateKey); //解密後的明文 System.out.println("解密後的明文: " + new String(privateDecrypt)); } catch (Exception e) { e.printStackTrace(); } } }