package test; import java.io.UnsupportedEncodingException; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.PublicKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; import java.util.Base64.Decoder; import java.util.Base64.Encoder; import javax.crypto.Cipher; public class Test { public static void main(String[] args) throws UnsupportedEncodingException { //加密 Encoder en = Base64.getEncoder(); //解密 Decoder de = Base64.getDecoder(); String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC18PiGQSQEuP0kv/qLPOr5cwT2QnaAugwwldQNXPLODB4px1U8hgOmGs8z7bVWU9yZjVzXVfBia9Y/TJIzCoeHSGSD3m0PIqPfDCdyA+natWEQVJxJMkEtYzWte3Wfa8e2QG581r64y7zq6Re9/e5BTlXgbUX9clGt/QyabZVufwIDAQAB"; String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALXw+IZBJAS4/SS/+os86vlzBPZCdoC6DDCV1A1c8s4MHinHVTyGA6YazzPttVZT3JmNXNdV8GJr1j9MkjMKh4dIZIPebQ8io98MJ3ID6dq1YRBUnEkyQS1jNa17dZ9rx7ZAbnzWvrjLvOrpF7397kFOVeBtRf1yUa39DJptlW5/AgMBAAECgYAEh9uofDbJZ1HmeGFk1j+DtrmI6F7LFdb33sNJEed5+hNGNxCIUU4F9OT0Bhdd7vivWjXVpu41hTnwmZeKbdiyGvrgs3zEYM0st2Iv+Em66MVoFcvhxdQuOq13smvDXQ59ZFGApnD7PpHBAGZpAMwCVdw7dd0rYJHyc1oQByk+AQJBAN1CYANeCPXeHjSfpWScVqkj3Ku9uGCZji8qdWfcTdaJw9xxPhK8AQ5+Y2XaD/u5V9PyA68esMBhHyH0hK71w/kCQQDSgjCpQn8G4cAgfJtK9SvP9Eh9lJgUfFGUF2VFQJF0UuVmrCbua9107kht//gsFk2TtnqIGAvLdRqS21HJt/Q3AkEA2ocr/SdrAisW+jKTUAN5NCTPVnBTIlWHMN5mhuX3muUGJTtTa310LbR2x6nQD3bHX/Wv3c4aFGfYL9kpEwR5QQJBAJ1uul/LmF6YNOzW4/kCNLr4kcJBgB9J/mTHvng1WtLpMCFCPHdw+hV6I3cIyRyHrYr8HNVtTlzh/na4SYmX5q8CQHE/mnmgAhQMfsEHzLcDHdxWkISX9jOEPP1zc/FBZxDBF670Ragb7BdEFnSkRo4O33JqjTQ1d1sQRTsbOTULW/k="; byte[] bPubKey = de.decode(publicKey); byte[] bPriKey = de.decode(privateKey); String s = "中文你"; System.out.println("加密字符串:"+s); String ss = en.encodeToString(encryptByRSA1(bPriKey,s.getBytes("UTF-8"))); System.out.println("加密後的字符串:"+ss); String sss = new String(decryptByRSA1(bPubKey,de.decode(ss)),"UTF-8"); System.out.println("解密後的字符串:"+sss); } /** * 使用RSA私鑰加密數據 * * @param pubKeyInByte * 打包的byte[]形式私鑰 * @param data * 要加密的數據 * @return 加密數據 */ public static byte[] encryptByRSA1(byte[] privKeyInByte, byte[] data) { try { PKCS8EncodedKeySpec priv_spec = new PKCS8EncodedKeySpec( privKeyInByte); KeyFactory mykeyFactory = KeyFactory.getInstance("RSA"); PrivateKey privKey = mykeyFactory.generatePrivate(priv_spec); Cipher cipher = Cipher.getInstance(mykeyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privKey); return cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 用RSA公鑰解密 * * @param privKeyInByte * 公鑰打包成byte[]形式 * @param data * 要解密的數據 * @return 解密數據 */ public static byte[] decryptByRSA1(byte[] pubKeyInByte, byte[] data) { try { KeyFactory mykeyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec pub_spec = new X509EncodedKeySpec(pubKeyInByte); PublicKey pubKey = mykeyFactory.generatePublic(pub_spec); Cipher cipher = Cipher.getInstance(mykeyFactory.getAlgorithm()); //Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, pubKey); return cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); return null; } } /** * RSA公鑰加密 * * @param str * 加密字符串 * @param publicKey * 公鑰 * @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); String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8"))); return outStr; } /** * RSA私鑰解密 * * @param str * 加密字符串 * @param privateKey * 私鑰 * @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); String outStr = new String(cipher.doFinal(inputByte)); return outStr; } }