參數(shù)加密說明
更新時間:
金融級實人認證方案支持在認證請求中使用SM2加密傳輸個人信息(姓名、身份證號碼)。您可以通過公鑰對認證請求中的個人信息參數(shù)進行加密。本文介紹如何開啟SM2參數(shù)加密。
公鑰
以下為金融級實人認證方案中刷臉認證時使用的SM2公鑰。
02cd77e007bdc86eeaf9a479ba7a2c22bc0a517ccb3a6975c3f94b4ac93347dea6
Java語言示例
添加Maven依賴
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.65</version>
</dependency>
示例代碼
import org.bouncycastle.asn1.gm.GMNamedCurves;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.jce.spec.ECPublicKeySpec;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.encoders.Hex;
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.util.Base64;
public class SM2EncryptUtils {
private static final String publicKey = "02cd77e007bdc86eeaf9a479ba7a2c22bc0a517ccb3a6975c3f94b4ac93347dea6";
private static Cipher cipher;
static {
try {
BouncyCastleProvider provider = new BouncyCastleProvider();
// 獲取SM2相關參數(shù)
X9ECParameters parameters = GMNamedCurves.getByName("sm2p256v1");
// 橢圓曲線參數(shù)規(guī)格
ECParameterSpec ecParameterSpec = new ECParameterSpec(parameters.getCurve(), parameters.getG(), parameters.getN(), parameters.getH());
// 將公鑰HEX字符串轉(zhuǎn)換為橢圓曲線對應的點
ECPoint ecPoint = parameters.getCurve().decodePoint(Hex.decode(publicKey));
// 獲取橢圓曲線KEY生成器
KeyFactory keyFactory = KeyFactory.getInstance("EC", provider);
// 將橢圓曲線點轉(zhuǎn)為公鑰KEY對象
BCECPublicKey bcecPublicKey = (BCECPublicKey) keyFactory.generatePublic(new ECPublicKeySpec(ecPoint, ecParameterSpec));
// 獲取SM2加密器
cipher = Cipher.getInstance("SM2", provider);
// 初始化為加密模式
cipher.init(Cipher.ENCRYPT_MODE, bcecPublicKey);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* SM2 加密
* @param content 需要加密的內(nèi)容
* @return 加密后的密文
*/
public static String encrypt(String content) {
try {
String contentEncrypt = Base64.getEncoder().encodeToString(cipher.doFinal(content.getBytes("UTF-8")));
return contentEncrypt;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
文檔內(nèi)容是否對您有幫助?