小程序获取实名+身份证加密解密方法

文档中(保密)提供了php的加解密方法,这里就不重复描述,java方法类型提供如下

小程序前端:

  <button open-type="getRealnameAuthInfo" bindgetrealnameauthinfo="queryuserInfo" style='font-size: 40rpx;border-radius:0rpx;  background-color: #005bac;  line-height: 90rpx;color:white;' category-id="{{[99, 6]}}">获取实名</button>

前端需要注意的是行业目录坐标category-id,具体不重复。

后端加密解密java代码

/**
	 * 私钥加密
	 */
	public static String sign(String request, String privateKey) throws Exception {
		byte[] data = request.getBytes("UTF-8");
		byte[] keyBytes = Base64Util.decode(privateKey);
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
		Signature signature = Signature.getInstance("SHA256WithRSA");
		signature.initSign(privateK);
		signature.update(data);
		return Base64Util.encode(signature.sign());
	}

	// 私钥解密
	public static String decryptByPrivateKey(String content, String Key) throws Exception {
		// 将base64编码后的私钥字符串转成PrivateKey实例
		byte[] keyBytes = Base64.getDecoder().decode(Key.getBytes());
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
		// 获取私钥
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] cipherText = Base64.getDecoder().decode(content);
		byte[] decryptText = cipher.doFinal(cipherText);
		return new String(decryptText, "gbk");
	}

由于接口为高级接口,不能公开,提供java后端加密解密方法,仅供参考

猜你喜欢

转载自blog.csdn.net/u010598111/article/details/81358054