文章目录
- 前言
- 一. Jakarta代码
- 二. TypeScript
- 三.golang
前言
最近还要深度研究hutools底层实现,一定要搞透澈,本章将会是持续更新 所有密钥由Jakarta统一生成,因为没测试其他语言生成是否可以
参考资料:
Java代码实现SM2算法以及注意点总结(踩坑记录)
国密算法工具Smutil
一. Jakarta代码
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.29</version></dependency><dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15to18</artifactId><version>1.69</version></dependency>
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.ECKeyUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.symmetric.SM4;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;import java.nio.charset.StandardCharsets;
import java.security.KeyPair;/*** @author YuanJie* @date 2024/8/6 上午8:44* 加密方式* 加密方式里边,有两种 一种是 C1C3C2另一种是C1C2C3,* 这两种加密方式不同,当时找的资料说是,旧版本的标准上,* 使用的是C1C2C3,但是后续应该是更新过,使用的是C1C3C2,* 其他语言,比如Python或者Golang或者C的实现大多直接就是C1C3C2的,* 但是如果java中使用的bouncycastle的包,默认使用的是C1C2C3,* 就会发生与其他语言的加密结果不能相互解密的情况,但是可能你跟其他人的Java系统加解密又没有问题。* <p>* 导出* Java生成的密钥,在导出的时候,最好使用 PrivateKey.getD().toByteArray() 和 PublicKey.getQ().getEncoded() 导出密钥,然后再转成Base64或者Hex给其他系统;* <p>* 导入* 导入的时候,请使用我上边的代码转换成对应的公钥或者私钥的对象,* 这里有另一个容易出错的地方,在私钥byte[]转私钥对象的时候,* 有些人会使用new BigInteger(byte[])这个方法将byte[]转为BigInteger,* 然后调用 keyFactory.generatePrivate(new ECPrivateKeySpec(BigInteger, ECParameterSpec)),* 但是实测这样在一些情况下会报错,这个时间长了记不清具体原因了,好像是因为第一位为负数的情况下会报错,* 我的方法是把byte[]转为Hex,然后再使用new BigInteger(hexStr, int)这种方式转为BigInteger,这个需要注意。* <p>* 公钥压缩* publicKey.getQ().getEncoded()这个方法,* 有一个boolean参数,控制输出的密钥是否为压缩后的密钥,* 输出内容转为Hex之后,02和03开头的为压缩后的密钥,04表示未经压缩的密钥。* Java里边压缩为压缩的调用同一个方法就能转为公钥对象,但是其他语言的目前不清楚,所以导出的时候,最好标注一下压缩或者未压缩。* <p>* Sm2签名时,有一个userId的概念,这个东西一般直接用CipherParameters对象是带不进去的,* 如果没有,默认是 Hex.decodeStrict(“31323334353637383132333435363738”) ,* 也就是1234567812345678的Ascii值,如果要使用自定义的userId,则需要使用ParametersWithID这个对象调用Signer的init,* 这个对象可以传入一个CipherParameters,然后再传入一个userId,可以把制定的userId带进去。* <p>* 签名验签RS* 我们平常接触的算法,一般我们调用加解密算法只返回一个值,* 但是Sm2算法,签名其实是有两个值,一个R,一个S,* 两个值构成一个签名结果,Java中bouncycastle的返回虽然也是一个值,* 但是大概看了一下算法的实现代码,其实得出的结果也是两个值,* 一个R,一个S,然后通过一个方法拼接成一个值(不确定这个方法的转换方式是不是有标准的)。* 在我们与C程序一块测试的时候,他们反馈了这个问题,* 然后我对着bouncycastle包内的org.bouncycastle.crypto.signers.SM2Signer类,* 摘出来了R、S和单一返回值的转换代码,已经提取到上边的代码里,可以直接使用。*/
@Configuration
@Slf4j
public class NationalSecretsUtils {public static String privateKey2;public static String publicKey2;public static String secretKey4;public NationalSecretsUtils(@Value("${sm2.privateKey}") String privateKey2,@Value("${sm2.publicKey}") String publicKey2,@Value("${sm4.secretKey}") String secretKey4) {NationalSecretsUtils.privateKey2 = privateKey2;NationalSecretsUtils.publicKey2 = publicKey2;NationalSecretsUtils.secretKey4 = secretKey4;}public static void main(String[] args) {KeyPair keyPair = SecureUtil.generateKeyPair("SM2");// 2. 获取公私钥//这里公钥不压缩 公钥的第一个字节用于表示是否压缩 可以不要 65字节变64字节byte[] publicKey = ((BCECPublicKey) keyPair.getPublic()).getQ().getEncoded(false);byte[] privateKey = ((BCECPrivateKey) keyPair.getPrivate()).getD().toByteArray();String sm2pub = HexUtil.encodeHexStr(publicKey);String sm2pri = HexUtil.encodeHexStr(privateKey);NationalSecretsUtils.publicKey2 = sm2pub;NationalSecretsUtils.privateKey2 = sm2pri;NationalSecretsUtils.secretKey4 = HexUtil.encodeHexStr(SecureUtil.generateKey("SM4").getEncoded());log.info("sm2公钥:{}", sm2pub);log.info("sm2私钥:{}", sm2pri);log.info("sm4密钥:{}", NationalSecretsUtils.secretKey4);// 测试sm2加解密log.info("sm2加密:{}", HexUtil.encodeHexStr(encryptSM2Sign("我是sm2数据")));log.info("sm2解密:{}", decryptSM2Sign(encryptSM2Sign("我是sm2解密数据")));// 测试sm2加签名验证log.info("sm2签名:{}", HexUtil.encodeHexStr(generateSM2Sign("我是sm2数据")));log.info("sm2验证:{}", verifySM2Sign("我是sm2数据", generateSM2Sign("我是sm2数据")));// 测试sm4加解密log.info("sm4加密:{}", HexUtil.encodeHexStr(generateSM4Sign("我是sm4数据")));log.info("sm4解密:{}", decryptSM4Sign(generateSM4Sign("我是sm4数据")));// 测试sm3哈希算法log.info("sm3哈希算法:{}", generateSM3Sign("我是sm3数据"));log.info("sm3验证:{}", verifySM3Sign("我是sm3数据", generateSM3Sign("我是sm3数据")));}/*** SM2 加密*/public static byte[] encryptSM2Sign(String text) {SM2 sm2 = new SM2(null, publicKey2);sm2.usePlainEncoding();return sm2.encrypt(text.getBytes(StandardCharsets.UTF_8));}/*** SM2 解密*/public static String decryptSM2Sign(byte[] text) {SM2 sm2 = new SM2(privateKey2, null);sm2.usePlainEncoding();return new String(sm2.decrypt(text), StandardCharsets.UTF_8);}/*** SM2签名 使用私钥D值签名* 默认C1C3C2**/public static byte[] generateSM2Sign(String text) {SM2 sm2 = new SM2(privateKey2, null, null);sm2.usePlainEncoding();return sm2.sign(text.getBytes(StandardCharsets.UTF_8), null);}/*** SM2验证 使用公钥Q值验证签名*/public static boolean verifySM2Sign(String text, byte[] sign) {SM2 sm2 = new SM2(null, ECKeyUtil.toSm2PublicParams(publicKey2));sm2.usePlainEncoding();return sm2.verify(text.getBytes(StandardCharsets.UTF_8), sign);}/*** SM2*//*** SM3 哈希算法 3是能够计算出256比特的散列值的单向散列函数,主要用于数字签名和消息认证码。* base16*/public static String generateSM3Sign(String text) {return SmUtil.sm3(text);}/*** SM3验证**/public static boolean verifySM3Sign(String text, String sign) {return SmUtil.sm3(text).equals(sign);}/*** SM4 是一种对称加密算法,它使用128位密钥,使用分组密码模式,使用CBC模式加密。* SM4加密*/public static String generateSM4Sign(String text) {SM4 sm4 = SmUtil.sm4(HexUtil.decodeHex(secretKey4));return sm4.encryptHex(text);}/*** SM4解密*/public static String decryptSM4Sign(String text) {SM4 sm4 = SmUtil.sm4(HexUtil.decodeHex(secretKey4));return sm4.decryptStr(text, StandardCharsets.UTF_8);}
}
提取这里数据,供ts和go自检
二. TypeScript
npm install sm-crypto
/*** npm install sm-crypto* https://www.npmjs.com/package/sm-crypto*/
import {sm2, sm3, sm4} from 'sm-crypto';export class NationalSecretsUtils {static privateKey2: string;static publicKey2: string;static secretKey4: string;constructor(privateKey2: string, publicKey2: string, secretKey4: string) {NationalSecretsUtils.privateKey2 = privateKey2;NationalSecretsUtils.publicKey2 = publicKey2;NationalSecretsUtils.secretKey4 = secretKey4;}static main() {console.log('sm2公钥:', NationalSecretsUtils.publicKey2);console.log('sm2私钥:', NationalSecretsUtils.privateKey2);console.log('sm4密钥:', NationalSecretsUtils.secretKey4);const sm2Sign = NationalSecretsUtils.generateSM2Sign("我是sm2数据");console.log('sm2加密:', NationalSecretsUtils.generateSM2Encrypt("我是sm2数据"));console.log('sm2解密:', NationalSecretsUtils.generateSM2Decrypt(NationalSecretsUtils.generateSM2Encrypt("我是sm2数据")));console.log('sm2签名:', sm2Sign);console.log('sm2验证:', NationalSecretsUtils.verifySM2Sign("我是sm2数据", sm2Sign));const sm4Encrypt = NationalSecretsUtils.generateSM4Sign("我是sm4数据");console.log('sm4加密:', sm4Encrypt);console.log('sm4解密:', NationalSecretsUtils.decryptSM4Sign(sm4Encrypt));const sm3Hash = NationalSecretsUtils.generateSM3Sign("我是sm3数据");console.log('sm3哈希算法:', sm3Hash);console.log('sm3验证:', NationalSecretsUtils.verifySM3Sign("我是sm3数据", sm3Hash));}/*** 生成sm2加密*/static generateSM2Encrypt(text: string): string {return sm2.doEncrypt(text, NationalSecretsUtils.publicKey2);}/*** 生成sm2解密*/static generateSM2Decrypt(text: string): string {return sm2.doDecrypt(text, NationalSecretsUtils.privateKey2);}/*** 生成sm2签名* @param text*/static generateSM2Sign(text: string): string {return sm2.doSignature(text, NationalSecretsUtils.privateKey2);}/*** 验证sm2签名* @param text* @param sign*/static verifySM2Sign(text: string, sign: string): boolean {return sm2.doVerifySignature(text, sign, NationalSecretsUtils.publicKey2);}/*** 生成sm3哈希算法* @param text*/static generateSM3Sign(text: string): string {return sm3(text);}/*** 验证sm3哈希算法* @param text* @param sign*/static verifySM3Sign(text: string, sign: string): boolean {return sm3(text) === sign;}/*** 生成sm4加密* @param text*/static generateSM4Sign(text: string): string {const key = NationalSecretsUtils.secretKey4;return sm4.encrypt(text,key);}/*** 生成sm4解密* @param text*/static decryptSM4Sign(text: string): string {const key = NationalSecretsUtils.secretKey4;return sm4.decrypt(text,key);}
}// 示例调用
const nsu = new NationalSecretsUtils('00900b2ba339026a0ab4f5d7bfc03c640084c5e178ac88a3b9d7ab1184957897a1','040ff81a6e29ba81e7997740f5c1f010a98e9782ed3c659868e506acf4ab3ff8922737d8e8b10799ae1e2ffb3a7009e8a81f02a8ed7d95db2133cc82522b901bf3','60bfa3a9b5d6531b690c0ca2096f2e2e');
NationalSecretsUtils.main();
三.golang
package mainimport ("bytes""crypto/rand""encoding/hex""fmt""github.com/tjfoc/gmsm/sm2""github.com/tjfoc/gmsm/sm3""github.com/tjfoc/gmsm/sm4""log""math/big"
)/**
* go get -u github.com/tjfoc/gmsm@v1.4.1*/
type NationalSecretsUtils struct {PrivateKey2 *sm2.PrivateKeyPublicKey2 *sm2.PublicKeySecretKey4 []byte
}func NewNationalSecretsUtils(privateKeyHex2, publicKeyHex2, secretKeyHex string) *NationalSecretsUtils {// Decode SM2 private keyprivateKeyBytes, err := hex.DecodeString(privateKeyHex2)if err != nil {log.Fatalf("Failed to decode SM2 private key: %v", err)}privateKey := new(sm2.PrivateKey)privateKey.D = new(big.Int).SetBytes(privateKeyBytes)// Initialize the rest of the private key fieldsprivateKey.Curve = sm2.P256Sm2()privateKey.PublicKey.Curve = privateKey.CurveprivateKey.PublicKey.X, privateKey.PublicKey.Y = privateKey.Curve.ScalarBaseMult(privateKey.D.Bytes())// Decode SM2 public keypublicKeyBytes, err := hex.DecodeString(publicKeyHex2)if err != nil {log.Fatalf("Failed to decode SM2 public key: %v", err)}publicKey := new(sm2.PublicKey)publicKey.Curve = sm2.P256Sm2()publicKey.X = new(big.Int).SetBytes(publicKeyBytes[1:33])publicKey.Y = new(big.Int).SetBytes(publicKeyBytes[33:65])// Decode SM4 secret keysecretKey, err := hex.DecodeString(secretKeyHex)if err != nil {log.Fatalf("Failed to decode SM4 secret key: %v", err)}if len(secretKey) != 16 {log.Fatalf("Invalid SM4 secret key length: expected 16 bytes, got %d bytes", len(secretKey))}return &NationalSecretsUtils{PrivateKey2: privateKey,PublicKey2: publicKey,SecretKey4: secretKey,}
}/**
* SM2加密*/
func (nsu *NationalSecretsUtils) EncryptSM2(text string) string {// 0 -> C1C3C2 1->C1C2C3cipher, err := sm2.Encrypt(nsu.PublicKey2, []byte(text), nil, 0)if err != nil {log.Fatalf("Failed to encrypt text: %v", err)}return hex.EncodeToString(cipher)
}/**
* SM2解密*/
func (nsu *NationalSecretsUtils) DecryptSM2(ciphertextHex string) string {ciphertext, err := hex.DecodeString(ciphertextHex)if err != nil {log.Fatalf("Failed to decode ciphertext: %v", err)}plaintext, err := sm2.Decrypt(nsu.PrivateKey2, ciphertext, 0)if err != nil {log.Fatalf("Failed to decrypt text: %v", err)}return string(plaintext)
}/**
* 生成SM2签名*/
func (nsu *NationalSecretsUtils) GenerateSM2Sign(text string) []byte {signature, err := nsu.PrivateKey2.Sign(rand.Reader, []byte(text), nil)if err != nil {log.Fatalf("Failed to generate SM2 signature: %v", err)}return signature
}/**
* 验证SM2签名*/
func (nsu *NationalSecretsUtils) VerifySM2Sign(text string, signature []byte) bool {ok := nsu.PublicKey2.Verify([]byte(text), signature)return ok
}/**
* 生成SM3签名*/
func (nsu *NationalSecretsUtils) GenerateSM3Sign(text string) string {hash := sm3.New()hash.Write([]byte(text))return hex.EncodeToString(hash.Sum(nil))
}/**
* 验证SM3签名*/
func (nsu *NationalSecretsUtils) VerifySM3Sign(text string, sign string) bool {return nsu.GenerateSM3Sign(text) == sign
}/**
* 生成SM4签名*/
func (nsu *NationalSecretsUtils) GenerateSM4Sign(text string) string {cipher, err := sm4.NewCipher(nsu.SecretKey4)if err != nil {log.Fatalf("Failed to create SM4 cipher: %v", err)}plaintext := []byte(text)padding := sm4.BlockSize - len(plaintext)%sm4.BlockSizepadText := bytes.Repeat([]byte{byte(padding)}, padding)plaintext = append(plaintext, padText...)ciphertext := make([]byte, len(plaintext))cipher.Encrypt(ciphertext, plaintext)return hex.EncodeToString(ciphertext)
}/**
* 解密SM4签名*/
func (nsu *NationalSecretsUtils) DecryptSM4Sign(ciphertextHex string) string {cipher, err := sm4.NewCipher(nsu.SecretKey4)if err != nil {log.Fatalf("Failed to create SM4 cipher: %v", err)}ciphertext, err := hex.DecodeString(ciphertextHex)if err != nil {log.Fatalf("Failed to decode hex string: %v", err)}plaintext := make([]byte, len(ciphertext))cipher.Decrypt(plaintext, ciphertext)padding := int(plaintext[len(plaintext)-1])return string(plaintext[:len(plaintext)-padding])
}func main() {// 自定义的密钥,可以替换成实际使用的密钥privateKeyHex2 := "00900b2ba339026a0ab4f5d7bfc03c640084c5e178ac88a3b9d7ab1184957897a1"publicKeyHex2 := "040ff81a6e29ba81e7997740f5c1f010a98e9782ed3c659868e506acf4ab3ff8922737d8e8b10799ae1e2ffb3a7009e8a81f02a8ed7d95db2133cc82522b901bf3"secretKeyHex := "60bfa3a9b5d6531b690c0ca2096f2e2e"nsu := NewNationalSecretsUtils(privateKeyHex2, publicKeyHex2, secretKeyHex)fmt.Printf("sm2公钥: %s\n", hex.EncodeToString(nsu.PublicKey2.X.Bytes())+hex.EncodeToString(nsu.PublicKey2.Y.Bytes()))fmt.Printf("sm2私钥: %s\n", hex.EncodeToString(nsu.PrivateKey2.D.Bytes()))fmt.Printf("sm4密钥: %s\n", hex.EncodeToString(nsu.SecretKey4))// 测试SM2加密和解密fmt.Printf("sm2加密: %s\n", nsu.EncryptSM2("我是sm2数据"))fmt.Printf("sm2解密: %s\n", nsu.DecryptSM2(nsu.EncryptSM2("我是sm2数据")))// 测试SM2签名和验证sm2Sign := nsu.GenerateSM2Sign("我是sm2数据")fmt.Printf("sm2签名: %s\n", hex.EncodeToString(sm2Sign))fmt.Printf("sm2验证: %v\n", nsu.VerifySM2Sign("我是sm2数据", sm2Sign))// 测试SM4加解密sm4Encrypt := nsu.GenerateSM4Sign("我是sm4数据")fmt.Printf("sm4加密: %s\n", sm4Encrypt)fmt.Printf("sm4解密: %s\n", nsu.DecryptSM4Sign(sm4Encrypt))// 测试SM3哈希算法sm3Hash := nsu.GenerateSM3Sign("我是sm3数据")fmt.Printf("sm3哈希算法: %s\n", sm3Hash)fmt.Printf("sm3验证: %v\n", nsu.VerifySM3Sign("我是sm3数据", sm3Hash))
}