【Golang】国密SM2公钥私钥序列化到redis中并加密解密实战_sm2反编(1)

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介: 【Golang】国密SM2公钥私钥序列化到redis中并加密解密实战_sm2反编(1)
redisTemplate.Set(context.Background(), “privateKey”, privateKeyToString, g.OneDayDuration)
return
}
③测试用例
func TestSM2_Local_GenerateKeyPair(t *testing.T) {
// 生成密钥对
publicKeyObj, privateKeyObj := generateLoc()
// 输出公钥和私钥字符串
t.Log(“公钥字符串:”, PublicKeyToString(publicKeyObj))
// 040a5cccc33685eade33b0a1a40f1eea0f86ae93bd3cbb9f88fa466ca49a87bdbcd1ab65c9cb9f587a3b1f6d143f964acab78a23c2c37b1c16e2d16b796861f7bf
t.Log(“私钥字符串:”, PrivateKeyToString(privateKeyObj))
// 5b8037839b43ee13804e4c9fb3626b8949f0f58729547f0da4e8415481243b03
}
打印结果:
=== RUN TestSM2_Local_GenerateKeyPair
sm2_test.go:14: 公钥字符串:040a5cccc33685eade33b0a1a40f1eea0f86ae93bd3cbb9f88fa466ca49a87bdbcd1ab65c9cb9f587a3b1f6d143f964acab78a23c2c37b1c16e2d16b796861f7bf
sm2_test.go:16: 私钥字符串:5b8037839b43ee13804e4c9fb3626b8949f0f58729547f0da4e8415481243b03
— PASS: TestSM2_Local_GenerateKeyPair (0.00s)
PASS

2.使用公钥加密

①需求描述

要使用上面生成并缓存在redis中的公钥字符串,对内容进行加密.

②编写代码

将公钥字符串反序列化转为公钥对象:

