您的位置:首页 > 科技 > 能源 > 免费二维码生成工具_设计师怎么弄个人网站_网站排名查询站长之家_公司seo推广营销网站

免费二维码生成工具_设计师怎么弄个人网站_网站排名查询站长之家_公司seo推广营销网站

2024/10/5 23:22:29 来源:https://blog.csdn.net/2301_77171572/article/details/142179380  浏览:    关键词:免费二维码生成工具_设计师怎么弄个人网站_网站排名查询站长之家_公司seo推广营销网站
免费二维码生成工具_设计师怎么弄个人网站_网站排名查询站长之家_公司seo推广营销网站

初级代码游戏的专栏介绍与文章目录-CSDN博客

我的github:codetoys,所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。

这些代码大部分以Linux为目标但部分代码是纯C++的,可以在任何平台上使用。

github源码指引的指引-CSDN博客


        本文是国密起步6:GmSSL3使用SM4自定义格式加解密C++版-CSDN博客的对应C#版。

        GmSSL没有C#接口,所以C#上要用别的库,比如BouncyCastle这个著名加解密库。nuget上的名字是BouncyCastle.Cryptography。

一、源码

        因为是跟C++版是对照关系,直接上源码了:

	static public string gm4DecryptMessage(string text, string userkey){try{byte[] encryptdata = Convert.FromBase64String(text);//格式为版本1字节、IV16字节、加密后的数据(第一个块是明文长度,仅用8字节,其余未用)byte[] key = new byte[16];//密钥var bytesUserKey = Encoding.UTF8.GetBytes(userkey);for (int i = 0; i < 16; ++i){if (i < userkey.Length) key[i] = bytesUserKey[i];else key[i] = 0;}KeyParameter Key = ParameterUtilities.CreateKeyParameter("SM4", key);ParametersWithIV keyParamWithIv = new ParametersWithIV(Key, encryptdata.Skip(1).Take(16).ToArray());IBufferedCipher inCipher = CipherUtilities.GetCipher("SM4/CBC/NoPadding");inCipher.Init(false, keyParamWithIv);byte[] full_plaindata = inCipher.ProcessBytes(encryptdata.Skip(17).ToArray());int nPlainLength = (int)BitConverter.ToInt64(full_plaindata.Take(8).ToArray());text = Encoding.UTF8.GetString(full_plaindata.Skip(16).Take(nPlainLength).ToArray());}catch (Exception ex){text = ex.ToString();}return text;}static public string gm4EncryptMessage(string text, string userkey){try{//格式为版本1字节、IV16字节、加密后的数据(第一个块是明文长度,仅用8字节,其余未用)byte[] key = new byte[16];//密钥var bytesUserKey = Encoding.UTF8.GetBytes(userkey);for (int i = 0; i < 16; ++i){if (i < userkey.Length) key[i] = bytesUserKey[i];else key[i] = 0;}byte[] IV = new byte[16];Random rand = new Random();for (int i = 0; i < 16; ++i) IV[i] = (byte)rand.Next(0, 255);KeyParameter Key = ParameterUtilities.CreateKeyParameter("SM4", key);ParametersWithIV keyParamWithIv = new ParametersWithIV(Key, IV);IBufferedCipher inCipher = CipherUtilities.GetCipher("SM4/CBC/NoPadding");inCipher.Init(true, keyParamWithIv);int plainBufLen;//明文缓冲区长度,第一个块是明文长度,其后是原始数据,按照16字节补齐if (0 == text.Length % 16) plainBufLen = 16 + text.Length;else plainBufLen = 16 + (text.Length / 16 + 1) * 16;byte[] plainBuf = new byte[plainBufLen];BitConverter.GetBytes((long)text.Length).CopyTo(plainBuf, 0);//必须是8位整数Encoding.UTF8.GetBytes(text).CopyTo(plainBuf, 16);byte[] tmp = new byte[1 + 16 + plainBufLen];//输出缓冲区tmp[0] = 1;IV.CopyTo(tmp, 1);byte[] cipher = inCipher.ProcessBytes(plainBuf);cipher.CopyTo(tmp, 17);text = "G" + Convert.ToBase64String(tmp);}catch (Exception ex){text = ex.ToString();}return text;}

         加密后的数据转为base64编码并增加一个“G”开头。实际使用的格式是这样的:

  • 明文,json格式,总是以‘{’开头
  • 压缩,C+base64
  • AES,E+base64
  • 国密,G+base64

         所以不会有冲突。


(这里是文档结束)        

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com