RSAEncrypt.java 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package com.yihu.jw.utils;
  2. import org.apache.commons.codec.binary.Base64;
  3. import javax.crypto.Cipher;
  4. import java.security.*;
  5. import java.security.interfaces.RSAPrivateKey;
  6. import java.security.interfaces.RSAPublicKey;
  7. import java.security.spec.PKCS8EncodedKeySpec;
  8. import java.security.spec.X509EncodedKeySpec;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. public class RSAEncrypt {
  12. /**
  13. * 随机生成密钥对
  14. * @throws NoSuchAlgorithmException
  15. */
  16. public static Map<Integer, String> genKeyPair() throws NoSuchAlgorithmException {
  17. Map<Integer, String> keyMap = new HashMap<Integer, String>();
  18. // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
  19. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  20. // 初始化密钥对生成器,密钥大小为96-1024位
  21. keyPairGen.initialize(1024,new SecureRandom());
  22. // 生成一个密钥对,保存在keyPair中
  23. KeyPair keyPair = keyPairGen.generateKeyPair();
  24. RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私钥
  25. RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公钥
  26. String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
  27. // 得到私钥字符串
  28. String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
  29. // 将公钥和私钥保存到Map
  30. keyMap.put(0,publicKeyString); //0表示公钥
  31. keyMap.put(1,privateKeyString); //1表示私钥
  32. return keyMap;
  33. }
  34. /**
  35. * RSA公钥加密
  36. *
  37. * @param str
  38. * 加密字符串
  39. * @param publicKey
  40. * 公钥
  41. * @return 密文
  42. * @throws Exception
  43. * 加密过程中的异常信息
  44. */
  45. public static String encrypt( String str, String publicKey) throws Exception{
  46. //base64编码的公钥
  47. byte[] decoded = Base64.decodeBase64(publicKey);
  48. RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
  49. //RSA加密
  50. Cipher cipher = Cipher.getInstance("RSA");
  51. cipher.init(Cipher.ENCRYPT_MODE, pubKey);
  52. String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
  53. return outStr;
  54. }
  55. /**
  56. * RSA私钥解密
  57. *
  58. * @param str
  59. * 加密字符串
  60. * @param privateKey
  61. * 私钥
  62. * @return 铭文
  63. * @throws Exception
  64. * 解密过程中的异常信息
  65. */
  66. public static String decrypt(String str, String privateKey) throws Exception{
  67. //64位解码加密后的字符串
  68. byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
  69. //base64编码的私钥
  70. byte[] decoded = Base64.decodeBase64(privateKey);
  71. RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
  72. //RSA解密
  73. Cipher cipher = Cipher.getInstance("RSA");
  74. cipher.init(Cipher.DECRYPT_MODE, priKey);
  75. String outStr = new String(cipher.doFinal(inputByte));
  76. return outStr;
  77. }
  78. // public static void main(String[] args) throws Exception {
  79. // //生成公钥和私钥
  80. // Map<Integer, String> keyMap = genKeyPair();
  81. // //加密字符串
  82. // String message = "test";
  83. // System.out.println("随机生成的公钥为:" + keyMap.get(0));
  84. // System.out.println("随机生成的私钥为:" + keyMap.get(1));
  85. // String messageEn = encrypt(message,keyMap.get(0));
  86. // System.out.println(message + "\t加密后的字符串为:" + messageEn);
  87. // String messageDe = decrypt(messageEn,keyMap.get(1));
  88. // System.out.println("还原后的字符串为:" + messageDe);
  89. // }
  90. }