- Aes
publicstaticclassAesUtil{#region AES加密/// <summary>/// aes加码/// </summary>/// <param name="toEncrypt"></param>/// <returns></returns>/// publicstaticstringEncrypt(string toEncrypt){try{byte[] keyArray = UTF8Encoding.UTF8.GetBytes("testtesttesttest");byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);RijndaelManaged rDel =newRijndaelManaged();//using System.Security.Cryptography;
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;//using System.Security.Cryptography;
rDel.Padding = PaddingMode.PKCS7;//using System.Security.Cryptography; ICryptoTransform cTransform = rDel.CreateEncryptor();//using System.Security.Cryptography; byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray,0, toEncryptArray.Length);StringBuilder builder =newStringBuilder(resultArray.Length *3);foreach(byte data in resultArray)
builder.Append(Convert.ToString(data,16).PadLeft(2,'0').PadRight(3,' '));return builder.ToString().ToUpper().Replace(" ","");}catch(Exception ex){
Log.Error($"AesEncrypt ex:{ex.Message}");return"";}//return Convert.ToBase64String(resultArray, 0, resultArray.Length);}#endregion AES加密#region AES解密/// <summary>/// aes解码/// </summary>/// <param name="toDecrypt"></param>/// <returns></returns>publicstaticstringDecrypt(string toDecrypt){try{byte[] keyArray = UTF8Encoding.UTF8.GetBytes("testtesttesttest");
toDecrypt = toDecrypt.Replace(" ","");byte[] comBuffer =newbyte[toDecrypt.Length /2];for(int i =0; i < toDecrypt.Length; i +=2)
comBuffer[i /2]=(byte)Convert.ToByte(toDecrypt.Substring(i,2),16);RijndaelManaged rDel =newRijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;ICryptoTransform cTransform = rDel.CreateDecryptor();byte[] resultArray = cTransform.TransformFinalBlock(comBuffer,0, comBuffer.Length);return UTF8Encoding.UTF8.GetString(resultArray);}catch(Exception ex){
Log.Error($"AesDecrypt ex:{ex.Message}");return"";}}#endregion AES解密}
- SHA256
publicstaticclassSHA256EncryptUtil{/// SHA256加密,并十六进制编码/// </summary>/// <param name="data"></param>/// <returns></returns>publicstaticstringSHA256EncryptString(string data){byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);byte[] hash = SHA256Managed.Create().ComputeHash(bytes);StringBuilder builder =newStringBuilder();for(int i =0; i < hash.Length; i++){
builder.Append(hash[i].ToString("x2"));}return builder.ToString();}/// <summary>/// HMACSHA256加密/// </summary>/// <param name="message"></param>/// <param name="secret"></param>/// <returns></returns>publicstaticstringHmacSHA256(string message,string secret){
secret = secret ??"";var encoding =newSystem.Text.UTF8Encoding();byte[] keyByte = encoding.GetBytes(secret);byte[] messageBytes = encoding.GetBytes(message);using(var hmacsha256 =newHMACSHA256(keyByte)){byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);StringBuilder builder =newStringBuilder();for(int i =0; i < hashmessage.Length; i++){
builder.Append(hashmessage[i].ToString("x2"));}return builder.ToString();}}/// <summary>/// 二进制/// </summary>/// <param name="message"></param>/// <param name="secret"></param>/// <returns></returns>publicstaticbyte[]HmacSHA256Bytes(string message,string secret){
secret = secret ??"";var encoding =newSystem.Text.UTF8Encoding();byte[] keyByte = encoding.GetBytes(secret);byte[] messageBytes = encoding.GetBytes(message);using(var hmacsha256 =newHMACSHA256(keyByte)){byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);//StringBuilder builder = new StringBuilder();//for (int i = 0; i < hashmessage.Length; i++)//{// builder.Append(hashmessage[i].ToString("x2"));//}return hashmessage;}}}
测试密钥
公钥
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp3nEMet00r0M72wQW5wpg1Gl06R2iH/p6L9HO5qPw8y2uu5ZXYyP9rcuzMzQ0Gr5NfdJMEf6FYKqe+UnMvURctTxCH+Nu0yKSjKJwWTthvyDZ3uKVZsK5ix56zsJNJt/zgFhsuioLXrSQuqea/G867VYqYp7Q2RzT0KBmmIVVG8Bh5NBEs8fhnHFpMH7x3WVWipUT+ulYCebVX+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BgQDAyT1NkFu/rDdOegG5SYpCHBjvjeHKjDqPrmKSz+9pRDHp0AJgbJ4Yl6PNK7RpsDt0MFTSnTGmDTMNqlpcMInaE7qf4hMzrzRwoGvgzj/10l08mbjGnFLtEKVO53bTlTgjL3FPeDO83k3ceyTJLlfUGZtW8596GmPwn6RFRfBfswKBgQDTkMeU9EwpQWo/yq1/8AKyXJqwKoRjDuGjUZmUzW0ii44sHauLXYoUecu2ZoCAI1eBii7nLJgedT+0Y4D3naSf2r/MD+jcxkNEt351+u1csQahKHuopF4zpJZj9DwA/6868JH3RzQsrsMLQ3+Qr1usdz7kEg9uwwReT3MkWNzsiwKBgQC6DXysLnVdNcbqZbTkLKTRyGwIRNVztIGPyKw5I5PZ84Z4L9cbV7f749yhVVx34yx9pWc3G52es35h4dXywqVHYWLqijQd3SiuySNTgbkYdDynD3PTuWoOf9IJpDh5Wbkju9WO7GKCE8jwz6dgwOjiORRBhjySioWI44iVkjm5IQKBgFvbsJmh982qp9dsJuZTzTTcLjw/7wiB1a93Tw/HzPu9s4TjcVVdnJ0SFKHt/ZxToYbuPPHjqiWXi14+lYvp6OLVIsRXufUz965eiCEC3sF6risVhhMacy8KacxVwdIKcDaeSUn3/xEXQhEbghmrhj87RF4K1aA3D2GfK6aBG6V9
-----END PRIVATE KEY-----
本文转载自: https://blog.csdn.net/u012932582/article/details/130706229
版权归原作者 hbsyuyang 所有, 如有侵权,请联系我们删除。
版权归原作者 hbsyuyang 所有, 如有侵权,请联系我们删除。