| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Security.Cryptography;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using Org.BouncyCastle.Crypto;
- using Org.BouncyCastle.Crypto.Macs;
- using Org.BouncyCastle.Crypto.Parameters;
- using Org.BouncyCastle.Utilities.Encoders;
- namespace PTMedicalInsurance.Common
- {
- class Encrypt
- {
- public static string ToBase64hmac(string strText, string strKey)
- {
- HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(strKey));
- byte[] byteText = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(strText));
- return System.Convert.ToBase64String(byteText);
- }
- public static string HMACSHA1Text(string EncryptText, string EncryptKey)
- {
- //HMACSHA1加密
- string message;
- string key;
- message = EncryptText;
- key = EncryptKey;
- System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
- byte[] keyByte = encoding.GetBytes(key);
- HMACSHA1 hmacsha1 = new HMACSHA1(keyByte);
- byte[] messageBytes = encoding.GetBytes(message);
- byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
- //return ByteToString(hashmessage);
- return Convert.ToBase64String(hashmessage);
- }
- public static string HMACSHA1Text2(string EncryptText, string EncryptKey)
- {
- //HMACSHA1加密
- HMACSHA1 hmacsha1 = new HMACSHA1();
- hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(EncryptKey);
- byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(EncryptText);
- byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
- return Convert.ToBase64String(hashBytes);
- }
- public static string SHA256(string str)
- {
- //如果str有中文,不同Encoding的sha是不同的!!
- byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
- SHA256Managed Sha256 = new SHA256Managed();
- byte[] by = Sha256.ComputeHash(SHA256Data);
- //return BitConverter.ToString(by).Replace("-", "").ToLower(); //64
- return Convert.ToBase64String(by); //44
- }
- public static Byte[] SHA256Encrypt(string StrIn)
- {
- var sha256 = new SHA256Managed();
- var Asc = new ASCIIEncoding();
- var tmpByte = Asc.GetBytes(StrIn);
- //var tmpByte = Encoding.UTF8.GetBytes(StrIn);
- var EncryptBytes = sha256.ComputeHash(tmpByte);
- sha256.Clear();
- return EncryptBytes;
- }
- public static string GetMD5(string myString)
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] fromData = System.Text.Encoding.UTF8.GetBytes(myString);
- byte[] targetData = md5.ComputeHash(fromData);
- string byte2String = null;
- for (int i = 0; i < targetData.Length; i++)
- {
- byte2String += targetData[i].ToString("X2");
- }
- return byte2String;
- }
- }
- class HaErBinEncrypt
- {
- [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "Signature", CallingConvention = CallingConvention.StdCall)]
- private static extern int Signature(byte[] userid, byte[] sm2key, byte[] sm4key, byte[] plain, byte[] type, ref IntPtr pSignature, ref IntPtr pCipher);
- [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "EncryptBySM4", CallingConvention = CallingConvention.StdCall)]
- private static extern IntPtr EncryptBySM4(byte[] sm4key, byte[] plain);
- [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "DecryptBySM4_ECB", CallingConvention = CallingConvention.StdCall)]
- private static extern IntPtr DecryptBySM4_ECB(byte[] sm4key, byte[] pCipher);
- [DllImport("HaErBinEncrypt.dll", CharSet = CharSet.Ansi, EntryPoint = "EncryptBySM3HASH", CallingConvention = CallingConvention.StdCall)]
- private static extern IntPtr EncryptBySM3HASH(byte[] plain);
- const string sm2key = "AYvxpsjPGd06aErh6w8uMFKqXwpV6V0pXJYuP4KfD0s=";
- const string sm4key = "RNSNupzo1RAizTKU";
- public string Signature(string plain, ref string cipher)
- {
- byte[] bUID = Encoding.UTF8.GetBytes("");
- byte[] bSm2Key = Encoding.UTF8.GetBytes(sm2key);
- byte[] bSm4Key = Encoding.UTF8.GetBytes(sm4key);
- byte[] bPlain = Encoding.UTF8.GetBytes(plain);
- byte[] bType = Encoding.UTF8.GetBytes("asn1");
- byte[] btSignature = new byte[10240];
- try
- {
- IntPtr pSignatrue = IntPtr.Zero;
- IntPtr pCiper = IntPtr.Zero;
- int i = Signature(bUID, bSm2Key, bSm4Key, bPlain, bType, ref pSignatrue, ref pCiper);
- cipher = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pCiper);
- return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pSignatrue); ;
- }
- catch (Exception ex)
- {
- return "签名异常:" + ex.Message;
- }
- }
- public string Encrypt(string plain)
- {
- byte[] bSm4Key = Encoding.UTF8.GetBytes(sm4key);
- byte[] bPlain = Encoding.UTF8.GetBytes(plain);
- IntPtr pCipher = IntPtr.Zero;
- try
- {
- pCipher = EncryptBySM4(bSm4Key, bPlain);
- return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pCipher);
- }
- catch (Exception ex)
- {
- return "解密异常:" + ex.Message;
- }
- }
- public string Decrypt(string cipher)
- {
- byte[] bSm4Key = Encoding.UTF8.GetBytes(sm4key);
- byte[] bCipher = Encoding.UTF8.GetBytes(cipher);
- IntPtr pPlain = IntPtr.Zero;
- try
- {
- pPlain = DecryptBySM4_ECB(bSm4Key, bCipher);
- return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pPlain);
- }
- catch (Exception ex)
- {
- return "解密异常:" + ex.Message;
- }
- }
- public static string EncryptBySM3HASH(string plain)
- {
- byte[] bPlain = Encoding.UTF8.GetBytes(plain);
- IntPtr pCipher = IntPtr.Zero;
- try
- {
- pCipher = EncryptBySM3HASH(bPlain);
- return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pCipher);
- }
- catch (Exception ex)
- {
- return "解密异常:" + ex.Message;
- }
- }
- }
- #region SM3
- /// <summary>
- /// General
- /// </summary>
- public abstract class GeneralDigest : IDigest
- {
- /// <summary>
- /// 内部缓冲区的大小
- /// </summary>
- private const int ByteLength = 64;
- /// <summary>
- /// 消息摘要
- /// </summary>
- private readonly byte[] XBuf;
- /// <summary>
- /// 待更新的消息摘要的索引
- /// </summary>
- private int XBufOff;
- /// <summary>
- /// 待更新的消息摘要的大小
- /// </summary>
- private long ByteCount;
- /// <summary>
- /// 构造函数
- /// </summary>
- internal GeneralDigest()
- {
- XBuf = new byte[4];
- }
- /// <summary>
- /// 复制构造函数
- /// </summary>
- /// <param name="t"></param>
- internal GeneralDigest(GeneralDigest t)
- {
- XBuf = new byte[t.XBuf.Length];
- Array.Copy(t.XBuf, 0, XBuf, 0, t.XBuf.Length);
- XBufOff = t.XBufOff;
- ByteCount = t.ByteCount;
- }
- /// <summary>
- /// 用一个字节更新消息摘要。
- /// </summary>
- /// <param name="input"></param>
- public void Update(byte input)
- {
- XBuf[XBufOff++] = input;
- if (XBufOff == XBuf.Length)
- {
- ProcessWord(XBuf, 0);
- XBufOff = 0;
- }
- ByteCount++;
- }
- /// <summary>
- /// 用字节块更新消息摘要
- /// </summary>
- /// <param name="input"></param>
- /// <param name="inOff"></param>
- /// <param name="length"></param>
- public void BlockUpdate(byte[] input, int inOff, int length)
- {
- //更新当前消息摘要
- while ((XBufOff != 0) && (length > 0))
- {
- Update(input[inOff]);
- inOff++;
- length--;
- }
- //处理完整的消息摘要
- while (length > XBuf.Length)
- {
- ProcessWord(input, inOff);
- inOff += XBuf.Length;
- length -= XBuf.Length;
- ByteCount += XBuf.Length;
- }
- //填充剩余的消息摘要
- while (length > 0)
- {
- Update(input[inOff]);
- inOff++;
- length--;
- }
- }
- /// <summary>
- /// 产生最终的摘要值
- /// </summary>
- public void Finish()
- {
- long bitLength = (ByteCount << 3);
- //添加字节
- Update(unchecked((byte)128));
- while (XBufOff != 0) Update(unchecked((byte)0));
- ProcessLength(bitLength);
- ProcessBlock();
- }
- /// <summary>
- /// 重启
- /// </summary>
- public virtual void Reset()
- {
- ByteCount = 0;
- XBufOff = 0;
- Array.Clear(XBuf, 0, XBuf.Length);
- }
- /// <summary>
- /// 摘要应用其压缩功能的内部缓冲区的大小
- /// </summary>
- /// <returns></returns>
- public int GetByteLength()
- {
- return ByteLength;
- }
- /// <summary>
- /// 处理消息摘要
- /// ABCDEFGH 串联
- /// </summary>
- /// <param name="input"></param>
- /// <param name="inOff"></param>
- internal abstract void ProcessWord(byte[] input, int inOff);
- internal abstract void ProcessLength(long bitLength);
- /// <summary>
- /// 迭代压缩
- /// </summary>
- internal abstract void ProcessBlock();
- /// <summary>
- /// 算法名称
- /// </summary>
- public abstract string AlgorithmName { get; }
- /// <summary>
- /// 消息摘要生成的摘要的大小
- /// </summary>
- /// <returns></returns>
- public abstract int GetDigestSize();
- /// <summary>
- /// 关闭摘要,产生最终的摘要值。doFinal调用使摘要复位。
- /// </summary>
- /// <param name="output"></param>
- /// <param name="outOff"></param>
- /// <returns></returns>
- public abstract int DoFinal(byte[] output, int outOff);
- }
- /// <summary>
- /// 使用指定的数字执行无符号按位右移
- /// </summary>
- public class SupportClass
- {
- /// <summary>
- /// 使用指定的数字执行无符号按位右移
- /// </summary>
- /// <param name="number">要操作的编号</param>
- /// <param name="bits">要移位的比特数</param>
- /// <returns>移位操作产生的数字</returns>
- public static int URShift(int number, int bits)
- {
- if (number >= 0)
- return number >> bits;
- else
- return (number >> bits) + (2 << ~bits);
- }
- /// <summary>
- /// 使用指定的数字执行无符号按位右移
- /// </summary>
- /// <param name="number">要操作的编号</param>
- /// <param name="bits">要移位的比特数</param>
- /// <returns>移位操作产生的数字</returns>
- public static int URShift(int number, long bits)
- {
- return URShift(number, (int)bits);
- }
- /// <summary>
- /// 使用指定的数字执行无符号按位右移
- /// </summary>
- /// <param name="number">要操作的编号</param>
- /// <param name="bits">要移位的比特数</param>
- /// <returns>移位操作产生的数字</returns>
- public static long URShift(long number, int bits)
- {
- if (number >= 0)
- return number >> bits;
- else
- return (number >> bits) + (2L << ~bits);
- }
- /// <summary>
- /// 使用指定的数字执行无符号按位右移
- /// </summary>
- /// <param name="number">要操作的编号</param>
- /// <param name="bits">要移位的比特数</param>
- /// <returns>移位操作产生的数字</returns>
- public static long URShift(long number, long bits)
- {
- return URShift(number, (int)bits);
- }
- }
- /// <summary>
- ///
- /// ⊕ 等价于 ^
- /// ^ 等价于 &
- /// v 等价于 |
- /// </summary>
- public class SM3Digest : GeneralDigest
- {
- public override string AlgorithmName
- {
- get
- {
- return "SM3";
- }
- }
- /// <summary>
- /// 消息摘要生成的摘要的大小
- /// </summary>
- /// <returns></returns>
- public override int GetDigestSize()
- {
- return DigestLength;
- }
- /// <summary>
- /// SM3算法产生的哈希值大小
- /// </summary>
- private const int DigestLength = 32;
- /// <summary>
- /// 初始值IV
- /// </summary>
- private static readonly int[] IV = new int[] {
- 0x7380166f, 0x4914b2b9, 0x172442d7,
- unchecked((int)0xda8a0600), unchecked((int)0xa96f30bc), 0x163138aa,
- unchecked((int)0xe38dee4d), unchecked((int)0xb0fb0e4e)
- };
- /// <summary>
- /// 备份的字寄存器
- /// </summary>
- private readonly int[] v = new int[8];
- /// <summary>
- /// 使用中的字寄存器
- /// </summary>
- private readonly int[] v_ = new int[8];
- private static readonly int[] X0 = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- private readonly int[] X = new int[68];
- private int xOff;
- /// <summary>
- /// 0到15的Tj常量
- /// </summary>
- private readonly int TOne = 0x79cc4519;
- /// <summary>
- /// 16到63的Tj常量
- /// </summary>
- private readonly int TSecond = 0x7a879d8a;
- public SM3Digest()
- {
- Reset();
- }
- /// <summary>
- /// 复制构造函数
- /// </summary>
- /// <param name="t"></param>
- public SM3Digest(SM3Digest t) : base(t)
- {
- Array.Copy(t.X, 0, X, 0, t.X.Length);
- xOff = t.xOff;
- Array.Copy(t.v, 0, v, 0, t.v.Length);
- }
- /// <summary>
- /// 将复制的对象状态还原到该对象。
- /// 此方法的实现应尝试避免或最小化内存分配以执行重置。
- /// </summary>
- public override void Reset()
- {
- base.Reset();
- Array.Copy(IV, 0, v, 0, IV.Length);
- xOff = 0;
- Array.Copy(X0, 0, X, 0, X0.Length);
- }
- internal override void ProcessBlock()
- {
- int j;
- int[] ww = X;
- //64位比特串
- int[] ww_ = new int[64];
- #region 块消息扩展
- //消息扩展16 TO 67
- for (j = 16; j < 68; j++)
- {
- ww[j] = P1(ww[j - 16] ^ ww[j - 9] ^ (Rotate(ww[j - 3], 15))) ^ (Rotate(ww[j - 13], 7)) ^ ww[j - 6];
- }
- //消息扩展0 TO 63
- for (j = 0; j < 64; j++)
- {
- ww_[j] = ww[j] ^ ww[j + 4];
- }
- #endregion
- #region 压缩函数
- int[] vv = v;
- int[] vv_ = v_;//A,B,C,D,E,F,G,H为字寄存器
- Array.Copy(vv, 0, vv_, 0, IV.Length);
- //中间变量SS1,SS2,TT1,TT2
- int SS1, SS2, TT1, TT2;
- int aaa;
- //将消息分组B(i)划分为16个字
- for (j = 0; j < 16; j++)
- {
- aaa = Rotate(vv_[0], 12);
- SS1 = aaa + vv_[4] + Rotate(TOne, j);
- SS1 = Rotate(SS1, 7);
- SS2 = SS1 ^ aaa;
- TT1 = FFOne(vv_[0], vv_[1], vv_[2]) + vv_[3] + SS2 + ww_[j];
- TT2 = GGOne(vv_[4], vv_[5], vv_[6]) + vv_[7] + SS1 + ww[j];
- #region 更新各个寄存器
- vv_[3] = vv_[2];
- vv_[2] = Rotate(vv_[1], 9);
- vv_[1] = vv_[0];
- vv_[0] = TT1;
- vv_[7] = vv_[6];
- vv_[6] = Rotate(vv_[5], 19);
- vv_[5] = vv_[4];
- vv_[4] = P0(TT2);
- #endregion
- }
- for (j = 16; j < 64; j++)
- {
- aaa = Rotate(vv_[0], 12);
- SS1 = aaa + vv_[4] + Rotate(TSecond, j);
- SS1 = Rotate(SS1, 7);
- SS2 = SS1 ^ aaa;
- TT1 = FFSecond(vv_[0], vv_[1], vv_[2]) + vv_[3] + SS2 + ww_[j];
- TT2 = GGSecond(vv_[4], vv_[5], vv_[6]) + vv_[7] + SS1 + ww[j];
- #region 更新各个寄存器
- vv_[3] = vv_[2];
- vv_[2] = Rotate(vv_[1], 9);
- vv_[1] = vv_[0];
- vv_[0] = TT1;
- vv_[7] = vv_[6];
- vv_[6] = Rotate(vv_[5], 19);
- vv_[5] = vv_[4];
- vv_[4] = P0(TT2);
- #endregion
- }
- #endregion
- //256比特的杂凑值y =vv_(j+1) ABCDEFGH
- for (j = 0; j < 8; j++)
- {
- vv[j] ^= vv_[j];
- }
- // Reset
- xOff = 0;
- Array.Copy(X0, 0, X, 0, X0.Length);
- }
- internal override void ProcessWord(byte[] in_Renamed, int inOff)
- {
- int n = in_Renamed[inOff] << 24;
- n |= (in_Renamed[++inOff] & 0xff) << 16;
- n |= (in_Renamed[++inOff] & 0xff) << 8;
- n |= (in_Renamed[++inOff] & 0xff);
- X[xOff] = n;
- if (++xOff == 16)
- {
- ProcessBlock();
- }
- }
- internal override void ProcessLength(long bitLength)
- {
- if (xOff > 14)
- {
- ProcessBlock();
- }
- X[14] = (int)(SupportClass.URShift(bitLength, 32));
- X[15] = (int)(bitLength & unchecked((int)0xffffffff));
- }
- /// <summary>
- /// 写入到大端
- /// </summary>
- /// <param name="n"></param>
- /// <param name="bs"></param>
- /// <param name="off"></param>
- public static void IntToBigEndian(int n, byte[] bs, int off)
- {
- bs[off] = (byte)(SupportClass.URShift(n, 24));
- bs[++off] = (byte)(SupportClass.URShift(n, 16));
- bs[++off] = (byte)(SupportClass.URShift(n, 8));
- bs[++off] = (byte)(n);
- }
- /// <summary>
- /// 关闭摘要,产生最终的摘要值。doFinal调用使摘要复位。
- /// </summary>
- /// <param name="out_Renamed"></param>
- /// <param name="outOff"></param>
- /// <returns></returns>
- public override int DoFinal(byte[] out_Renamed, int outOff)
- {
- Finish();
- for (int i = 0; i < 8; i++)
- {
- IntToBigEndian(v[i], out_Renamed, outOff + i * 4);
- }
- Reset();
- return DigestLength;
- }
- /// <summary>
- /// x循环左移n比特运算
- /// </summary>
- /// <param name="x"></param>
- /// <param name="n"></param>
- /// <returns></returns>
- private static int Rotate(int x, int n)
- {
- return (x << n) | (SupportClass.URShift(x, (32 - n)));
- }
- #region 置换函数
- /// <summary>
- /// 置换函数P0
- /// </summary>
- /// <param name="x"></param>
- /// <returns></returns>
- private static int P0(int x)
- {
- return (x) ^ Rotate(x, 9) ^ Rotate(x, 17);
- }
- /// <summary>
- /// 置换函数P1
- /// </summary>
- /// <param name="x"></param>
- /// <returns></returns>
- private static int P1(int x)
- {
- return (x) ^ Rotate(x, 15) ^ Rotate(x, 23);
- }
- #endregion
- #region 布尔函数
- /// <summary>
- /// 0到15的布尔函数FF (X⊕^Y⊕Z)
- /// </summary>
- /// <param name="X"></param>
- /// <param name="Y"></param>
- /// <param name="Z"></param>
- /// <returns></returns>
- private static int FFOne(int X, int Y, int Z)
- {
- return (X ^ Y ^ Z);
- }
- /// <summary>
- /// 16到63的布尔函数FF
- /// </summary>
- /// <param name="X"></param>
- /// <param name="Y"></param>
- /// <param name="Z"></param>
- /// <returns></returns>
- private static int FFSecond(int X, int Y, int Z)
- {
- return ((X & Y) | (X & Z) | (Y & Z));
- }
- /// <summary>
- /// 0到15的布尔函数GG
- /// </summary>
- /// <param name="X"></param>
- /// <param name="Y"></param>
- /// <param name="Z"></param>
- /// <returns></returns>
- private static int GGOne(int X, int Y, int Z)
- {
- return (X ^ Y ^ Z);
- }
- /// <summary>
- /// 16到63的布尔函数GG
- /// </summary>
- /// <param name="X"></param>
- /// <param name="Y"></param>
- /// <param name="Z"></param>
- /// <returns></returns>
- private static int GGSecond(int X, int Y, int Z)
- {
- return ((X & Y) | (~X & Z));
- }
- #endregion
- }
- /// <summary>
- /// Sm3算法(10进制的ASCII)
- /// 在SHA-256基础上改进实现的一种算法
- /// 对标国际MD5算法和SHA算法
- /// </summary>
- public static class Sm3Crypto
- {
- /// <summary>
- /// sm3加密(使用自定义密钥)
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static byte[] ToSM3byte(string data, string key)
- {
- byte[] msg1 = Encoding.Default.GetBytes(data);
- byte[] key1 = Encoding.Default.GetBytes(key);
- KeyParameter keyParameter = new KeyParameter(key1);
- SM3Digest sm3 = new SM3Digest();
- HMac mac = new HMac(sm3);//带密钥的杂凑算法
- mac.Init(keyParameter);
- mac.BlockUpdate(msg1, 0, msg1.Length);
- byte[] result = new byte[mac.GetMacSize()];
- mac.DoFinal(result, 0);
- return Hex.Encode(result);
- }
- /// <summary>
- /// sm3加密
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static byte[] ToSM3byte(this string data)
- {
- var msg = ToHexByte(data);//把字符串转成16进制的ASCII码
- SM3Digest sm3 = new SM3Digest();
- sm3.BlockUpdate(msg, 0, msg.Length);
- byte[] md = new byte[sm3.GetDigestSize()];//SM3算法产生的哈希值大小
- sm3.DoFinal(md, 0);
- return Hex.Encode(md);
- }
- /// <summary>
- /// 字符串转16进制字节数组
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static byte[] ToHexByte(string data)
- {
- byte[] msg1 = Encoding.Default.GetBytes(data);
- string hexString = BytesToHexString(msg1);
- byte[] returnBytes = new byte[hexString.Length / 2];
- for (int i = 0; i < returnBytes.Length; i++)
- returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 10);
- return returnBytes;
- }
- /// <summary>
- /// byte[]数组转16进制字符串
- /// </summary>
- /// <param name="input">byte[]数组</param>
- /// <returns>16进制字符串</returns>
- public static string BytesToHexString(byte[] input)
- {
- StringBuilder hexString = new StringBuilder(64);
- for (int i = 0; i < input.Length; i++)
- {
- hexString.Append(String.Format("{0:X2}", input[i]));
- }
- return hexString.ToString();
- }
- }
- #endregion
- #region JAVASHA256
- public static class JAVASHA256
- {
- /*** 获取随机数 */
- public static String getRandomString(int length)
- {
- String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- Random random = new Random();
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < length; ++i)
- {
- int number = random.Next(62);
- sb.Append(str.Substring(number, 1));
- }
- return sb.ToString();
- }
- /*** 获取当前时间戳 */
- //private static long getCurrentUnixSeconds()
- //{
- // ZoneOffset zoneOffset = ZoneOffset.ofHours(8);
- // LocalDateTime localDateTime = LocalDateTime.now();
- // return localDateTime.toEpochSecond(zoneOffset);
- //}
- public static String byte2Hex(byte[] bytes)
- {
- StringBuilder stringBuffer = new StringBuilder();
- String temp = null;
- for (int i = 0; i < bytes.Length; ++i)
- {
- temp = bytes[i].ToString("X2");
- if (temp.Length == 1)
- {
- stringBuffer.Append("0");
- }
- stringBuffer.Append(temp);
- }
- return stringBuffer.ToString();
- }
- }
- #endregion
- }
|