就我想把通过java那边先通过sm2算法加密获取内容,在经过base64编码后的密文通过c++代码解密出来,java那边加密和解密的流程是这样的,现在给了我个密文为BNMcl56sscM7IkViF/xo/NBazYLAFOQGeCXbb9s/bmEjUn+tDW0NJjvrNZBpfGHIzp/ykGOKxf1S2wxC043GM0hYKvAClYTUZENppcNLgnmSf4Uv4q9YisFVc4NowbOHACRDtMluJ1fCVXo= ,明文为demo,怎么通过c++的代码实现解密的流程(不要用ai)
//私钥
private static final String privateKeyStr = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg84+DlFPtZ+gM5RW66O0FYPKD5udg91Eog67Kj43BjdKgCgYIKoEcz1UBgi2hRANCAAQQckJs4NWcjekZrtdASqgo6UctMPva7fYHthAn+zty6iAtIz/vALlNEJBs+lEORElzOCA/SjZqyJ9aLHXjjIFB";
//公钥
private static final String publicKeyStr = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEEHJCbODVnI3pGa7XQEqoKOlHLTD72u32B7YQJ/s7cuogLSM/7wC5TRCQbPpRDkRJczggP0o2asifWix144yBQQ==";
/**
* SM2加密算法
*
* @param data 明文数据
* @return
*/
public static String encrypt(String data) {
PublicKey publicKey = null;
try {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(publicKeyStr);
KeyFactory keyFactory = KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
publicKey = keyFactory.generatePublic(keySpec);
} catch (Exception e) {
log.error("SM2字符串公钥转换异常:{}", e.getMessage());
e.printStackTrace();
}
ECPublicKeyParameters ecPublicKeyParameters = null;
if (publicKey instanceof BCECPublicKey) {
BCECPublicKey bcecPublicKey = (BCECPublicKey) publicKey;
ECParameterSpec ecParameterSpec = bcecPublicKey.getParameters();
ECDomainParameters ecDomainParameters = new ECDomainParameters(ecParameterSpec.getCurve(),
ecParameterSpec.getG(), ecParameterSpec.getN());
ecPublicKeyParameters = new ECPublicKeyParameters(bcecPublicKey.getQ(), ecDomainParameters);
}
SM2Engine sm2Engine = new SM2Engine();
sm2Engine.init(true, new ParametersWithRandom(ecPublicKeyParameters, new SecureRandom()));
byte[] arrayOfBytes = null;
try {
byte[] in = data.getBytes(StandardCharsets.UTF_8);
arrayOfBytes = sm2Engine.processBlock(in, 0, in.length);
} catch (Exception e) {
log.error("SM2加密时出现异常:{}", e.getMessage());
}
return Base64.encodeBase64String(arrayOfBytes);
}
/**
* SM2解密算法
*
* @param cipherData 密文数据
* @return
*/
public static String decrypt(String cipherData) {
PrivateKey privateKey = null;
byte[] keyBytes;
try {
keyBytes = (new BASE64Decoder()).decodeBuffer(privateKeyStr); // 将私钥字符串进行 Base64 解码
KeyFactory keyFactory = KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
privateKey = keyFactory.generatePrivate(keySpec); // 使用解码后的字节数组生成私钥对象
} catch (Exception e) {
log.error("SM2字符串私钥转换异常:{}", e.getMessage());
e.printStackTrace();
}
BCECPrivateKey bcecPrivateKey = (BCECPrivateKey) privateKey;
ECParameterSpec ecParameterSpec = bcecPrivateKey.getParameters();
ECDomainParameters ecDomainParameters = new ECDomainParameters(ecParameterSpec.getCurve(),
ecParameterSpec.getG(), ecParameterSpec.getN());
ECPrivateKeyParameters ecPrivateKeyParameters = new ECPrivateKeyParameters(bcecPrivateKey.getD(),
ecDomainParameters); // 使用私钥对象创建 SM2 解密需要的 ECPrivateKeyParameters
SM2Engine sm2Engine = new SM2Engine();
sm2Engine.init(false, ecPrivateKeyParameters); // 初始化 SM2 引擎,使用私钥参数
String result = null;
byte[] arrayOfBytes = null;
try {
byte[] in = Base64.decodeBase64(cipherData); // 对密文进行 Base64 解码
arrayOfBytes = sm2Engine.processBlock(in, 0, in.length); // 对解码后的密文进行 SM2 解密
result = new String(arrayOfBytes, StandardCharsets.UTF_8); // 将解密结果转换为字符串
} catch (Exception e) {
log.error("SM2字符串私钥解密异常:{}", e.getMessage());
e.printStackTrace();
}
return result; // 返回解密结果
}