国密算法c#和Java互通

最近和外部系统对接,使用了国密算法,且我们这边是c#,对方是Java,折腾了一番后,把踩过的坑记录下来,有需要的可以参考。

一、密钥生成

我们这边生成密钥是用的bouncycastle.crypto库,一开始对方是用Java自带的加密库生成的密钥,结果是双方都使用不了对方的密钥,后来我们两边都统一使用bouncycastle.crypto库来生成密钥,问题解决。

二、密文格式

c#这边默认的密文格式是C1C2C3,而Java那边使用格式一开始是C1C3C2,导致加解密、加验签不成功,后来两边统一使用C1C2C3,问题解决。

总结:c#和Java国密互通,两边的密钥生成以及加解密、加验签都要统一使用bouncycastle.crypto库,密文格式也最好统一,如果不统一,需要C1C2C3和C1C2C3进行转换。

附加解密、加验签的代码

加密

cs 复制代码
public string EncryptM2(string message, string key)
        {
            X9ECParameters curve = ECNamedCurveTable.GetByName("sm2p256v1");
            ECPoint q = curve.Curve.DecodePoint(Base64.Decode(key));
            ECDomainParameters domain = new ECDomainParameters(curve);
            ECPublicKeyParameters pubk = new ECPublicKeyParameters("EC", q, domain);

            // 创建SM2加密器
            SM2Engine sm2Engine = new SM2Engine();
            sm2Engine.Init(true, new ParametersWithRandom(pubk, new SecureRandom()));

            byte[] dataBytes = Encoding.UTF8.GetBytes(message);
            byte[] encryptedData = sm2Engine.ProcessBlock(dataBytes, 0, dataBytes.Length);

            return Base64.ToBase64String(encryptedData);
        }

解密

cs 复制代码
public string DecryptM2(string message, string key)
        {
            X9ECParameters curve = ECNamedCurveTable.GetByName("sm2p256v1");
            ECDomainParameters domain = new ECDomainParameters(curve);
            BigInteger d = new BigInteger(1, Base64.Decode(key));
            ECPrivateKeyParameters prik = new ECPrivateKeyParameters(d, domain);

            // 创建SM2加密器
            SM2Engine sm2Engine = new SM2Engine();
            sm2Engine.Init(false, prik);

            byte[] encryptedData = Base64.Decode(message);
            byte[] decryptedData = sm2Engine.ProcessBlock(encryptedData, 0, encryptedData.Length);

            return Encoding.UTF8.GetString(decryptedData);
        }

加签

cs 复制代码
public string SignM2(string strPrivateKey, string strData)
        {
            byte[] messageBytes = System.Text.Encoding.UTF8.GetBytes(strData);

            X9ECParameters curve1 = ECNamedCurveTable.GetByName("sm2p256v1");
            ECDomainParameters domain1 = new ECDomainParameters(curve1);
            BigInteger d = new BigInteger(1, Base64.Decode(strPrivateKey));
            ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(d, domain1);

            ISigner signer = SignerUtilities.GetSigner("SM3withSM2");
            signer.Init(true, new ParametersWithRandom(privKey, new SecureRandom()));
            signer.BlockUpdate(messageBytes, 0, messageBytes.Length);
            byte[] signature = signer.GenerateSignature();

            return Base64.ToBase64String(signature);
        }

验签

cs 复制代码
public bool verifySignM2(string strPublickey, string strData, string strSign)
        {
            byte[] messageBytes = System.Text.Encoding.UTF8.GetBytes(strData);
            byte[] signature = Base64.Decode(strSign);

            X9ECParameters curve = ECNamedCurveTable.GetByName("sm2p256v1");
            ECPoint q = curve.Curve.DecodePoint(Base64.Decode(strPublickey));
            ECDomainParameters domain = new ECDomainParameters(curve);
            ECPublicKeyParameters pubk = new ECPublicKeyParameters("EC", q, domain);

            ISigner signer = SignerUtilities.GetSigner("SM3withSM2");
            signer.Init(false, pubk);
            signer.BlockUpdate(messageBytes, 0, messageBytes.Length);
            bool isVerified = signer.VerifySignature(signature);
            return isVerified;
        }
相关推荐
名字还没想好☜6 小时前
Python f-string 进阶:数字格式化、对齐填充、调试 = 号与嵌套表达式
开发语言·数据库·python·字符串格式化·f-string
·薯条大王7 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
hez20109 小时前
.NET 11 Runtime Async 详解
c#·.net·.net core
一壶浊酒..10 小时前
Scrape Webpack练习
开发语言·javascript·webpack
ZJH__GO10 小时前
网络编程v4pro--实现聊天室文件传输功能
java·服务器·网络·计算机网络
程序员黑豆10 小时前
Windows 系统 Java 环境变量配置全攻略:解决“不是内部或外部命令”
java·前端·ai编程
命运之光10 小时前
【C语言完整代码】就诊信息管理系统
java·c语言·开发语言
凤山老林10 小时前
Spring Boot @Async 线上实战:从默认配置到生产级线程池治理
java·spring boot·后端
命运之光11 小时前
【C语言完整代码】图书管理系统:图书馆场景下的增删改查实战
c语言·开发语言
猿长大人11 小时前
C# | Serilog 新手入门
开发语言·c#·.net·log