利用磁盘的序列号进行软件加密
用过共享软件的人都知道,一般的共享软件(特别是国外的)在使用一段时间后都会提出一些“苛刻”的要求,如让您输入注册号等等。如果您想在软件中实现该“功能”的话,方法有很多。在这里我介绍一种我认为安全性比较高的一种,仅供参考。
大家都知道,当您在命令行中键入“dir”指令后,系统都会读出一个称作serial number的十六进制数字。这个数字理论上有上亿种可能,而且很难同时找到两个序列号一样的硬盘。这就是我这种注册方法的理论依据,通过判断指定磁盘的序列号决定该机器的注册号。
要实现该功能,如何获得指定磁盘的序列号是最关键的。在windows中,有一个getvolumeinformation的api函数,我们利用这个函数就可以实现。
下面是实现该功能所需要的代码:
private declare function getvolumeinformation& lib "kernel32" _
alias "getvolumeinformationa" (byval lprootpathname as string, _
byval pvolumenamebuffer as string, byval nvolumenamesize as long, _
lpvolumeserialnumber as long, lpmaximumcomponentlength as long, _
lpfilesystemflags as long, byval lpfilesystemnamebuffer as string, _
byval nfilesystemnamesize as long)
private const max_filename_len = 256
public function driveserial(byval sdrv as string) as long
'usage:
'dim ds as long
'ds = driveserial("c")
dim retval as long
dim str as string * max_filename_len
dim str2 as string * max_filename_len
dim a as long
dim b as long
getvolumeinformation sdrv & ":\", str, max_filename_len, retval, _
a, b, str2, max_filename_len
driveserial = retval
end function
如果我们需要某个磁盘的序列号的话,只要driverserial(该磁盘的盘符)即可。如driveraserialnumber=driverserial("a")。
下面,我们就可以利用返回的磁盘序列号进行加密,需要用到一些数学知识。在这里我用了俄罗斯密码表的加密算法对进行了数学变换的序列号进行加密。下面是注册码验证部分的代码:
public function isvalidate(byval src as long, byval value as string) as boolean
dim sourcestring as string
dim newsrc as long
for i = 0 to 30
if (src and 2 ^ i) = 2 ^ i then
sourcestring = sourcestring + "1"
else
sourcestring = sourcestring + "0"
end if
next i
if src < 0 then
sourcestring = sourcestring + "1"
else
sourcestring = sourcestring + "0"
end if
dim table as string
dim tableindex as integer
'================================================================================
'这是密码表,根据你的要求换成别的,不过长度要一致
'================================================================================
'注意:这里的密码表变动后,对应的注册号生成器的密码表也要完全一致才能生成正确的注册号
table = "jsdjfkluwruoisdh;ksadjklwq;abcdefhihl;kladshkjagfwiherqowrlqh"
'================================================================================
dim result as string
dim midword as string
dim midwordvalue as byte
dim resultvalue as byte
for t = 1 to 1
for i = 1 to len(sourcestring)
midword = mid(sourcestring, i, 1)
midwordvalue = asc(midword)
tableindex = tableindex + 1
if tableindex > len(table) then tableindex = 1
resultvalue = asc(mid(table, tableindex, 1)) mod midwordvalue
result = result + hex(resultvalue)
next i
sourcestring = result
next t
dim bittorool as integer
for t = 1 to len(cstr(src))
bittorool = src and 2 ^ t
for i = 1 to bittorool
sourcestring = right(sourcestring, 1) _
+ left(sourcestring, len(sourcestring) - 1)
next i
next t
if sourcestring = value then isvalidate = true
end function
由于代码较长,还有一些部分的代码在此省略,您可以去我的网站(http://vbtechnology.yeah.net)下载源程序研究一下。
最后,我们就可以利用这些子程序进行加密了。