您现在的位置是:首页 > cms教程 > Discuz教程Discuz教程
C#版本的discuz authcode函数代码
语兰2025-07-03Discuz教程已有人查阅
导读根据网上流传甚广的一个版本修改,修正了设置加密串过期时间expiry没有效果的问题。usingSystem;using System.Collections.Generic;
根据网上流传甚广的一个版本修改,修正了设置加密串过期时间expiry没有效果的问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
namespace API.Common
{
public enum DiscuzAuthcodeMode { Encode, Decode };
/// <summary>
/// C#版本 discuz authcode函数。根据网上版本修正设置expiry无效问题。by pwg17
/// </summary>
public class Authcode
{
private static Encoding encoding = Encoding.GetEncoding("gbk");
public Authcode()
{
}
/// <summary>
/// 从字符串的指定位置截取指定长度的子字符串
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="startIndex">子字符串的起始位置</param>
/// <param name="length">子字符串的长度</param>
/// <returns>子字符串</returns>
private static string CutString(string str, int startIndex, int length)
{
if (startIndex >= 0)
{
if (length < 0)
{
length = length * -1;
if (startIndex - length < 0)
{
length = startIndex;
startIndex = 0;
}
else
{
startIndex = startIndex - length;
}
}
if (startIndex > str.Length)
{
return "";
}
}
else
{
if (length < 0)
{
return "";
}
else
{
if (length + startIndex > 0)
{
length = length + startIndex;
startIndex = 0;
}
else
{
return "";
}
}
}
if (str.Length - startIndex < length)
{
length = str.Length - startIndex;
}
return str.Substring(startIndex, length);
}
/// <summary>
/// 从字符串的指定位置开始截取到字符串结尾的了符串
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="startIndex">子字符串的起始位置</param>
/// <returns>子字符串</returns>
private static string CutString(string str, int startIndex)
{
return CutString(str, startIndex, str.Length);
}
/// <summary>
/// MD5函数
/// </summary>
/// <param name="str">原始字符串</param>
/// <returns>MD5结果</returns>
public static string MD5(string str)
{
byte[] b = encoding.GetBytes(str);
b = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
string ret = "";
for (int i = 0; i < b.Length; i++)
{
ret += b.ToString("x").PadLeft(2, '0');
}
return ret;
}
/// <summary>
/// 用于 RC4 处理密码
/// </summary>
/// <param name="pass">密码字串</param>
/// <param name="kLen">密钥长度,一般为 256</param>
/// <returns></returns>
private static Byte[] GetKey(Byte[] pass, Int32 kLen)
{
Byte[] mBox = new Byte[kLen];
for (Int64 i = 0; i < kLen; i++)
{
mBox = (Byte)i;
}
Int64 j = 0;
for (Int64 i = 0; i < kLen; i++)
{
j = (j + mBox + pass[i % pass.Length]) % kLen;
Byte temp = mBox;
mBox = mBox[j];
mBox[j] = temp;
}
return mBox;
}
/// <summary>
/// 生成随机字符
/// </summary>
/// <param name="lens">随机字符长度</param>
/// <returns>随机字符</returns>
private static string RandomString(int lens)
{
char[] CharArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int clens = CharArray.Length;
string sCode = "";
Random random = new Random();
for (int i = 0; i < lens; i++)
{
sCode += CharArray[random.Next(clens)];
}
return sCode;
}
/// <summary>
/// 使用 authcode 方法对字符串加密
/// </summary>
/// <param name="source">原始字符串</param>
/// <param name="key">密钥</param>
/// <param name="expiry">加密字串有效时间,单位是秒</param>
/// <returns>加密结果</returns>
public static string DiscuzAuthcodeEncode(string source, string key, int expiry)
{
return DiscuzAuthcode(source, key, DiscuzAuthcodeMode.Encode, expiry);
}
/// <summary>
/// 使用 Discuz authcode 方法对字符串加密
/// </summary>
/// <param name="source">原始字符串</param>
/// <param name="key">密钥</param>
/// <returns>加密结果</returns>
public static string DiscuzAuthcodeEncode(string source, string key)
{
return DiscuzAuthcode(source, key, DiscuzAuthcodeMode.Encode, 0);
}
/// <summary>
/// 使用 Discuz authcode 方法对字符串解密
/// </summary>
/// <param name="source">原始字符串</param>
/// <param name="key">密钥</param>
/// <returns>解密结果</returns>
public static string DiscuzAuthcodeDecode(string source, string key)
{
return DiscuzAuthcode(source, key, DiscuzAuthcodeMode.Decode, 0);
}
/// <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));
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="input">原始字串数组</param>
/// <param name="pass">密钥</param>
/// <returns>处理后的字串数组</returns>
private static Byte[] RC4(Byte[] input, String pass)
{
if (input == null || pass == null) return null;
byte[] output = new Byte[input.Length];
byte[] mBox = GetKey(encoding.GetBytes(pass), 256);
// 加密
Int64 i = 0;
Int64 j = 0;
for (Int64 offset = 0; offset < input.Length; offset++)
{
i = (i + 1) % mBox.Length;
j = (j + mBox) % mBox.Length;
Byte temp = mBox;
mBox = mBox[j];
mBox[j] = temp;
Byte a = input[offset];
//Byte b = mBox[(mBox + mBox[j] % mBox.Length) % mBox.Length];
// mBox[j] 一定比 mBox.Length 小,不需要在取模
Byte b = mBox[(mBox + mBox[j]) % mBox.Length];
output[offset] = (Byte)((Int32)a ^ (Int32)b);
}
return output;
}
private static string AscArr2Str(byte[] b)
{
return System.Text.UnicodeEncoding.Unicode.GetString(
System.Text.ASCIIEncoding.Convert(System.Text.Encoding.ASCII,
System.Text.Encoding.Unicode, b)
);
}
public static long UnixTimestamp()
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
TimeSpan toNow = dtNow.Subtract(dtStart);
string timeStamp = toNow.Ticks.ToString();
return long.Parse(timeStamp.Substring(0, timeStamp.Length - 7));
}
public static string urlencode(string str)
{
//php的urlencode不同于HttpUtility.UrlEncode
//return HttpUtility.UrlEncode(str);
string tmp = string.Empty;
string strSpecial = "_-.1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < str.Length; i++)
{
string crt = str.Substring(i, 1);
if (strSpecial.Contains(crt))
tmp += crt;
else
{
byte[] bts = encoding.GetBytes(crt);
foreach (byte bt in bts)
{
tmp += "%" + bt.ToString("X");
}
}
}
return tmp;
}
public static long time()
{
TimeSpan ts = new TimeSpan(System.DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks);
return (long)ts.TotalMilliseconds;
}
}
}
本文标签:
很赞哦! ()
相关教程
- PHP中的array_intersect_ukey():用自定义回调函数比较键名的数组交集(只比较键)
- PHP中的array_intersect_uassoc():用自定义回调函数比较键名的数组交集
- PHP中的array_filter():用回调函数过滤数组元素
- PHP中的array_diff_ukey():用自定义回调函数比较键名的数组差集(只比键)
- PHP中的array_diff_uassoc():用自定义回调函数比较键名的数组差集
- PHP中的asort()函数:按值升序排序并保持键值关联
- PHP中的arsort()函数:保持键值关联的逆向排序
- PHP中五种字母三角形图案的详细拆解(使用range()函数)
- PHP中闰年判定的两种方法:日期函数与条件运算
- PHP中字符串反转的两种实现方式:内置函数与手动循环
- PHP中数字反转的两种方法:数学算法与字符串函数
- PHP发送邮件:mail()函数
图文教程
Discuz百度jiathis代码分享
templates/default/viewthread_node.htm中找到$post[message],这个显示的是贴子内容,在这个div里你可以添加想显示的东西
discuz3.x ssrf漏洞分析
漏洞促发点\souce\module\forum\forum_ajax.php之后看到了这里看名字看出应该是个远程下载图片的功能。preg_match_all会匹配完整的字符串把他输出到array[0]中然后array[1]中存放
Discuz!NT网站安装自动化论坛程序安装及初始化过程
网站安装自动化--论坛程序安装及初始化过程论坛的安装文件位于Discuz.Web中的Instal文件夹中。该文件夹中的文件index.aspx为安装的起始页。
Windows部署配置php+mysql搭建Discuz
Discuz! X 是一款以 PHP 为编程语言,以 MySQL 为数据库,并使用 Apache/IIS/Nginx(任意一种即可) 提供 web 服务的产品。要搭建 Discuz! X 站点,服务器必须安装由 PHP、MySQL、Apa
相关源码
-
(自适应响应式)高端珠宝首饰奢侈品pbootcms模板下载本模板为珠宝首饰及奢侈品行业打造,采用PbootCMS内核开发,具备卓越的视觉表现力与商业转化能力。自适应设计确保在手机端呈现产品细节,后台数据实时同步,助您高效展示钻石查看源码 -
(自适应响应式)瓷砖大理石装修建材类网站pbootcms模板html5模板本模板基于PbootCMS开发,针对瓷砖、大理石等建材行业特点进行优化设计。采用响应式布局技术,确保产品展示效果在不同设备上都能合理呈现。模板特别强化了石材纹理的视觉表现力,帮助建材企业更好地展示产品质感。查看源码 -
pbootcms模板PC+WAP娱乐新闻资讯类博客网站源码该模板基于PbootCMS内核开发,专为娱乐新闻、健康生活类资讯网站设计,同时支持快速适配其他行业(如企业官网、博客门户等),仅需替换图文内容即可完成转型。查看源码 -
自适应车行汽车租赁二手车行业企业网站模板为汽车租赁与二手车交易场景深度优化,采用PbootCMS内核开发,聚焦车辆展示、租赁流程与服务介绍三大核心模块。响应式布局确保PC与移动端数据实时同步,后台一键管理车辆信息查看源码 -
(自适应)蓝色环保科技设备带三级栏目网站模板下载该模板为环保科技企业设计,提供专业的产品展示与技术服务平台。采用响应式布局,适配环保设备、清洁技术等应用场景,通过可视化后台可快速搭建符合行业特性的展示网站。查看源码 -
(自适应)变压器电子元器件电器配件pbootcms网站模板源码为电子元器件企业打造的响应式网站模板,基于PbootCMS内核开发,助力企业快速构建专业级线上展示平台。支持页面独立设置标题、关键词和描述,内置SEO友好结构。PHP程序确保运行安全稳定,有助于提升搜索引擎收录效果。查看源码
| 分享笔记 (共有 篇笔记) |