// StringToPublicKey 公钥字符串还原为 sm2.PublicKey 对象(与java中org.bouncycastle.crypto生成的公私钥完全互通使用)
func StringToPublicKey(publicKeyStr string) (*sm2.PublicKey, error) {
publicKeyBytes, err := hex.DecodeString(publicKeyStr)
if err != nil {
return nil, err
}
// 提取 x 和 y 坐标字节切片
curve := sm2.P256Sm2().Params()
byteLen := (curve.BitSize + 7) / 8
xBytes := publicKeyBytes[1 : byteLen+1]
yBytes := publicKeyBytes[byteLen+1 : 2*byteLen+1]
// 将字节切片转换为大整数
x := new(big.Int).SetBytes(xBytes)
y := new(big.Int).SetBytes(yBytes)
// 创建 sm2.PublicKey 对象
publicKey := &sm2.PublicKey{
Curve: curve,
X: x,
Y: y,
}
return publicKey, nil
}
使用公钥对象加密字符串:
// publicKeyStr 公钥字符串, text 待加密明文字符串
func encryptLoc(publicKeyStr, text string) (string, error) {
publicKey, err := StringToPublicKey(publicKeyStr)
if err != nil {
return “”, err
}
encryptStr, _ := sm2.Encrypt(publicKey, []byte(text), rand.Reader, sm2.C1C2C3)
encodeToString := hex.EncodeToString(encryptStr)
fmt.Println(“加密后的字符串:”, encodeToString)
return strings.ToUpper(encodeToString), nil
}
从redis取公钥并加密明文, 将上面两个函数整合:
// SM2_Encrypt_Local sm2 本地加密
//
// @version latest
func SM2_Encrypt_Local(text string) (string, error) {
// 先检测redis中是否缓存有密钥
redisTemplate := redis.RedisTemplate// 你自己的redis客户端获取方式
publicKeyStr, _ := redisTemplate.Get(context.Background(), “publicKey”).Result()
publicKey := strings.ReplaceAll(publicKeyStr, “”", “”)
if publicKey == “” {
// 重新生成密钥
publicKey, _ = SM2_GenerateKeyPair_Local()
}
encrypt, err := encryptLoc(publicKey, text)
if err != nil {
return “”, err
}
return encrypt, nil
}
③测试用例
将字符串 ABC123加密
func TestSM2_Local_Encrypt(t *testing.T) {
publicKeyStr := “040a5cccc33685eade33b0a1a40f1eea0f86ae93bd3cbb9f88fa466ca49a87bdbcd1ab65c9cb9f587a3b1f6d143f964acab78a23c2c37b1c16e2d16b796861f7bf”
publicKey, _ := StringToPublicKey(publicKeyStr)
encryptStr, _ := sm2.Encrypt(publicKey, []byte(“ABC123”), rand.Reader, sm2.C1C2C3)
encodeToString := hex.EncodeToString(encryptStr)
t.Log(“加密后的字符串:”, encodeToString)
}
打印结果:
=== RUN TestSM2_Local_Encrypt
sm2_test.go:25: 加密后的字符串: 04d5bfb00dff8607a07337ff5999cbad8713a286c2655797148211dbf315b616faad48999428faf1597413d5d1cd81a425ccf192783c28cccd7a00ce618c759e29b82e5a34af99c34b0792d81026a6ca16317e76e11190c6564810a0737ceebb26a555498b23e0
— PASS: TestSM2_Local_Encrypt (0.00s)
PASS

3.使用私钥解密

①需求描述

要使用上面生成并缓存在redis中的私钥字符串,对加密的字符串进行解密

②编写代码
将私钥字符串反序列化转为私钥对象:
// StringToPrivateKey 私钥还原为 sm2.PrivateKey对象(与java中org.bouncycastle.crypto生成的公私钥完全互通使用)
func StringToPrivateKey(privateKeyStr string, publicKey *sm2.PublicKey) (*sm2.PrivateKey, error) {
privateKeyBytes, err := hex.DecodeString(privateKeyStr)
if err != nil {
return nil, err
}
// 将字节切片转换为大整数
d := new(big.Int).SetBytes(privateKeyBytes)
// 创建 sm2.PrivateKey 对象
privateKey := &sm2.PrivateKey{
PublicKey: *publicKey,
D: d,
}
return privateKey, nil
}
使用私钥对象解密密文字符串:
func decryptLoc(publicKeyStr, privateKeyStr, cipherText string) (string, error) {
publicKeyObj, err := StringToPublicKey(publicKeyStr)
if err != nil {
fmt.Println(err)
}
privateKeyObj, err := StringToPrivateKey(privateKeyStr, publicKeyObj)
if err != nil {
fmt.Println(err)
}
decodeString, err := hex.DecodeString(cipherText)
decrypt, err := sm2.Decrypt(privateKeyObj, decodeString, sm2.C1C2C3)
if err != nil {
fmt.Println(err)
}
resultStr := string(decrypt)
fmt.Println(“解密后的字符串:”, resultStr)
return resultStr, nil
}
从redis取私钥字符串并解密密文, 将上面两个函数整合:
// SM2_Decrypt_Local sm2 本地解密
//
// @version latest


相关文章
|
2月前
|
缓存 NoSQL Java
基于SpringBoot的Redis开发实战教程
Redis在Spring Boot中的应用非常广泛,其高性能和灵活性使其成为构建高效分布式系统的理想选择。通过深入理解本文的内容,您可以更好地利用Redis的特性,为应用程序提供高效的缓存和消息处理能力。
182 79
|
6月前
|
NoSQL 安全 测试技术
Redis游戏积分排行榜项目中通义灵码的应用实战
Redis游戏积分排行榜项目中通义灵码的应用实战
155 4
|
9月前
|
存储 缓存 NoSQL
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
111 2
|
8月前
|
Go
Golang生成随机数案例实战
关于如何使用Go语言生成随机数的三个案例教程。
243 91
Golang生成随机数案例实战
|
7月前
|
NoSQL 关系型数据库 MySQL
MySQL与Redis协同作战:优化百万数据查询的实战经验
【10月更文挑战第13天】 在处理大规模数据集时,传统的关系型数据库如MySQL可能会遇到性能瓶颈。为了提升数据处理的效率,我们可以结合使用MySQL和Redis,利用两者的优势来优化数据查询。本文将分享一次实战经验,探讨如何通过MySQL与Redis的协同工作来优化百万级数据统计。
362 5
|
8月前
|
JSON NoSQL Java
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
这篇文章介绍了在Java中使用Redis客户端的几种方法,包括Jedis、SpringDataRedis和SpringBoot整合Redis的操作。文章详细解释了Jedis的基本使用步骤,Jedis连接池的创建和使用,以及在SpringBoot项目中如何配置和使用RedisTemplate和StringRedisTemplate。此外,还探讨了RedisTemplate序列化的两种实践方案,包括默认的JDK序列化和自定义的JSON序列化,以及StringRedisTemplate的使用,它要求键和值都必须是String类型。
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
|
7月前
|
缓存 NoSQL Java
Spring Boot与Redis:整合与实战
【10月更文挑战第15天】本文介绍了如何在Spring Boot项目中整合Redis,通过一个电商商品推荐系统的案例,详细展示了从添加依赖、配置连接信息到创建配置类的具体步骤。实战部分演示了如何利用Redis缓存提高系统响应速度,减少数据库访问压力,从而提升用户体验。
324 2
|
7月前
|
NoSQL Java Redis
shiro学习四:使用springboot整合shiro,正常的企业级后端开发shiro认证鉴权流程。使用redis做token的过滤。md5做密码的加密。
这篇文章介绍了如何使用Spring Boot整合Apache Shiro框架进行后端开发,包括认证和授权流程,并使用Redis存储Token以及MD5加密用户密码。
120 0
shiro学习四:使用springboot整合shiro,正常的企业级后端开发shiro认证鉴权流程。使用redis做token的过滤。md5做密码的加密。
|
7月前
|
JSON 缓存 NoSQL
Redis 在线查看序列化对象技术详解
Redis 在线查看序列化对象技术详解
96 2
|
7月前
|
数据安全/隐私保护
sm2加密解密
sm2加密解密
121 3