求教RSA加密问题,先感谢各位大佬!

壹爱 发布于 09/07 11:15
阅读 164
收藏 0

项目对接一个第三方,对方接口文档规定加密顺序:

字符串排序后 先 md5,然后 再RSA 加密 得到签名字符串

 

可我这边尝试了很久,依然与对方加密出来的结果匹配不上,

 

求助各位JAVA大佬 ,!!! 万分感谢。

 

提供以下测试数据:

私钥
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANMX8Vm2w4vfqylNU+z+nj11F3nXMR+wNMWBpbHTgL1TyxHnaw1miNnUVISiMPuYH3yDrxriNZqEovkGAxnMRkORpFmgRhBB3YnyqjPXl9rmV3Ne2GVm+KM24rf7OQ3BCiujFv7pi0tmNApEO1T8V6LdauiAKVNPwAQkBn45uhOnAgMBAAECgYAy7h/pCVN8t+ZP/Lw2/een5ExZtTXA836H+JPFp8Xz5/eixjbp8Mcr5zLNHWS8DyPposvorJNKSJ4Zfv3Yh5Sb4I6jOJxUDjL7KEhL0vCzpCLi+Rrke7K3Y3oPQs3VKp9fyGnr07lCqktLlxeOie78F0+74NLkT8A+qT7s+xMQoQJBAPJDDd54f/1b4PQaTBovXGnAOCepG6glNhnS3frJoVwRbNUQvAWfe/tnvM9f/xixZVMGsqucTVJMb0cZEIO4+tsCQQDfEGnA51Pelp7nUmFwI85CM0E9DDoIBMnx+PJWfB32KC0YmfojHDZ1hEIFF9xPECgRSpP70N/eKbcIWzog/RYlAkAFT90U/LVpu8MB9zWl+VXgZ7uTewzFMhOum0YMwrEMEMG/Jj/JK8cPKyYjseopA3LCUEpIowO7AzwJZg5LpRmzAkB2Zvzrgx7WoPa57NWBs8/Q/encILhQ91HGWhEwbPNubQb67wiCSIBzwn9UusSbRwUtoHuKh+W8xEG+csYZKp3tAkEArOcRDsbY3VegFLA23RaiaIyPRsoh/5oAbiSHcYTs0AOb0q4qcM+ijCMIGXDDvTz3cEBjZtECChPcPcRE5cZH/A==

公钥
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCgoXLfff3vQPvUuHDIso07zji2O+d6R+Qyr+ByUjPVDN4BU300Bx4h2YUd6jVRmsed1iiVYOx95R1GJX8F2eEVfEEDfCUIUzPoUkzudx9VmKx0aF49WL7QxYJjinQmq4Vd0vRTDHcrSsffljMfdX3PvivlzuJ4Oe//iATbhkAt3wIDAQAB

 

 

加密前字符串
amount=10&attach=888&goods_name=aaaaaa&mert_no=TESTON001&notify_url=http://app.xxx.com/pay/payOrderCallback&order_ip=1.2.3.4&out_trade_no=1567753205030&pay_type=ALIQR&platform_no=A&return_url=http://app.xxx.com

md5后
85fd20eddc12765a3ffc4f37c768ccdb

rsa签名后
UxEiX7rAwvtfo/vpLeFYR44oU7FpbmpAOzh68g11hb5dIm8MRAVnyL3mxwlcys272hpy23TJIyV9E8JymnuUeuLxJ8aBR6NnGIkseo7D2w1xtWC3qERfCU7gVvNNQ299kAEwwBk7kAB2QKbofhcpDWxbbo14SHbjwh60ClvEQiI=
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package com.cn.pay.kit;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;

