/// <summary> /// 使用 变形的 rc4 编码方法对字符串进行加密或者解密 /// </summary> /// <param name="source">原始字符串</param> /// <param name="key">密钥</param> /// <param name="operation">操作 加密还是解密</param> /// <param name="expiry">加密字串过期时间</param> /// <returns>加密或者解密后的字符串</returns> private static string DiscuzAuthcode(string source, string key, DiscuzAuthcodeMode operation, int expiry) { if (source == null || key == null) { return ""; } int ckey_length = 4; string keya, keyb, keyc, cryptkey, result; string timestamp = UnixTimestamp(); key = MD5(key); keya = MD5(CutString(key, 0, 16)); keyb = MD5(CutString(key, 16, 16)); keyc = ckey_length > 0 ? (operation == DiscuzAuthcodeMode.Decode ? CutString(source, 0, ckey_length) : RandomString(ckey_length)) : ""; cryptkey = keya + MD5(keya + keyc); if (operation == DiscuzAuthcodeMode.Decode) { byte[] temp; try { temp = System.Convert.FromBase64String(CutString(source, ckey_length)); } catch { try { temp = System.Convert.FromBase64String(CutString(source + "=", ckey_length)); } catch { try { temp = System.Convert.FromBase64String(CutString(source + "==", ckey_length)); } catch { return ""; } } } result = System.Text.Encoding.Default.GetString(RC4(temp, cryptkey)); if (CutString(result, 10, 16) == CutString(MD5(CutString(result, 26) + keyb), 0, 16)) { return CutString(result, 26); } else { return ""; } } else { source = "0000000000" + CutString(MD5(source + keyb), 0, 16) + source; byte[] temp = RC4(System.Text.Encoding.Default.GetBytes(source), cryptkey); return keyc + System.Convert.ToBase64String(temp); } }
/// <summary> /// 使用 变形的 rc4 编码方法对字符串进行加密或者解密 /// </summary> /// <param name="source">原始字符串</param> /// <param name="key">密钥</param> /// <param name="operation">操作 加密还是解密</param> /// <param name="expiry">密文有效期, 加密时候有效, 单 位 秒,0 为永久有效</param> /// <returns>加密或者解密后的字符串</returns> private static string DiscuzAuthcode(string source, string key, DiscuzAuthcodeMode operation, int expiry) { if (source == null || key == null) { return(""); } int ckey_length = 4; string keya, keyb, keyc, cryptkey, result; key = MD5(key); keya = MD5(CutString(key, 0, 16)); keyb = MD5(CutString(key, 16, 16)); keyc = ckey_length > 0 ? (operation == DiscuzAuthcodeMode.Decode ? CutString(source, 0, ckey_length) : RandomString(ckey_length)) : ""; cryptkey = keya + MD5(keya + keyc); if (operation == DiscuzAuthcodeMode.Decode) { byte[] temp; try { temp = System.Convert.FromBase64String(CutString(source, ckey_length)); } catch { try { temp = System.Convert.FromBase64String(CutString(source + "=", ckey_length)); } catch { try { temp = System.Convert.FromBase64String(CutString(source + "==", ckey_length)); } catch { return(""); } } } result = encoding.GetString(RC4(temp, cryptkey)); //throw new Exception(CutString(result, 0, 10)); long timestamp = long.Parse(CutString(result, 0, 10)); if ((timestamp == 0 || timestamp - UnixTimestamp() > 0) && CutString(result, 10, 16) == CutString(MD5(CutString(result, 26) + keyb), 0, 16)) { return(CutString(result, 26)); } else { return(""); } } else { source = (expiry == 0 ? "0000000000" : (expiry + UnixTimestamp()).ToString()) + CutString(MD5(source + keyb), 0, 16) + source; byte[] temp = RC4(encoding.GetBytes(source), cryptkey); return(keyc + System.Convert.ToBase64String(temp)); } }
/// <summary> /// 使用 变形的 rc4 编码方法对字符串进行加密或者解密 /// </summary> /// <param name="source">原始字符串</param> /// <param name="key">密钥</param> /// <param name="operation">操作 加密还是解密</param> /// <param name="expiry">加密字串过期时间</param> /// <returns>加密或者解密后的字符串</returns> private static string DiscuzAuthcode(string source, string key, DiscuzAuthcodeMode operation, int expiry) { source = System.Web.HttpUtility.UrlDecode(source); if (source == null || key == null) { return(""); } int ckey_length = 4; string keya, keyb, keyc, cryptkey, result; string timestamp = UnixTimestamp(); key = MD5.Encrypt(key); keya = MD5.Encrypt(CutString(key, 0, 16)); keyb = MD5.Encrypt(CutString(key, 16, 16)); keyc = ckey_length > 0 ? (operation == DiscuzAuthcodeMode.Decode ? CutString(source, 0, ckey_length) : RandomString(ckey_length)) : ""; cryptkey = keya + MD5.Encrypt(keya + keyc); if (operation == DiscuzAuthcodeMode.Decode) { byte[] temp; try { temp = System.Convert.FromBase64String(CutString(source, ckey_length)); } catch { try { temp = System.Convert.FromBase64String(CutString(source + "=", ckey_length)); } catch { try { temp = System.Convert.FromBase64String(CutString(source + "==", ckey_length)); } catch { return(""); } } } result = System.Text.Encoding.Default.GetString(RC4(temp, cryptkey)); if (CutString(result, 10, 16) == CutString(MD5.Encrypt(CutString(result, 26) + keyb), 0, 16)) { return(CutString(result, 26)); } else { return(""); } } else { source = "0000000000" + CutString(MD5.Encrypt(source + keyb), 0, 16) + source; byte[] temp = RC4(System.Text.Encoding.Default.GetBytes(source), cryptkey); return(keyc + System.Convert.ToBase64String(temp)); } }
/// <summary> /// 使用 变形的 rc4 编码方法对字符串进行加密或者解密 /// </summary> /// <param name="source">原始字符串</param> /// <param name="key">密钥</param> /// <param name="operation">操作 加密还是解密</param> /// <returns>加密或者解密后的字符串</returns> private static string DiscuzAuthcode(string source, string key, DiscuzAuthcodeMode operation) { if (source == null || key == null) { return ""; } const int ckeyLength = 7; key = key.Hash(new MD5CryptoServiceProvider()).ToLower(); string keya = CutString(key, 0, 16).Hash(new MD5CryptoServiceProvider()).ToLower(); string keyb = CutString(key, 16, 16).Hash(new MD5CryptoServiceProvider()).ToLower(); string keyc = (operation == DiscuzAuthcodeMode.Decode ? CutString(source, 0, ckeyLength) : StringGenerator.GetRandomizer(ckeyLength, true, true, true, true)); string cryptkey = keya + (keya + keyc).Hash(new MD5CryptoServiceProvider()).ToLower(); if (operation == DiscuzAuthcodeMode.Decode) { byte[] temp; try { temp = Convert.FromBase64String(CutString(source, ckeyLength)); } catch { try { temp = Convert.FromBase64String(CutString(source + "=", ckeyLength)); } catch { try { temp = Convert.FromBase64String(CutString(source + "==", ckeyLength)); } catch { return ""; } } } string result = Encoding.Default.GetString(RC4(temp, cryptkey)); return CutString(result, 10, 16) == CutString((CutString(result, 26) + keyb).Hash(new MD5CryptoServiceProvider()).ToLower(), 0, 16) ? CutString(result, 26) : ""; } else { source = "0000000000" + CutString((source + keyb).Hash(new MD5CryptoServiceProvider()).ToLower(), 0, 16) + source; var temp = RC4(Encoding.Default.GetBytes(source), cryptkey); return keyc + Convert.ToBase64String(temp); } }
private static string AuthCode(string source, string key, DiscuzAuthcodeMode operation, int expiry) { if (source == null || key == null) { return ""; } int ckey_length = 4; string keyb; string keyc; string cryptkey; string result; string timestamp = Utils.UnixTimestamp(); key = Utils.MD5(key); string keya = Utils.MD5(Utils.CutString(key, 0, 16)); keyb = Utils.MD5(Utils.CutString(key, 16, 16)); keyc = ckey_length > 0 ? (operation == DiscuzAuthcodeMode.Decode ? Utils.CutString(source, 0, ckey_length) : Utils.RandomString(ckey_length)) : ""; cryptkey = keya + Utils.MD5(keya + keyc); if (operation == DiscuzAuthcodeMode.Decode) { byte[] temp; try { temp = Convert.FromBase64String(Utils.CutString(source, ckey_length)); } catch { try { temp = Convert.FromBase64String(Utils.CutString(source + "=", ckey_length)); } catch { try { temp = Convert.FromBase64String(Utils.CutString(source + "==", ckey_length)); } catch { return ""; } } } result = Encoding.Default.GetString(RC4(temp, cryptkey)); if (Utils.CutString(result, 10, 16) == Utils.CutString(Utils.MD5(Utils.CutString(result, 26) + keyb), 0, 16)) { return Utils.CutString(result, 26); } else { return ""; } } else { source = "0000000000" + Utils.CutString(Utils.MD5(source + keyb), 0, 16) + source; byte[] temp = RC4(Encoding.Default.GetBytes(source), cryptkey); return keyc + Convert.ToBase64String(temp); } }
/// <summary> /// 使用 变形的 rc4 编码方法对字符串进行加密或者解密 /// </summary> /// <param name="source">原始字符串</param> /// <param name="key">密钥</param> /// <param name="operation">操作 加密还是解密</param> /// <param name="expiry">密文有效期, 加密时候有效, 单 位 秒,0 为永久有效</param> /// <returns>加密或者解密后的字符串</returns> private static string DiscuzAuthcode(string source, string key, DiscuzAuthcodeMode operation, int expiry) { if (source == null || key == null) { return ""; } int ckey_length = 4; string keya, keyb, keyc, cryptkey, result; key = MD5(key); keya = MD5(CutString(key, 0, 16)); keyb = MD5(CutString(key, 16, 16)); keyc = ckey_length > 0 ? (operation == DiscuzAuthcodeMode.Decode ? CutString(source, 0, ckey_length) : RandomString(ckey_length)) : ""; cryptkey = keya + MD5(keya + keyc); if (operation == DiscuzAuthcodeMode.Decode) { byte[] temp; try { temp = System.Convert.FromBase64String(CutString(source, ckey_length)); } catch { try { temp = System.Convert.FromBase64String(CutString(source + "=", ckey_length)); } catch { try { temp = System.Convert.FromBase64String(CutString(source + "==", ckey_length)); } catch { return ""; } } } result = encoding.GetString(RC4(temp, cryptkey)); //throw new Exception(CutString(result, 0, 10)); long timestamp = long.Parse(CutString(result, 0, 10)); if ((timestamp == 0 || timestamp - UnixTimestamp() > 0) && CutString(result, 10, 16) == CutString(MD5(CutString(result, 26) + keyb), 0, 16)) { return CutString(result, 26); } else { return ""; } } else { source = (expiry == 0 ? "0000000000" : (expiry + UnixTimestamp()).ToString()) + CutString(MD5(source + keyb), 0, 16) + source; byte[] temp = RC4(encoding.GetBytes(source), cryptkey); return keyc + System.Convert.ToBase64String(temp); } }