paip.提升安全性-----时间判断
2012-08-18 00:58
260 查看
paip.提升安全性-----时间判断
常常有这样的情景,我们需要在编码中需要根据时间来执行某段代码,但是这段时间判断代码又不希望被别人看懂..特别是不能出现日期字串,以
防止别人猜明意思
伪码如下:
if( nowtime>"2013.1.1" )
{...........//some code
}
一个比较好的办法是使用magic number,并且不要使用相关的时间函数..如在C#,不要使用DATETIME类...
可以使用WIN32API来取时间,并且转换为LONG型数字来进行判断..
如:
long t= x.getl();
if (t > 63484678861000)
{
//todo
}
这样就避免了代码段被人轻易读懂..这里的MAGIC NUMBER 63484678861000表示一个日期" 2013.1.1"..
getl函数主要如下,这里为了方便阅读,没有混淆加密。。如果在实际应用中,应该对此类方法进行混淆加密
------------------------- 相关代码--------------------------------------------
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)] //这个如果不加,则返回值全部为0
public class MySystemTime
{
public ushort wYear; //必须设为ushort,如果为INT ,则数据错乱..WIN32API里是INTERGE
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
public class Win32API
{
// [DllImport("User32.dll")]
[DllImport("kernel32.dll")]
public static extern void GetSystemTime(MySystemTime st);
public static long getl()
{
// 是“协同世界时间”(即UTC,也叫做GMT)格式,需要+8个时区
MySystemTime mt = new MySystemTime();
Win32API.GetSystemTime(mt);
int days = mt.wYear * 365 + mt.wMonth * 30 + mt.wDay;
long millsecs = (long)days * 24 * 3600 * 1000 + (long)getHour(mt.wHour) * 3600 * 1000 + (long)mt.wMinute * 60 * 1000
+ mt.wSecond * 1000 + mt.wMilliseconds;
//string s = mt.wYear + pad0left(mt.wMonth.ToString(), 2) + pad0left(mt.wDay.ToString(), 2);
//s += getHour(mt.wHour) + pad0left(mt.wMinute.ToString(), 2) + pad0left(mt.wSecond.ToString(), 2);
//s += pad0left(mt.wMilliseconds.ToString(), 3);
return millsecs;
}
private static int getHour(ushort hour)
{
ushort t = (ushort)(hour + ushort.Parse("8"));
if (t == 24)
t = 0;
return t;
}
private static string pad0left(string ms, int len)
{
string strOrdid = ms;
for (int i = 0; i < len - ms.Length; i++)
{
strOrdid = "0" + strOrdid;
}
return strOrdid;
}
public static long getMagicNum()
{
int days = 2013 * 365 + 1 * 30 + 1;
long millsecs = (long)days * 24 * 3600 * 1000 + (long)getHour(1) * 3600 * 1000 + (long)1 * 60 * 1000 +1 * 1000 + 0;
return millsecs;
}
}
常常有这样的情景,我们需要在编码中需要根据时间来执行某段代码,但是这段时间判断代码又不希望被别人看懂..特别是不能出现日期字串,以
防止别人猜明意思
伪码如下:
if( nowtime>"2013.1.1" )
{...........//some code
}
一个比较好的办法是使用magic number,并且不要使用相关的时间函数..如在C#,不要使用DATETIME类...
可以使用WIN32API来取时间,并且转换为LONG型数字来进行判断..
如:
long t= x.getl();
if (t > 63484678861000)
{
//todo
}
这样就避免了代码段被人轻易读懂..这里的MAGIC NUMBER 63484678861000表示一个日期" 2013.1.1"..
getl函数主要如下,这里为了方便阅读,没有混淆加密。。如果在实际应用中,应该对此类方法进行混淆加密
------------------------- 相关代码--------------------------------------------
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)] //这个如果不加,则返回值全部为0
public class MySystemTime
{
public ushort wYear; //必须设为ushort,如果为INT ,则数据错乱..WIN32API里是INTERGE
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
public class Win32API
{
// [DllImport("User32.dll")]
[DllImport("kernel32.dll")]
public static extern void GetSystemTime(MySystemTime st);
public static long getl()
{
// 是“协同世界时间”(即UTC,也叫做GMT)格式,需要+8个时区
MySystemTime mt = new MySystemTime();
Win32API.GetSystemTime(mt);
int days = mt.wYear * 365 + mt.wMonth * 30 + mt.wDay;
long millsecs = (long)days * 24 * 3600 * 1000 + (long)getHour(mt.wHour) * 3600 * 1000 + (long)mt.wMinute * 60 * 1000
+ mt.wSecond * 1000 + mt.wMilliseconds;
//string s = mt.wYear + pad0left(mt.wMonth.ToString(), 2) + pad0left(mt.wDay.ToString(), 2);
//s += getHour(mt.wHour) + pad0left(mt.wMinute.ToString(), 2) + pad0left(mt.wSecond.ToString(), 2);
//s += pad0left(mt.wMilliseconds.ToString(), 3);
return millsecs;
}
private static int getHour(ushort hour)
{
ushort t = (ushort)(hour + ushort.Parse("8"));
if (t == 24)
t = 0;
return t;
}
private static string pad0left(string ms, int len)
{
string strOrdid = ms;
for (int i = 0; i < len - ms.Length; i++)
{
strOrdid = "0" + strOrdid;
}
return strOrdid;
}
public static long getMagicNum()
{
int days = 2013 * 365 + 1 * 30 + 1;
long millsecs = (long)days * 24 * 3600 * 1000 + (long)getHour(1) * 3600 * 1000 + (long)1 * 60 * 1000 +1 * 1000 + 0;
return millsecs;
}
}
相关文章推荐
- paip.提升安全性---网站各种验证码特点及识别率
- paip.提升安全性----Des加密 java php python的实现总结
- paip.提升安全性----我们需要多长的密码
- paip.提升安全性----Des加密 java php python的实现总结
- paip.提升安全性-------用户口令密码的检测与生成
- paip.提升用户体验与安全性---注册流程总结
- paip.提升安全性---选择一个安全的加密算法
- paip.提升安全性--------密码控件与软键盘
- paip.提升安全性--360,WI,AWVS三款WEB程序安全检测软件使用总结
- paip.提升用户体验与安全性----cookie盗用检测
- paip.提升安全性----用户资金账户模块平账功能
- paip.提升安全性------本机硬件绑定
- paip.提升安全性---登录密码出错次数检测
- PAIP.提升安全性---更好的签名HASH算法
- paip.提升ASP编程安全性之脚本部件
- paip.提升安全性------登录地区变换后进行验证
- paip.提升安全性-------生成一个安全的验证码
- paip.提升安全性---防止敏感文件被下载
- paip.提升用户体验与提升安全性----记住密码
- paip.提升用户体验与安全性----登录与权限流程总结