C# OpenSSL.Net 實作測試
C# OpenSSL.Net 實作測試
資料來源:https://www.cnblogs.com/azeri/p/8972432.html
GITHUB: https://github.com/jash-git/CS_OpenSSLNet
最近在工作中遇到需要對數據傳輸進行加密解密,一開始是.Net與.Net環境間進行交互,使用.Net下的【System.Security.Cryptography】完全沒有問題,但後來要與Java,Android ,IOS進行交互,結果是怎麼都對不上,在查看後得知三者平台都使用的OpenSSL進行的加解密,於是就翻出了OpenSSL.Net
最簡易的 OpenSSL.Net 測試碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OpenSSL.Core; using OpenSSL.Crypto; //https://www.cnblogs.com/azeri/p/8972432.html //https://github.com/openssl-net/openssl-net namespace CS_OpenSSLNet { class Program { static void Pause() { Console.Write("Press any key to continue..."); Console.ReadKey(true); } public static string MD5(string text, Encoding encoding) { return HashDigest(text, encoding, MessageDigest.MD5); } public static string SHA1(string text, Encoding encoding) { return HashDigest(text, encoding, MessageDigest.SHA1); } private static string HashDigest(string text, Encoding encoding, MessageDigest hashType) { using (MessageDigestContext hashDigest = new MessageDigestContext(hashType)) { byte[] hashBytes = encoding.GetBytes(text); byte[] signByte = hashDigest.Digest(hashBytes); return BitConverter.ToString(signByte).Replace(" - ", "").ToLower(); } } static void Main(string[] args) { var ciphertext = MD5(" Md5加密。", Encoding.UTF8); Console.Write(ciphertext+"\n"); ciphertext = SHA1(" SHA1加密。", Encoding.UTF8); Console.Write(ciphertext+"\n"); Pause(); } } }