+ * 生成密钥对(公钥和私钥) + *
* - * @throws NoSuchAlgorithmException + * @return + * @throws Exception */ - public static void getKeyPair() throws Exception { - //KeyPairGenerator类用于生成公钥和密钥对,基于RSA算法生成对象 - KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); - keyPairGen.initialize(2048, new SecureRandom()); - //生成一个密钥对,保存在keyPair中 + public static Map+ * 用私钥对信息生成数字签名 + *
* - * @param str 加密字符串 - * @param publicKey 公钥 - * @return 密文 - * @throws Exception 加密过程中的异常信息 + * @param msg 已加密数据 + * @param privateKey 私钥(BASE64编码) + * @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)); - //RAS加密 - Cipher cipher = Cipher.getInstance("RSA"); - cipher.init(Cipher.ENCRYPT_MODE, pubKey); - String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8"))); - return outStr; + public static String sign(String msg, String privateKey) throws Exception { + byte[] data = msg.getBytes(); + byte[] keyBytes = Base64.getDecoder().decode(privateKey); + PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); + Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); + signature.initSign(privateK); + signature.update(data); + return Base64.getEncoder().encodeToString(signature.sign()); } /** - * RSA私钥解密 + *+ * 校验数字签名 + *
* - * @param str 加密字符串 - * @param privateKey 私钥 - * @return 铭文 - * @throws Exception 解密过程中的异常信息 + * @param msg 已加密数据 + * @param publicKey 公钥(BASE64编码) + * @param sign 数字签名 + * @return + * @throws Exception */ - public static String decrypt(String str, String privateKey) throws Exception { - //Base64解码加密后的字符串 - byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8")); - //Base64编码的私钥 - byte[] decoded = Base64.decodeBase64(privateKey); - PrivateKey priKey = 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; + public static boolean verify(String msg, String publicKey, String sign) + throws Exception { + byte[] data = msg.getBytes(); + byte[] keyBytes = Base64.getDecoder().decode(publicKey); + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + PublicKey publicK = keyFactory.generatePublic(keySpec); + Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); + signature.initVerify(publicK); + signature.update(data); + return signature.verify(Base64.getDecoder().decode(sign)); + } + /** + *+ * 私钥解密 + *
+ * + * @param encryptedDataStr 已加密数据 + * @param privateKey 私钥(BASE64编码) + * @return + * @throws Exception + */ + public static String decryptByPrivateKey(String encryptedDataStr, String privateKey) + throws Exception { + byte[] encryptedData = Base64.getDecoder().decode(encryptedDataStr); + byte[] keyBytes = Base64.getDecoder().decode(privateKey); + PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + cipher.init(Cipher.DECRYPT_MODE, privateK); + int inputLen = encryptedData.length; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int offSet = 0; + byte[] cache; + int i = 0; + // 对数据分段解密 + while (inputLen - offSet > 0) { + if (inputLen - offSet > MAX_DECRYPT_BLOCK) { + cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); + } else { + cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); + } + out.write(cache, 0, cache.length); + i++; + offSet = i * MAX_DECRYPT_BLOCK; + } + byte[] decryptedData = out.toByteArray(); + out.close(); + return new String(decryptedData); } + + /** + *+ * 公钥解密 + *
+ * + * @param encryptedDataStr 已加密数据 + * @param publicKey 公钥(BASE64编码) + * @return + * @throws Exception + */ + public static String decryptByPublicKey(String encryptedDataStr, String publicKey) + throws Exception { + byte[] encryptedData = Base64.getDecoder().decode(encryptedDataStr); + byte[] keyBytes = Base64.getDecoder().decode(publicKey); + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + Key publicK = keyFactory.generatePublic(x509KeySpec); + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + cipher.init(Cipher.DECRYPT_MODE, publicK); + int inputLen = encryptedData.length; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int offSet = 0; + byte[] cache; + int i = 0; + // 对数据分段解密 + while (inputLen - offSet > 0) { + if (inputLen - offSet > MAX_DECRYPT_BLOCK) { + cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); + } else { + cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); + } + out.write(cache, 0, cache.length); + i++; + offSet = i * MAX_DECRYPT_BLOCK; + } + byte[] decryptedData = out.toByteArray(); + out.close(); + return new String(decryptedData); + } + + /** + *+ * 公钥加密 + *
+ * + * @param msg 源数据 + * @param publicKey 公钥(BASE64编码) + * @return + * @throws Exception + */ + public static String encryptByPublicKey(String msg, String publicKey) + throws Exception { + byte[] data = msg.getBytes(); + byte[] keyBytes = Base64.getDecoder().decode(publicKey); + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + Key publicK = keyFactory.generatePublic(x509KeySpec); + // 对数据加密 + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + cipher.init(Cipher.ENCRYPT_MODE, publicK); + int inputLen = data.length; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int offSet = 0; + byte[] cache; + int i = 0; + // 对数据分段加密 + while (inputLen - offSet > 0) { + if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { + cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); + } else { + cache = cipher.doFinal(data, offSet, inputLen - offSet); + } + out.write(cache, 0, cache.length); + i++; + offSet = i * MAX_ENCRYPT_BLOCK; + } + byte[] encryptedData = out.toByteArray(); + out.close(); + + String encryptedDataStr = Base64.getEncoder().encodeToString(encryptedData); + return encryptedDataStr; + } + + /** + *+ * 私钥加密 + *
+ * + * @param msg 源数据 + * @param privateKey 私钥(BASE64编码) + * @return + * @throws Exception + */ + public static String encryptByPrivateKey(String msg, String privateKey) + throws Exception { + byte[] data = msg.getBytes(); + byte[] keyBytes = Base64.getDecoder().decode(privateKey); + PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + cipher.init(Cipher.ENCRYPT_MODE, privateK); + int inputLen = data.length; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int offSet = 0; + byte[] cache; + int i = 0; + // 对数据分段加密 + while (inputLen - offSet > 0) { + if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { + cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); + } else { + cache = cipher.doFinal(data, offSet, inputLen - offSet); + } + out.write(cache, 0, cache.length); + i++; + offSet = i * MAX_ENCRYPT_BLOCK; + } + byte[] encryptedData = out.toByteArray(); + out.close(); + + String encryptedDataStr = Base64.getEncoder().encodeToString(encryptedData); + return encryptedDataStr; + } + + /** + *+ * 获取私钥 + *
+ * + * @param keyMap 密钥对 + * @return + * @throws Exception + */ + public static String getPrivateKey(Map+ * 获取公钥 + *
+ * + * @param keyMap 密钥对 + * @return + * @throws Exception + */ + public static String getPublicKey(Map