Closed HelloYang666 closed 1 month ago
这个java是对方的还是你封装的, 有两个问题: 1 SM2Engine sm2Engine = new SM2Engine(mode = Mode.C1C2C3 ); 这里有个参数 mode , 不填的话,缺省是 c1c2c3样式的, 新国密标准是c1c3c2, 这个参数可调,JAVA中没填就是 c1c2c3模式,请注意 2 这个BC库加密出来是 byte[] 返回直接, JAVA代码中 是 arrayOfBytes = Base64.getEncoder().encode(sm2Engine.processBlock(in, 0, in.length)); 函数中base64 了,这个也没问题,但后面最后返回又转成hex, 正常是返回base64或是hex, 这个加了两重了,用PHP解密的时候也得多解一次hex
// php中这么解就行了,
$m2EncryptData = '42504c66646b4d6e4a55646d69566145644447616c4c524b6747784a4b516674564b744374774c69614235506d546f666336384c485359774e68393974746c585a534547524e6b6339464270304670312b4e41506233687755583179715a754c38724f49444e624663316268416f352f4c38442f506c525a595a68586c4d7771537356643337796f68773d3d';
$m2EncryptData = bin2hex(base64_decode(hex2bin($m2EncryptData)));
$privateKey = "f49ea2f274c5a982..........12c2d4c6c4c532f5e3a04cb1";
$m2DecryptData = $sm2->doDecrypt($m2EncryptData,$privateKey,1,C1C2C3);
echo ("\n解密后: ".$m2DecryptData);
如果你用PHP加密也注意下 比如我的代码生成的是hex ==》bin=>base64==>hex 注意下,java 缺省用的 c1c2c3模式,php缺省是c1c3c2模式,加密的时候加上模式参数
例子中解开后是“成功”二字
这个java是对方的还是你封装的,有两个问题: 1 SM2Engine sm2Engine = new SM2Engine(mode = Mode.C1C2C3 ); 这里有个参数模式,不填的话,是c1c2c3风格的,新国密标准是c1c3c2,这个参数可调,JAVA中没填就是c1c2c3模式,请注意 2这个BC库加密出来的是byte[]返回直接,JAVA代码中 是 arrayOfBytes = Base64.getEncoder().encode(sm2Engine.processBlock(in, 0, in.length)); 函数中base64了,这个也没有问题,但是后面最后返回又转成hex,正常是返回base64或者hex,这个加了两次重了,用PHP解密的时候也解解了一次hex
// php中这么解就行了, $m2EncryptData = '42504c66646b4d6e4a55646d69566145644447616c4c524b6747784a4b516674564b744374774c69614235506d546f666336384c485359774e68393974746c585a534547524e6b6339464270304670312b4e41506233687755583179715a754c38724f49444e624663316268416f352f4c38442f506c525a595a68586c4d7771537356643337796f68773d3d'; $m2EncryptData = bin2hex(base64_decode(hex2bin($m2EncryptData))); $privateKey = "f49ea2f274c5a982..........12c2d4c6c4c532f5e3a04cb1"; $m2DecryptData = $sm2->doDecrypt($m2EncryptData,$privateKey,1,C1C2C3); echo ("\n解密后: ".$m2DecryptData);
如果你用PHP加密的是也注意下比如我的代码生成hex ==》bin=>base64==>hex注意下,java 操作用的 c1c2c3 模式,php 调用是 c1c3c2 模式,加密的加上时候模式参数
java是对方封装的,我这边使用的php。使用你写的php解密可以正常解密了,但是加密还是不行,麻烦再帮忙给看看
/**
* phpSm2加密
*/
public static function doEncrypt($arguments)
{
$m2EncryptData = self::$sm2->doEncrypt($arguments, self::$publicKey, C1C2C3);
$result = bin2hex(base64_encode(hex2bin($m2EncryptData)));
return $result;
}
php使用sm2加密后,java无法解密。java加密的数据php也无法解密
### java代码 package com.aisino.risk.detection.business.util; import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Base64;
import org.bouncycastle.asn1.gm.GMNamedCurves; import org.bouncycastle.asn1.x9.X9ECParameters; import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.InvalidCipherTextException; import org.bouncycastle.crypto.KeyGenerationParameters; import org.bouncycastle.crypto.engines.SM2Engine; import org.bouncycastle.crypto.generators.ECKeyPairGenerator; import org.bouncycastle.crypto.params.ECDomainParameters; import org.bouncycastle.crypto.params.ECKeyGenerationParameters; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.params.ParametersWithRandom; import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.util.encoders.Hex;
import com.aisino.risk.common.vo.Sm2KeyVo; import com.aisino.risk.common.web.ex.InterruptBusinessException;
public class SM2Util { private static String CHARSET_UTF8="UTF-8";
public static void main(String[] args) throws InvalidCipherTextException, UnsupportedEncodingException, NoSuchAlgorithmException {
// //region 生成公私钥 Sm2KeyVo Sm2KeyVo = getKeys(); String qifuClientsm2pubkey = Sm2KeyVo.getPublicKey(); System.out.println("sm2pubkey:" + qifuClientsm2pubkey); String qifuServersm2prikey = Sm2KeyVo.getPrivateKey(); System.out.println("PrivateKey:" + qifuServersm2prikey); //endregion
// String content = "{\"openId\":\"11\",\"uniqueId\":\"11\"}"; // String publicKey = "04e00a77ba5c1f50a2a3144b0fa2eb1bda2e00bb5c56bf10f2e3808429b30e1a2836c1c4e664e12f84749fd14eeb0b60d66dd15c56e7dc527c930778aaa60ae433"; // String s = encryptByPublicKey(content, publicKey); // System.out.println(s); //endregion
// String ciphertext = "42504c66646b4d6e4a55646d69566145644447616c4c524b6747784a4b516674564b744374774c69614235506d546f666336384c485359774e68393974746c585a534547524e6b6339464270304670312b4e41506233687755583179715a754c38724f49444e624663316268416f352f4c38442f506c525a595a68586c4d7771537356643337796f68773d3d"; // String responseEncry = decryptByPrivateKey(ciphertext, "f49ea2f2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5e3a04cb1"); // System.out.println(responseEncry); //endregion
//
} }