1. package com.xiong;
    2. import sun.misc.BASE64Decoder;
    3. import sun.misc.BASE64Encoder;
    4. import javax.crypto.Cipher;
    5. import java.io.ByteArrayOutputStream;
    6. import java.io.UnsupportedEncodingException;
    7. import java.security.KeyFactory;
    8. import java.security.KeyPair;
    9. import java.security.KeyPairGenerator;
    10. import java.security.interfaces.RSAPrivateKey;
    11. import java.security.interfaces.RSAPublicKey;
    12. import java.security.spec.PKCS8EncodedKeySpec;
    13. import java.security.spec.X509EncodedKeySpec;
    14. //import java.util.Base64;
    15. public class RsaUtils {
    16. private static final String TYPE = "RSA";
    17. private static final String ALGORITHM = "RSA/ECB/PKCS1PADDING";
    18. private static final String CHARSET = "UTF-8";
    19. private static final int KEY_SIZE = 1024;
    20. public static KeyPair createKeyPair() {
    21. try {
    22. KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(TYPE);
    23. keyPairGenerator.initialize(KEY_SIZE);
    24. return keyPairGenerator.generateKeyPair();
    25. } catch (Exception e) {
    26. return null;
    27. }
    28. }
    29. public static void main(String[] args) throws Exception {
    30. KeyPair keyPair = RsaUtils.createKeyPair();
    31. String privateKey = RsaUtils.getPrivateKey(keyPair);
    32. String publicKey = RsaUtils.getPublicKey(keyPair);
    33. String encrypt = RsaUtils.encrypt("1234567", publicKey);
    34. System.out.println(privateKey);
    35. System.out.println(publicKey);
    36. System.out.println(encrypt);
    37. }
    38. public static String getPublicKey(KeyPair keyPair) {
    39. // return Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());
    40. return new BASE64Encoder().encode(keyPair.getPublic().getEncoded());
    41. }
    42. public static String getPrivateKey(KeyPair keyPair) throws UnsupportedEncodingException {
    43. byte[] encoded = keyPair.getPrivate().getEncoded();
    44. // return Base64.getEncoder().encodeToString(encoded);
    45. return new BASE64Encoder().encode(encoded);
    46. }
    47. public static String encrypt(String data, String publicKeyString) throws Exception {
    48. Cipher cipher = Cipher.getInstance(ALGORITHM);
    49. KeyFactory keyFactory = KeyFactory.getInstance(TYPE);
    50. X509EncodedKeySpec keySpec = new X509EncodedKeySpec(new BASE64Decoder().decodeBuffer(publicKeyString));
    51. RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);
    52. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    53. // return Base64.getEncoder().encodeToString(splitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET)));
    54. return new BASE64Encoder().encode(splitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET)));
    55. }
    56. public static String decrypt(String data, String privateKeyString) throws Exception {
    57. Cipher cipher = Cipher.getInstance(ALGORITHM);
    58. KeyFactory keyFactory = KeyFactory.getInstance(TYPE);
    59. PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(new BASE64Decoder().decodeBuffer(privateKeyString));
    60. RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
    61. cipher.init(Cipher.DECRYPT_MODE, privateKey);
    62. return new String(splitCodec(cipher, Cipher.DECRYPT_MODE, new BASE64Decoder().decodeBuffer(data)), CHARSET);
    63. }
    64. private static byte[] splitCodec(Cipher cipher, int mode, byte[] data) throws Exception {
    65. int maxBlock = KEY_SIZE / 8 - (mode == Cipher.DECRYPT_MODE ? 0 : 11);
    66. try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
    67. byte[] buffer;
    68. for (int offset = 0; offset < data.length; offset += maxBlock) {
    69. buffer = cipher.doFinal(data, offset, Math.min(maxBlock, data.length - offset));
    70. out.write(buffer, 0, buffer.length);
    71. }
    72. return out.toByteArray();
    73. }
    74. }
    75. }