public class RSAKit {
	// 非对称密钥算法
	private static final String KEY_ALGORITHM = "RSA";
	private static final int enSegmentSize = 117;// 加密长度
	private static final int deSegmentSize = 128;// 解密长度
	/**
	 * 使用RSA私钥加密数据
	 * 
	 * @param key私钥
	 * @param data
	 *            要加密的数据
	 * @return 加密数据
	 */
	@SuppressWarnings("restriction")
	public static String encryptByRSA(String data, String key) throws Exception {
		// 取得私钥
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(key));
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		// 生成私钥
		PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, privateKey);
		// return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes("UTF-8")));
		return new sun.misc.BASE64Encoder().encode(cipher.doFinal(data.getBytes("UTF-8")));
	}
	/**
	 * 私钥解密
	 * 
	 * @param data
	 *            待解密数据
	 * @param key
	 *            密钥
	 */
	public static String decryptByPrivateKey(String data, String key) throws Exception {
		// 取得私钥
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(key));
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		// 生成私钥
		PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
		/**/
		// 数据解密
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] decBytes = cipherDoFinal(cipher, Base64.getDecoder().decode(data), deSegmentSize); // 分段加密;
		return new String(decBytes, "UTF-8");
	}
	/**
	 * 用RSA公钥解密
	 * 
	 * @param privKeyInByte
	 *            公钥打包成byte[]形式
	 * @param data
	 *            要解密的数据
	 * @return 解密数据
	 */
	public static String decryptByRSA(String privKeyInByte, String data) {
		try {
			KeyFactory mykeyFactory = KeyFactory.getInstance("RSA");
			X509EncodedKeySpec pub_spec = new X509EncodedKeySpec(Base64.getDecoder().decode(privKeyInByte));
			PublicKey pubKey = mykeyFactory.generatePublic(pub_spec);
			Cipher cipher = Cipher.getInstance(mykeyFactory.getAlgorithm());
			cipher.init(Cipher.DECRYPT_MODE, pubKey);
			return Base64.getEncoder().encodeToString(cipher.doFinal(Base64.getDecoder().decode(data)));
		} catch (Exception e) {
			return null;
		}
	}
	/**
	 * 公钥加密
	 *
	 * @param data
	 *            待加密数据
	 * @param key
	 *            密钥
	 */
	public static String encryptByPublicKey(String data, String key) throws Exception {
		// 实例化密钥工厂
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		// 初始化公钥
		X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(key));
		// 产生公钥
		PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
		// 数据加密
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, pubKey);
		byte[] resultBytes = cipherDoFinal(cipher, data.getBytes("UTF-8"), enSegmentSize); // 分段加密
		return Base64.getEncoder().encodeToString(resultBytes);
	}
	/**
	 * 分段加解密
	 */
	private static byte[] cipherDoFinal(Cipher cipher, byte[] srcBytes, int segmentSize)
			throws IllegalBlockSizeException, BadPaddingException, IOException {
		if (segmentSize <= 0)
			throw new RuntimeException("分段大小必须大于0");
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int inputLen = srcBytes.length;
		int offSet = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段解密
		while (inputLen - offSet > 0) {
			if (inputLen - offSet > segmentSize) {
				cache = cipher.doFinal(srcBytes, offSet, segmentSize);
			} else {
				cache = cipher.doFinal(srcBytes, offSet, inputLen - offSet);
			}
			out.write(cache, 0, cache.length);
			i++;
			offSet = i * segmentSize;
		}
		byte[] data = out.toByteArray();
		return data;
	}
}

 

加载中
0
壹爱
壹爱

感谢各位,已经解决了。对方PHP接口,加密方式不一样

 

算是文档不完善导致的,对方的接口真是有点坑呀。

0
起个起个名字都难
起个起个名字都难

你这私钥公布出来不会出问题吗

壹爱
壹爱
测试环境用的:sweat_smile: 没关系
0
K
King丶J

我一般都是问第三方要个加解密的demo。。。

0
壹爱
壹爱

引用来自“壹爱”的评论

感谢各位,已经解决了。对方PHP接口,加密方式不一样

 

算是文档不完善导致的,对方的接口真是有点坑呀。

已解决

返回顶部
顶部