| | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * RSA工具类 |
| | | * @author WWW |
| | | */ |
| | | @SuppressWarnings("ALL") |
| | | public class RsaHelper { |
| | | /** |
| | | * 私钥 |
| | | */ |
| | | private static String privateKey; |
| | | |
| | | /** |
| | | * 公钥 |
| | | */ |
| | | private static String publicKey; |
| | | |
| | | /** |
| | | * 密钥算法 |
| | | */ |
| | | private static final String KEY_ALGORITHM = "RSA"; |
| | | |
| | | /** |
| | | * RSA密钥长度:1024 或 2048 |
| | | */ |
| | | private static final int DEFAULT_RSA_KEY_SIZE = 1024; |
| | | |
| | | /** |
| | | * 日志 |
| | | */ |
| | | private final static Log log = LogFactory.getLog(RsaHelper.class); |
| | | |
| | | /** |
| | | * 生成公私钥 |
| | | */ |
| | | public static void generate() { |
| | | Map<String, String> result = generateRsaKey(DEFAULT_RSA_KEY_SIZE); |
| | | System.out.println("公钥为:" + result.get("publicKey")); |
| | | System.out.println("私钥为:" + result.get("privateKey")); |
| | | } |
| | | |
| | | /** |
| | | * 获取RSA加密私钥 |
| | | * |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public static String getPrivateKey() throws IOException { |
| | | if (privateKey == null) { |
| | | InputStream inPrivate = new ClassPathResource("config" + File.separator + "rsa_private_key.txt").getInputStream(); |
| | |
| | | return privateKey; |
| | | } |
| | | |
| | | /** |
| | | * 获取RSA加密公钥 |
| | | * |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public static String getPublicKey() throws IOException { |
| | | if (publicKey == null) { |
| | | InputStream inPrivate = new ClassPathResource("config" + File.separator + "rsa_public_key.txt").getInputStream(); |
| | |
| | | return publicKey; |
| | | } |
| | | |
| | | /** |
| | | * 读取文本文件 |
| | | * |
| | | * @param fileName 文件路径 |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public static String readFile(String fileName) throws IOException { |
| | | File file = new File(fileName); |
| | | BufferedReader br = new BufferedReader(new FileReader(file)); |
| | |
| | | return result.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 把inputStream转成String |
| | | * |
| | | * @param is |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | private static String inputStream2String(InputStream is) throws IOException { |
| | | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| | | |
| | |
| | | return str; |
| | | } |
| | | |
| | | /** |
| | | * 生成RSA的公私钥 |
| | | * |
| | | * @param keySize 1025 或 2048 |
| | | * @return |
| | | */ |
| | | public static Map<String, String> generateRsaKey(int keySize) { |
| | | Map<String, String> result = new HashMap<>(2); |
| | | try { |
| | | KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); |
| | | |
| | | // 初始化密钥对生成器,密钥大小为1024 2048位 |
| | | keyPairGen.initialize(keySize, new SecureRandom()); |
| | | |
| | | // 生成一个密钥对,保存在keyPair中 |
| | | KeyPair keyPair = keyPairGen.generateKeyPair(); |
| | | |
| | | // 得到公钥字符串 |
| | | String pub = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded())); |
| | | result.put("publicKey", pub); |
| | | |
| | | // 得到私钥字符串 |
| | | String pri = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded())); |
| | | result.put("privateKey", pri); |
| | | } catch (Exception ex) { |
| | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * RSA私钥解密 |
| | | * |
| | | * @param str 加密的字符串 |
| | | * @return 解密字符串 |
| | | * @throws Exception 加密过程中的异常信息 |
| | | */ |
| | | public static String decrypt(String str) throws Exception { |
| | | // 64位解码加密后的字符串 |
| | | byte[] inputByte = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8)); |
| | | |
| | | // Base64编码的私钥 |
| | | byte[] decoded = Base64.decodeBase64(getPrivateKey()); |
| | | RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); |
| | | |
| | | // RSA解密:RSA/ECB/NoPadding |
| | | // RSA/ECB/NoPadding |
| | | Cipher cipher = Cipher.getInstance("RSA"); |
| | | cipher.init(Cipher.DECRYPT_MODE, priKey); |
| | | |
| | |
| | | return outStr; |
| | | } |
| | | |
| | | /** |
| | | * RSA公钥加密 |
| | | * |
| | | * @param str 需要加密的字符串 |
| | | * @return 密文 |
| | | * @throws Exception 加密过程中的异常信息 |
| | | */ |
| | | public static String encrypt(String str) throws Exception { |
| | | // Base64编码的公钥 |
| | | byte[] decoded = Base64.decodeBase64(getPublicKey()); |
| | | |
| | | RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); |
| | | |
| | | // RSA加密:RSA/ECB/NoPadding |
| | | // RSA/ECB/NoPadding |
| | | Cipher cipher = Cipher.getInstance("RSA"); |
| | | cipher.init(Cipher.ENCRYPT_MODE, pubKey); |
| | | |