最近遇到一个需求,需要对接第三方平台,然后对面只给公钥和私钥 ,本身我是用php开发的,第三方的demo 是java 头大完全不知道什么意思,看不懂java写法 有没有大哥帮我写个php的类
这是第三方demo提供的加密加签方法
以下是完整文件
package com.example.guojindemo.utils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class SignUtils {
public static void main(String[] args) throws Exception {
}
/**
* 生成DES密码,注意:当前版本需要为ASCII可见字符作为密码
*
* @return
* @throws Exception
*/
public static SecretKey genDESKey() throws Exception {
// return KeyGenerator.getInstance("DES").generateKey(); !!!!!注意!!!!暂不支持这种方式
int targetStringLength = 16;
Random random = new Random();
//随机生成16位可见字符密码, JDK8+
String key = random.ints('0', 'z' + 1)
.limit(targetStringLength)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
return keyFactory.generateSecret(dks);
}
/**
* 加密,用对方公钥加密,我方私钥加签
*
* @param raw
* @param desKey
* @param publicKey
* @param privateKey
* @return
* @throws Exception
*/
public static Map<String,Object> encrypt(String raw, SecretKey desKey, PublicKey publicKey, PrivateKey privateKey) throws Exception {
Map<String,Object> ret = new HashMap<>();
/* ------------- SHA1withRSA 算法进行签名 ------------------ */
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initSign(privateKey);
byte[] rawBytes = raw.getBytes();
//签名
sign.update(rawBytes);
byte[] signature = sign.sign();
//转base64
String sign_data = Base64.getEncoder().encodeToString(signature);
ret.put("sign_data",sign_data);
/* ------------- RSA 加密 DES 秘钥------------------ */
Cipher rsa_enc_cipher = Cipher.getInstance("RSA");
// 用对方的公钥加密
rsa_enc_cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] rsa_enc_Data = rsa_enc_cipher.doFinal(desKey.getEncoded());
//转base64
String encrypt_key = Base64.getEncoder().encodeToString(rsa_enc_Data);
ret.put("encrypt_key",encrypt_key);
/* ------------- DES 加密业务数据 ------------------ */
Cipher des_enc_cipher = Cipher.getInstance("DES");
des_enc_cipher.init(Cipher.ENCRYPT_MODE, desKey);
byte[] des_enc_Data = des_enc_cipher.doFinal(rawBytes);
String seal_data = Base64.getEncoder().encodeToString(des_enc_Data);
ret.put("seal_data",seal_data);
return ret;
}
/**
* 解密,用我方私钥解密,对方公钥验签
*
* @param privateKey
* @param publicKey
* @return
* @throws Exception
*/
public static String decrypt(String encrypt_key,String seal_data,String sign_data,
PrivateKey privateKey, PublicKey publicKey) throws Exception {
/* ------------- RSA 解密 DES 秘钥------------------ */
//base64 解码
byte[] decodedKey = Base64.getDecoder().decode(encrypt_key);
// RSA 解密 DES 秘钥
Cipher rsa_dec_cipher = Cipher.getInstance("RSA");
// 用自己的私钥进行解密
rsa_dec_cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedKey = rsa_dec_cipher.doFinal(decodedKey);
//获取 DES 秘钥
//SecretKey desKey = new SecretKeySpec(decryptedKey, 0, decryptedKey.length, "DES");
DESKeySpec dks = new DESKeySpec(decryptedKey);
SecretKey desKey = SecretKeyFactory.getInstance("DES").generateSecret(dks);
/* ------------- DES 解密业务数据 ------------------ */
byte[] decodedSealData = Base64.getDecoder().decode(seal_data);
Cipher des_dec_cipher = Cipher.getInstance("DES");
des_dec_cipher.init(Cipher.DECRYPT_MODE, desKey);
byte[] rawBytes = des_dec_cipher.doFinal(decodedSealData);
String raw = new String(rawBytes, "UTF-8");
/* ------------- 验签 ------------------ */
Signature verifySign = Signature.getInstance("SHA1withRSA");
verifySign.initVerify(publicKey);
verifySign.update(rawBytes);
byte[] signature = Base64.getDecoder().decode(sign_data);
if (!verifySign.verify(signature)) {
throw new Exception("验签不通过!");
}
return raw;
}
public static PublicKey getPublicKey(String key) throws Exception {
byte[] decodedKey = Base64.getDecoder().decode(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] decodedKey = Base64.getDecoder().decode(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
}
由于时间上不允许我研究太久 实在没办法,求各位大哥帮帮忙
在PHP中启用OpenSSL扩展