In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
In .NET projects, we use encryption more often. Because in modern projects, the demand for information security is getting higher and higher, so the encryption of so much information becomes very important. Several commonly used encryption / decryption algorithms are now available.
1. For the conversion between text and Base64-encoded text and between Byte [] and Base64-encoded text:
(1)。 Convert plain text to Base64-encoded text
/ convert plain text to Base64-encoded text / public static string StringToBase64String (string value) {if (string.IsNullOrEmpty (value)) {throw new ArgumentNullException (value) } try {var binBuffer = (new UnicodeEncoding ()) .GetBytes (value); var base64ArraySize = (int) Math.Ceiling (binBuffer.Length / 3D) * 4; var charBuffer = new char [base64ArraySize]; Convert.ToBase64CharArray (binBuffer, 0, binBuffer.Length, charBuffer, 0); var s = new string (charBuffer) Return s;} catch (Exception ex) {throw new Exception (ex.Message);}}
(2)。 Convert Base64-encoded text to plain text
/ / convert Base64-encoded text to plain text / Base64-encoded text / public static string Base64StringToString (string base64) {if (string.IsNullOrEmpty (base64)) {throw new ArgumentNullException (base64) } try {var charBuffer = base64.ToCharArray (); var bytes = Convert.FromBase64CharArray (charBuffer, 0, charBuffer.Length); return (new UnicodeEncoding ()) .GetString (bytes);} catch (Exception ex) {throw new Exception (ex.Message) }}
(3)。 Convert Byte [] to Base64-encoded text
/ / convert Byte [] to Base64-encoded text / Byte [] / public string ToBase64 (byte [] binBuffer) {if (binBuffer = = null) {throw new ArgumentNullException ("binBuffer") } try {var base64ArraySize = (int) Math.Ceiling (binBuffer.Length / 3D) * 4; var charBuffer = new char [base64ArraySize]; Convert.ToBase64CharArray (binBuffer, 0, binBuffer.Length, charBuffer, 0); var s = new string (charBuffer); return s } catch (Exception ex) {throw new Exception (ex.Message);}}
(4)。 Convert Base64-encoded text to Byte []
/ / convert Base64 encoded text to Byte [] / Base64 encoded text / public byte [] Base64ToBytes (string base64) {if (string.IsNullOrEmpty (base64)) {throw new ArgumentNullException (base64) } try {var charBuffer = base64.ToCharArray (); var bytes = Convert.FromBase64CharArray (charBuffer, 0, charBuffer.Length); return bytes;} catch (Exception ex) {throw new Exception (ex.Message);}}
2. DES encryption / decryption class.
(1)。 Encrypt
Private static readonly string KEY = "pengze0902"; / encryption / public static string Encrypt (string Text) {return Encrypt (Text, KEY) } / encrypt data / public static string Encrypt (string Text, string sKey) {var des = new DESCryptoServiceProvider (); var inputByteArray = Encoding.Default.GetBytes (Text) Var bKey = Encoding.ASCII.GetBytes (Md5Hash (sKey) .Substring (0,8)); des.Key = bKey; des.IV = bKey; var ms = new System.IO.MemoryStream (); var cs = new CryptoStream (ms, des.CreateEncryptor (), CryptoStreamMode.Write); cs.Write (inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock () Var ret = new StringBuilder (); foreach (byte b in ms.ToArray ()) {ret.AppendFormat ("{0:X2}", b);} return ret.ToString ();}
(2)。 Decryption
Private static readonly string KEY = "pengze0902"; / decrypt / public static string Decrypt (string text) {return Decrypt (text, KEY) } / decrypt data / public static string Decrypt (string text, string sKey) {var des = new DESCryptoServiceProvider (); var len = text.Length / 2; byte [] inputByteArray = new byte [len] Int x; for (x = 0; x
< len; x++) { var i = Convert.ToInt32(text.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } var bKey = Encoding.ASCII.GetBytes(Md5Hash(sKey).Substring(0, 8)); des.Key = bKey; des.IV = bKey; System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } (3).取得MD5加密串新航道培训 //// /// 取得MD5加密串 /// /// 源明文字符串 /// 密文字符串 public static string Md5Hash(string input) { var md5 = new MD5CryptoServiceProvider(); var bs = Encoding.UTF8.GetBytes(input); bs = md5.ComputeHash(bs); var s = new StringBuilder(); foreach (var b in bs) { s.Append(b.ToString("x2").ToUpper()); } var password = s.ToString(); return password; } 3.MD5加密 (1). 32位大写 /// /// 32位大写 /// /// public static string Upper32(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5"); if (hashPasswordForStoringInConfigFile != null) s = hashPasswordForStoringInConfigFile.ToString(); return s.ToUpper(); } (2). 32位小写 /// /// 32位小写 /// /// public static string Lower32(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5"); if (hashPasswordForStoringInConfigFile != null) s = hashPasswordForStoringInConfigFile.ToString(); return s.ToLower(); } (3).16位大写 /// /// 16位大写 /// /// public static string Upper16(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5"); if (hashPasswordForStoringInConfigFile != null) s = hashPasswordForStoringInConfigFile.ToString(); return s.ToUpper().Substring(8, 16); } (4).16位小写 /// /// 16位小写 /// /// public static string Lower16(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5"); if (hashPasswordForStoringInConfigFile != null) s = hashPasswordForStoringInConfigFile.ToString(); return s.ToLower().Substring(8, 16); } 4.Sha1签名算法 /// /// 签名算法 /// /// /// public static string GetSha1(string str) { if (string.IsNullOrEmpty(str)) { throw new ArgumentNullException(str); } try { //建立SHA1对象 SHA1 sha = new SHA1CryptoServiceProvider(); //将mystr转换成byte[] var enc = new ASCIIEncoding(); var dataToHash = enc.GetBytes(str); //Hash运算 var dataHashed = sha.ComputeHash(dataToHash); //将运算结果转换成string var hash = BitConverter.ToString(dataHashed).Replace("-", ""); return hash; } catch (Exception ex) { throw new Exception(ex.Message); } } 5.Sha256加密算法 /// /// 将字符串转换为sha256散列 /// /// 字符串进行转换 /// sha256散列或null public static string ToSha256(this string data) { try { if (string.IsNullOrEmpty(data)) return null; var hashValue = new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(data)); var hex = hashValue.Aggregate("", (current, x) =>Current + String.Format ("{0:x2}", x); if (string.IsNullOrEmpty (hex)) throw new Exception ("Erro creating SHA256 hash"); return hex;} catch (Exception e) {throw new Exception (e.Message, e);}}
These are some of the more commonly used algorithm code.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.