/// <summary> /// 多进制除法 /// </summary> /// <param name="format">格式</param> /// <param name="str1">第一个数</param> /// <param name="str2">第二个数</param> /// <returns>商</returns> static public string division(int format, string str1, string str2) { int num1 = CryptoString.stringToInt(anyToDec(str1, format)); int num2 = CryptoString.stringToInt(anyToDec(str2, format)); return(decToAny((num1 / num2).ToString(), format)); }
/// <summary> /// 字符串转&#十进制格式的Unicode编码(如:xss) /// </summary> /// <param name="str">字符串</param> /// <returns>&#十进制格式的Unicode编码</returns> public static string StringToHTMLUnicode(string str) { string unicode = StringToUnicode(str); string ux10 = ""; for (int i = 2; i < unicode.Length; i = i + 6) { ux10 += "\\u" + Conversion.convert(unicode.Substring(i, 4), 16, 10); } return(CryptoString.regReplace(ux10, "&#", @"\\u0*")); }
/// <summary> /// Sha256加密 /// </summary> /// <param name="str">要加密的字符串</param> /// <returns></returns> static public string sha256(string str) { byte[] bytes = CryptoString.stringToBytes(str); System.Security.Cryptography.SHA256 sha256 = new SHA256CryptoServiceProvider(); byte[] retval = sha256.ComputeHash(bytes); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retval.Length; i++) { sb.Append(retval[i].ToString("x2")); } return(sb.ToString()); }
/// <summary> /// 16进制ASCII串转字符串 /// </summary> /// <param name="hex">16进制ASCII串(每个ASCII必须占2位,不需要0x)</param> /// <returns>字符串</returns> public static string ASCIIToString(string hex) { if (hex.StartsWith("0x") || hex.StartsWith("0X")) { hex = hex.Remove(0, 2); } int len = hex.Length; if (len % 2 != 0) { throw new Exception("字符串长度错误"); } string str = ""; for (int i = 0; i < len; i++) { str += ASCIIToString(new byte[] { //按byte转ASCII (byte)CryptoString.stringToInt( //将10进制string数字字符串转对应int Conversion.convert( //16进制转10进制 new string(new char[] { hex[i], hex[++i] }), 16, 10)) }); } return(str); }
/// <summary> /// 生成随机盐值 /// </summary> /// <param name="n">盐的长度</param> /// <param name="_chars">自定义随机的类型(默认数字和大小写字母)</param> /// <returns>盐值</returns> static public string randomSalt(int n, string _chars = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM") { return(CryptoString.randomString(n, _chars)); }