RSA.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.yihu.ehr.util.encrypt;
  2. import com.yihu.ehr.util.encode.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.InvalidKeySpecException;
  8. import java.security.spec.PKCS8EncodedKeySpec;
  9. import java.security.spec.X509EncodedKeySpec;
  10. import java.util.HashMap;
  11. /**
  12. * RSA加密辅助类,采用Base64编码
  13. *
  14. * @created Air 2015/6/02.
  15. */
  16. public class RSA {
  17. public static final String PUBLIC_KEY = "public";
  18. public static final String PRIVATE_KEY = "private";
  19. public static final String KEY_ALGORITHM = "RSA";
  20. /**
  21. * 生成公钥和私钥
  22. *
  23. * @throws NoSuchAlgorithmException
  24. */
  25. public static HashMap<String, Key> generateKeys() throws NoSuchAlgorithmException {
  26. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
  27. keyPairGen.initialize(1024);
  28. KeyPair keyPair = keyPairGen.generateKeyPair();
  29. RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
  30. RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
  31. HashMap<String, Key> map = new HashMap<>();
  32. map.put(PUBLIC_KEY, publicKey);
  33. map.put(PRIVATE_KEY, privateKey);
  34. return map;
  35. }
  36. public static String encodeKey(Key key) {
  37. // return HexEncode.toHexString(key.getEncoded());
  38. return Base64.encode(key.getEncoded());
  39. }
  40. public static Key genPrivateKey(String key) {
  41. // byte[] bytes = HexEncode.toBytes(key);
  42. byte[] bytes = Base64.decode(key);
  43. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(bytes);
  44. try {
  45. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  46. return keyFactory.generatePrivate(pkcs8KeySpec);
  47. } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
  48. e.printStackTrace();
  49. }
  50. return null;
  51. }
  52. public static Key genPublicKey(String key) {
  53. if (key == null){
  54. return null;
  55. }
  56. byte[] bytes = Base64.decode(key);
  57. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(bytes);
  58. try {
  59. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  60. return keyFactory.generatePublic(x509KeySpec);
  61. } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
  62. e.printStackTrace();
  63. }
  64. return null;
  65. }
  66. /**
  67. * @param data 明文
  68. * @param key 密钥
  69. * @return HexString密文
  70. * @throws Exception
  71. */
  72. public static String encrypt(String data, Key key) throws Exception {
  73. Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
  74. cipher.init(Cipher.ENCRYPT_MODE, key);
  75. // return HexEncode.toHexString(cipher.doFinal(data.getBytes()));
  76. return Base64.encode(cipher.doFinal(data.getBytes()));
  77. }
  78. /**
  79. * @param data HexString密文
  80. * @param key 密钥
  81. * @return 明文
  82. * @throws Exception
  83. */
  84. public static String decrypt(String data, Key key) throws Exception {
  85. Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
  86. cipher.init(Cipher.DECRYPT_MODE, key);
  87. // return new String(cipher.doFinal(HexEncode.toBytes(data)));
  88. return new String(cipher.doFinal(Base64.decode(data)));
  89. }
  90. }