11 个解决方案
#1
来教你如何在vb里嵌入汇编!
作者: wl3000wl
本贴绝对值得你珍藏.
下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.
工程文件分为一个form1.frm 和一个模块module1.bas
----------------------form1.frm的源文件---------------------
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 1965
ClientLeft = 60
ClientTop = 345
ClientWidth = 3105
LinkTopic = "Form1"
ScaleHeight = 1965
ScaleWidth = 3105
StartUpPosition = 2 'Bildschirmmitte
Begin VB.CommandButton Command1
Caption = "Get CPU Name"
Height = 495
Left = 840
TabIndex = 0
Top = 315
Width = 1425
End
Begin VB.Label Label2
Alignment = 2 'Zentriert
AutoSize = -1 'True
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 240
Left = 1515
TabIndex = 2
Top = 1065
Width = 60
End
Begin VB.Label Label1
Alignment = 2 'Zentriert
AutoSize = -1 'True
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 285
Left = 1515
TabIndex = 1
Top = 1350
Width = 75
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
Label1 = ""
Label2 = ""
End Sub
Private Sub Command1_Click()
Label1 = GetCpuName() & " CPU"
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")
End Sub
------------------------------end---------------------------------
下面是modu1e.bas的源代码
----------------------module1.bas的源文件--------------------------
Option Explicit
'
'This shows how to incorporate machine code into VB
'''''''''''''''''''''''''''''''''''''''''''''''''''
'The example fills the array with a few machine instructions and then copies
'them to a procedure address. The modified procedure is then called thru
'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.
'
'##########################################################################
'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody
'can fix that please let me know... UMGEDV@AOL.COM
'The Error is not present in the native compiled version; so I think it got
'something to do with the P-Code Calling Convention (strange though)...
'##########################################################################
'
'Sub Dummy serves to reserve some space to copy the machine instructions into.
'
'
'Tested on Intel and AMD CPU's (uncompiled and compiled)
'
'
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private x As Long
Public Function GetCpuName() As String
Dim MachineCode(0 To 35) As Byte
Dim VarAddr As Long
Dim FunctAddr As Long
Dim EAX As Long
Dim CPUName(1 To 12) As Byte
'set up machine code
MachineCode(0) = &H55 'push ebp
MachineCode(1) = &H8B 'move ebp,esp
MachineCode(2) = &HEC
MachineCode(3) = &H57 'push edi
MachineCode(4) = &H52 'push edx
MachineCode(5) = &H51 'push ecx
MachineCode(6) = &H53 'push ebx
MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]
MachineCode(8) = &H45
MachineCode(9) = &H8
MachineCode(10) = &HF 'cpuid
MachineCode(11) = &HA2
MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]
MachineCode(13) = &H7D
MachineCode(14) = &HC
MachineCode(15) = &H89 'move dword ptr [edi],ebx
MachineCode(16) = &H1F
MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]
MachineCode(18) = &H7D
MachineCode(19) = &H10
MachineCode(20) = &H89 'move dword ptr [edi],ecx
MachineCode(21) = &HF
MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
MachineCode(23) = &H7D
MachineCode(24) = &H14
MachineCode(25) = &H89 'move dword ptr [edi],edx
MachineCode(26) = &H17
MachineCode(27) = &H58 'pop ebx
MachineCode(28) = &H59 'pop ecx
MachineCode(29) = &H5A 'pop edx
MachineCode(30) = &H55 'pop edi
MachineCode(31) = &HC9 'leave
MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
MachineCode(33) = &H10 ' but all produce the stack error
MachineCode(34) = &H0
'tell cpuid what we want
EAX = 0
'get address of Machine Code
VarAddr = VarPtr(MachineCode(0))
'get address of Sub Dummy
FunctAddr = GetAddress(AddressOf Dummy)
'copy the Machine Code to where it can be called
CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code
'call it
On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))
'Debug.Print Err; Err.Description
'MsgBox Err & Err.Description
On Error GoTo 0
GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName
End Function
Private Function GetAddress(Address As Long) As Long
GetAddress = Address
End Function
Private Sub Dummy()
'the code below just reserves some space to copy the machine code into
'it is never executed
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
End Sub
------------------------------end--------------------------------------
作者: wl3000wl
本贴绝对值得你珍藏.
下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.
工程文件分为一个form1.frm 和一个模块module1.bas
----------------------form1.frm的源文件---------------------
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 1965
ClientLeft = 60
ClientTop = 345
ClientWidth = 3105
LinkTopic = "Form1"
ScaleHeight = 1965
ScaleWidth = 3105
StartUpPosition = 2 'Bildschirmmitte
Begin VB.CommandButton Command1
Caption = "Get CPU Name"
Height = 495
Left = 840
TabIndex = 0
Top = 315
Width = 1425
End
Begin VB.Label Label2
Alignment = 2 'Zentriert
AutoSize = -1 'True
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 240
Left = 1515
TabIndex = 2
Top = 1065
Width = 60
End
Begin VB.Label Label1
Alignment = 2 'Zentriert
AutoSize = -1 'True
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 285
Left = 1515
TabIndex = 1
Top = 1350
Width = 75
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
Label1 = ""
Label2 = ""
End Sub
Private Sub Command1_Click()
Label1 = GetCpuName() & " CPU"
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")
End Sub
------------------------------end---------------------------------
下面是modu1e.bas的源代码
----------------------module1.bas的源文件--------------------------
Option Explicit
'
'This shows how to incorporate machine code into VB
'''''''''''''''''''''''''''''''''''''''''''''''''''
'The example fills the array with a few machine instructions and then copies
'them to a procedure address. The modified procedure is then called thru
'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.
'
'##########################################################################
'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody
'can fix that please let me know... UMGEDV@AOL.COM
'The Error is not present in the native compiled version; so I think it got
'something to do with the P-Code Calling Convention (strange though)...
'##########################################################################
'
'Sub Dummy serves to reserve some space to copy the machine instructions into.
'
'
'Tested on Intel and AMD CPU's (uncompiled and compiled)
'
'
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private x As Long
Public Function GetCpuName() As String
Dim MachineCode(0 To 35) As Byte
Dim VarAddr As Long
Dim FunctAddr As Long
Dim EAX As Long
Dim CPUName(1 To 12) As Byte
'set up machine code
MachineCode(0) = &H55 'push ebp
MachineCode(1) = &H8B 'move ebp,esp
MachineCode(2) = &HEC
MachineCode(3) = &H57 'push edi
MachineCode(4) = &H52 'push edx
MachineCode(5) = &H51 'push ecx
MachineCode(6) = &H53 'push ebx
MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]
MachineCode(8) = &H45
MachineCode(9) = &H8
MachineCode(10) = &HF 'cpuid
MachineCode(11) = &HA2
MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]
MachineCode(13) = &H7D
MachineCode(14) = &HC
MachineCode(15) = &H89 'move dword ptr [edi],ebx
MachineCode(16) = &H1F
MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]
MachineCode(18) = &H7D
MachineCode(19) = &H10
MachineCode(20) = &H89 'move dword ptr [edi],ecx
MachineCode(21) = &HF
MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
MachineCode(23) = &H7D
MachineCode(24) = &H14
MachineCode(25) = &H89 'move dword ptr [edi],edx
MachineCode(26) = &H17
MachineCode(27) = &H58 'pop ebx
MachineCode(28) = &H59 'pop ecx
MachineCode(29) = &H5A 'pop edx
MachineCode(30) = &H55 'pop edi
MachineCode(31) = &HC9 'leave
MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
MachineCode(33) = &H10 ' but all produce the stack error
MachineCode(34) = &H0
'tell cpuid what we want
EAX = 0
'get address of Machine Code
VarAddr = VarPtr(MachineCode(0))
'get address of Sub Dummy
FunctAddr = GetAddress(AddressOf Dummy)
'copy the Machine Code to where it can be called
CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code
'call it
On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))
'Debug.Print Err; Err.Description
'MsgBox Err & Err.Description
On Error GoTo 0
GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName
End Function
Private Function GetAddress(Address As Long) As Long
GetAddress = Address
End Function
Private Sub Dummy()
'the code below just reserves some space to copy the machine code into
'it is never executed
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
End Sub
------------------------------end--------------------------------------
#2
收藏!
#3
up!Cool!
#4
不是取的CPU号,只不过是取的名称而已.
#5
相关文章:
http://expert.csdn.net/Expert/topic/2048/2048806.xml?temp=.5135919
http://expert.csdn.net/Expert/topic/1916/1916649.xml?temp=.468075
http://expert.csdn.net/Expert/topic/2018/2018875.xml?temp=.1221277
http://expert.csdn.net/Expert/topic/1928/1928355.xml?temp=.4679834
http://expert.csdn.net/Expert/topic/1969/1969640.xml?temp=.4452631
http://expert.csdn.net/Expert/topic/1965/1965465.xml?temp=.8609735
http://expert.csdn.net/Expert/topic/1986/1986490.xml?temp=.1621973
http://expert.csdn.net/Expert/topic/2018/2018875.xml?temp=.1221277
http://expert.csdn.net/Expert/topic/2006/2006658.xml?temp=.5361292
http://expert.csdn.net/Expert/topic/2005/2005358.xml?temp=.1952021
http://expert.csdn.net/Expert/topic/2048/2048806.xml?temp=.5135919
http://vip.6to23.com/NowCan1/tech/vb_hd_info.htm
读取硬盘序列号的例子,可以用来为软件分配唯一的注册码
相关代码:
为你的VB程序程序加密
VB的好处我就不多说了。VB初学者模仿能力很强,总希望自己的程序看起来专业一点,如用密码登录、制作限次版、限时版、强行启动等等,其实这些东西并不神秘,在VB里只要用少量代码就可实现。
1.最简单的可执行文件密码登录:
在程序启动时加入以下代码:
Private Sub Form_Load()
Dim a as Variant
a=InputBox("请输入密码!")
If a<>"****" Then MsgBox "密码错误,您不能使用本软件!": End '****为预先设定的字符
End Sub
怎么样,能唬人吧。什么?太烂!
2.在硬盘上建立一个文件用于存放密码,这样就可以读写修改了
On Error GoTo sss
'若文件不存在,则捕获该错误,建立密码
Open ("c:abc.abc") For Input As #1
'文件存在,则打开文件
Input #1,b '将密码读入变量b
Close #1
a=InputBox("请输入密码!")
If a<>b Then MsgBox "密码错误,您不能使用本软件!":End
Exit Sub
sss:
a=InputBox("请建立密码!")
Open("c:abc.abc") For Output As #2 '在硬盘上建立存放密码的文件
Print #2,a
Close #2
MsgBox "建立密码成功!"
使用InputBox输入密码的缺点是密码被显示出来,大家可以另建一个窗体代替输入对话框,加入一TextBox并将其PasswordChar属性设为*就行了。文件abc.abc可用任何文本文件打开编辑,因此在你未学会加密算法之前可将文件命名为*.sys或*.dll,并放在windows或system目录下,甚至将其属性设为隐藏,哈哈,系统文件谁敢乱改!不过要小心不要覆盖真正的系统文件。
用启动登录的方法加密会令用户反感,最好只用在软件中较重要的修改数据部分或用于多用户登录。如果你想制作共享软件,那就先试试限次版吧。
3.软件限定使用次数说白了也是在硬盘中的某个地方作个标记,每启动一次就记数一次,当次数加到一定值时就不允许使用软件。以上代码稍加改动也能实现 :
Private Sub Form_Load()
On Error GoTo sss
'若文件不存在,则建立文件
Open("c:abc.abc") For Input As #1
'文件存在,则打开文件
Input #1,b '将数值读入变量b
Close #1
If b>100 Then MsgBox "对不起,您只能使用本软件100次!":End
'提示用户使用次数并退出程序
c=b+1 '计数器加1
Open("c:abc.abc") For Output As #3
Print #3,c '将加1后的数值写入文件
Close #3
Exit Sub
sss:
Open("c:abc.abc") For Output As #2
Print #2,1 '建立文件,并写入数值1
Close #2
End Sub
4.大家一定对win.ini和system.ini文件很熟悉吧,它是一种专门用来保存应用程序初始化信息和运行环境信息的文本文件,Windows软件的初始化参数的获取与保存是通过读取扩展名为.ini的文本文件来实现的。目前很多软件干脆就把软件密码保存在自己的ini文件中。VB只要利用API的GetPrivateProfileString和WritePrivateProfileString两个函数就可以很方便地读写ini文件,从而可以保存、读出和验证密码。首先认识一下ini文件。
ini文件的形式为:
[section1]
keyword1=value1
keyword2=value2
……
[section2]
keyword1=value1
keyword2=value2
……
section是段名,keyword是关键字名,value为关键字对应的设定值
首先用WritePrivateProfileSection创建新的段名和关键字名:
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String,ByVal lpString As String, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String,ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
A=WritePrivateProfileSection ("user","password","c:windowsuser.ini")
在windows目录下的user.ini文件中创建新段名user及该段名下的关键字password,如果目录下没有user.ini文件,则创建该文件
B=WritePrivateProfileString ("user","password","1234","c:windowsuser.ini"),设定关键字user的值为1234。这样在你的user.ini文件就会多出一段:
[user]
password=1234
利用GetPrivateProfileString函数可以读出password的值:
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String,ByVal lpKeyName As Any,ByVal lpDefault As String,ByVal lpReturnedString As String,ByVal nSize As Long,ByVal lpFileName As String) As Long
Dim key as String*255
c=GetPrivateProfileString("user","password","false",key,255,"c:windowsuser.ini")
If key="false" then
MsgBox"文件不存在或没有该字段"
Else: Form1.Print"The password is ";key
该函数将文件user.ini中password的值(即你设定的密码)赋予key,若发生错误(文件不存在或没有该段名)则key的值为“false”,注意一定要声明变量key的长度并与函数中的值一致。这样你就可以将key与登录密码进行对照或直接处理key的值来决定是否继续运行程序。
使用ini文件存储密码还有一个好处,就是设计者可以建立几个段名来存储不同的密码,从而可实现多用户登录。
5.在注册表中标记密码可能是保护你的劳动成果的最高境界了。主要方法是在注册表中创建一个键名,在键值里存放你的密码,以后运行时则取出该数据进行验证或处理,当满足条件时终止程序。可以认为注册表是"以乱取胜",只要你选择到一个隐蔽的位置做标记或存放数据,不用做任何加密算法的处理都应该是比较安全的。令人惊喜的是VB很容易利用API操作注册表。这里只简单介绍几个API函数,大家只要参照函数说明,正确引用变量传递数据,不需要任何技巧就可操作注册表。
RegCreateKeyEx:创建关键字,如果关键字已存在,则将只简单地将它打开
RegOpenKey:用于打开某键
RegSetValueEx:打开某键后,用于设置其键值
RegQueryValueEx:查询一个存在的值,如果此函数调用成功,会返回ERROR_SUCCESS标志
制作限时版只要会用几个函数如day、month、year、date就行了。例如到了2001年就不能执行程序:
a=Year(Date)
if a>=2001 then MsgBox"对不起,该软件已过期":End
你还可以用前面的方法使条件满足时在硬盘上作个标记,而用户通过修改系统时间、重新安装也不能再使用软件。
怎么样?学会了对硬盘的简单读写操作,这些东东一点也不神秘了吧!虽然不是很高明,但很多软件都确实使用这种方法进行简单加密;随着解密手段越来越高明,单一加密方法已成为过去,一些软件同时在ini文件和注册表等地方做标记,当然不是简单的把你输入的保存起来,win9x拨号上网时如果选择保存密码也会在硬盘上生成user.pwl文件,不过该文件加了密,强行用文本编辑器打开时只会看到些乱码。哈哈,又心痒痒想学其他招数了吧
http://expert.csdn.net/Expert/topic/2048/2048806.xml?temp=.5135919
http://expert.csdn.net/Expert/topic/1916/1916649.xml?temp=.468075
http://expert.csdn.net/Expert/topic/2018/2018875.xml?temp=.1221277
http://expert.csdn.net/Expert/topic/1928/1928355.xml?temp=.4679834
http://expert.csdn.net/Expert/topic/1969/1969640.xml?temp=.4452631
http://expert.csdn.net/Expert/topic/1965/1965465.xml?temp=.8609735
http://expert.csdn.net/Expert/topic/1986/1986490.xml?temp=.1621973
http://expert.csdn.net/Expert/topic/2018/2018875.xml?temp=.1221277
http://expert.csdn.net/Expert/topic/2006/2006658.xml?temp=.5361292
http://expert.csdn.net/Expert/topic/2005/2005358.xml?temp=.1952021
http://expert.csdn.net/Expert/topic/2048/2048806.xml?temp=.5135919
http://vip.6to23.com/NowCan1/tech/vb_hd_info.htm
读取硬盘序列号的例子,可以用来为软件分配唯一的注册码
相关代码:
为你的VB程序程序加密
VB的好处我就不多说了。VB初学者模仿能力很强,总希望自己的程序看起来专业一点,如用密码登录、制作限次版、限时版、强行启动等等,其实这些东西并不神秘,在VB里只要用少量代码就可实现。
1.最简单的可执行文件密码登录:
在程序启动时加入以下代码:
Private Sub Form_Load()
Dim a as Variant
a=InputBox("请输入密码!")
If a<>"****" Then MsgBox "密码错误,您不能使用本软件!": End '****为预先设定的字符
End Sub
怎么样,能唬人吧。什么?太烂!
2.在硬盘上建立一个文件用于存放密码,这样就可以读写修改了
On Error GoTo sss
'若文件不存在,则捕获该错误,建立密码
Open ("c:abc.abc") For Input As #1
'文件存在,则打开文件
Input #1,b '将密码读入变量b
Close #1
a=InputBox("请输入密码!")
If a<>b Then MsgBox "密码错误,您不能使用本软件!":End
Exit Sub
sss:
a=InputBox("请建立密码!")
Open("c:abc.abc") For Output As #2 '在硬盘上建立存放密码的文件
Print #2,a
Close #2
MsgBox "建立密码成功!"
使用InputBox输入密码的缺点是密码被显示出来,大家可以另建一个窗体代替输入对话框,加入一TextBox并将其PasswordChar属性设为*就行了。文件abc.abc可用任何文本文件打开编辑,因此在你未学会加密算法之前可将文件命名为*.sys或*.dll,并放在windows或system目录下,甚至将其属性设为隐藏,哈哈,系统文件谁敢乱改!不过要小心不要覆盖真正的系统文件。
用启动登录的方法加密会令用户反感,最好只用在软件中较重要的修改数据部分或用于多用户登录。如果你想制作共享软件,那就先试试限次版吧。
3.软件限定使用次数说白了也是在硬盘中的某个地方作个标记,每启动一次就记数一次,当次数加到一定值时就不允许使用软件。以上代码稍加改动也能实现 :
Private Sub Form_Load()
On Error GoTo sss
'若文件不存在,则建立文件
Open("c:abc.abc") For Input As #1
'文件存在,则打开文件
Input #1,b '将数值读入变量b
Close #1
If b>100 Then MsgBox "对不起,您只能使用本软件100次!":End
'提示用户使用次数并退出程序
c=b+1 '计数器加1
Open("c:abc.abc") For Output As #3
Print #3,c '将加1后的数值写入文件
Close #3
Exit Sub
sss:
Open("c:abc.abc") For Output As #2
Print #2,1 '建立文件,并写入数值1
Close #2
End Sub
4.大家一定对win.ini和system.ini文件很熟悉吧,它是一种专门用来保存应用程序初始化信息和运行环境信息的文本文件,Windows软件的初始化参数的获取与保存是通过读取扩展名为.ini的文本文件来实现的。目前很多软件干脆就把软件密码保存在自己的ini文件中。VB只要利用API的GetPrivateProfileString和WritePrivateProfileString两个函数就可以很方便地读写ini文件,从而可以保存、读出和验证密码。首先认识一下ini文件。
ini文件的形式为:
[section1]
keyword1=value1
keyword2=value2
……
[section2]
keyword1=value1
keyword2=value2
……
section是段名,keyword是关键字名,value为关键字对应的设定值
首先用WritePrivateProfileSection创建新的段名和关键字名:
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String,ByVal lpString As String, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String,ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
A=WritePrivateProfileSection ("user","password","c:windowsuser.ini")
在windows目录下的user.ini文件中创建新段名user及该段名下的关键字password,如果目录下没有user.ini文件,则创建该文件
B=WritePrivateProfileString ("user","password","1234","c:windowsuser.ini"),设定关键字user的值为1234。这样在你的user.ini文件就会多出一段:
[user]
password=1234
利用GetPrivateProfileString函数可以读出password的值:
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String,ByVal lpKeyName As Any,ByVal lpDefault As String,ByVal lpReturnedString As String,ByVal nSize As Long,ByVal lpFileName As String) As Long
Dim key as String*255
c=GetPrivateProfileString("user","password","false",key,255,"c:windowsuser.ini")
If key="false" then
MsgBox"文件不存在或没有该字段"
Else: Form1.Print"The password is ";key
该函数将文件user.ini中password的值(即你设定的密码)赋予key,若发生错误(文件不存在或没有该段名)则key的值为“false”,注意一定要声明变量key的长度并与函数中的值一致。这样你就可以将key与登录密码进行对照或直接处理key的值来决定是否继续运行程序。
使用ini文件存储密码还有一个好处,就是设计者可以建立几个段名来存储不同的密码,从而可实现多用户登录。
5.在注册表中标记密码可能是保护你的劳动成果的最高境界了。主要方法是在注册表中创建一个键名,在键值里存放你的密码,以后运行时则取出该数据进行验证或处理,当满足条件时终止程序。可以认为注册表是"以乱取胜",只要你选择到一个隐蔽的位置做标记或存放数据,不用做任何加密算法的处理都应该是比较安全的。令人惊喜的是VB很容易利用API操作注册表。这里只简单介绍几个API函数,大家只要参照函数说明,正确引用变量传递数据,不需要任何技巧就可操作注册表。
RegCreateKeyEx:创建关键字,如果关键字已存在,则将只简单地将它打开
RegOpenKey:用于打开某键
RegSetValueEx:打开某键后,用于设置其键值
RegQueryValueEx:查询一个存在的值,如果此函数调用成功,会返回ERROR_SUCCESS标志
制作限时版只要会用几个函数如day、month、year、date就行了。例如到了2001年就不能执行程序:
a=Year(Date)
if a>=2001 then MsgBox"对不起,该软件已过期":End
你还可以用前面的方法使条件满足时在硬盘上作个标记,而用户通过修改系统时间、重新安装也不能再使用软件。
怎么样?学会了对硬盘的简单读写操作,这些东东一点也不神秘了吧!虽然不是很高明,但很多软件都确实使用这种方法进行简单加密;随着解密手段越来越高明,单一加密方法已成为过去,一些软件同时在ini文件和注册表等地方做标记,当然不是简单的把你输入的保存起来,win9x拨号上网时如果选择保存密码也会在硬盘上生成user.pwl文件,不过该文件加了密,强行用文本编辑器打开时只会看到些乱码。哈哈,又心痒痒想学其他招数了吧
#6
不错
#7
不错是不错
简直是精妙
可是这样的程序有多少Visual Basic程序员能写?
所以搂主的问题
我绝的好的解决方法是用VC写读区CPU东东的DLL
然后在VB中调用
一家之言
简直是精妙
可是这样的程序有多少Visual Basic程序员能写?
所以搂主的问题
我绝的好的解决方法是用VC写读区CPU东东的DLL
然后在VB中调用
一家之言
#8
up
#9
帮你顶
#10
我曾经取过CPU的号,可这个号是有重复的
所以不能用以加密
而且不同的CPU,它的指令集也不尽相同
最好在网卡和硬盘上下功夫
所以不能用以加密
而且不同的CPU,它的指令集也不尽相同
最好在网卡和硬盘上下功夫
#11
http://expert.csdn.net/Expert/topic/1880/1880880.xml?temp=.325741
http://expert.csdn.net/Expert/topic/1972/1972386.xml?temp=.3529016
http://expert.csdn.net/Expert/topic/1969/1969640.xml?temp=.2918817
http://expert.csdn.net/Expert/topic/1880/1880883.xml?temp=.9266626
http://expert.csdn.net/Expert/topic/1838/1838722.xml?temp=.7783167
http://expert.csdn.net/Expert/topic/1680/1680715.xml?temp=.3237268
http://expert.csdn.net/Expert/topic/1328/1328644.xml?temp=.9751093
http://expert.csdn.net/Expert/topic/1972/1972386.xml?temp=.3529016
http://expert.csdn.net/Expert/topic/1969/1969640.xml?temp=.2918817
http://expert.csdn.net/Expert/topic/1880/1880883.xml?temp=.9266626
http://expert.csdn.net/Expert/topic/1838/1838722.xml?temp=.7783167
http://expert.csdn.net/Expert/topic/1680/1680715.xml?temp=.3237268
http://expert.csdn.net/Expert/topic/1328/1328644.xml?temp=.9751093
#1
来教你如何在vb里嵌入汇编!
作者: wl3000wl
本贴绝对值得你珍藏.
下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.
工程文件分为一个form1.frm 和一个模块module1.bas
----------------------form1.frm的源文件---------------------
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 1965
ClientLeft = 60
ClientTop = 345
ClientWidth = 3105
LinkTopic = "Form1"
ScaleHeight = 1965
ScaleWidth = 3105
StartUpPosition = 2 'Bildschirmmitte
Begin VB.CommandButton Command1
Caption = "Get CPU Name"
Height = 495
Left = 840
TabIndex = 0
Top = 315
Width = 1425
End
Begin VB.Label Label2
Alignment = 2 'Zentriert
AutoSize = -1 'True
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 240
Left = 1515
TabIndex = 2
Top = 1065
Width = 60
End
Begin VB.Label Label1
Alignment = 2 'Zentriert
AutoSize = -1 'True
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 285
Left = 1515
TabIndex = 1
Top = 1350
Width = 75
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
Label1 = ""
Label2 = ""
End Sub
Private Sub Command1_Click()
Label1 = GetCpuName() & " CPU"
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")
End Sub
------------------------------end---------------------------------
下面是modu1e.bas的源代码
----------------------module1.bas的源文件--------------------------
Option Explicit
'
'This shows how to incorporate machine code into VB
'''''''''''''''''''''''''''''''''''''''''''''''''''
'The example fills the array with a few machine instructions and then copies
'them to a procedure address. The modified procedure is then called thru
'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.
'
'##########################################################################
'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody
'can fix that please let me know... UMGEDV@AOL.COM
'The Error is not present in the native compiled version; so I think it got
'something to do with the P-Code Calling Convention (strange though)...
'##########################################################################
'
'Sub Dummy serves to reserve some space to copy the machine instructions into.
'
'
'Tested on Intel and AMD CPU's (uncompiled and compiled)
'
'
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private x As Long
Public Function GetCpuName() As String
Dim MachineCode(0 To 35) As Byte
Dim VarAddr As Long
Dim FunctAddr As Long
Dim EAX As Long
Dim CPUName(1 To 12) As Byte
'set up machine code
MachineCode(0) = &H55 'push ebp
MachineCode(1) = &H8B 'move ebp,esp
MachineCode(2) = &HEC
MachineCode(3) = &H57 'push edi
MachineCode(4) = &H52 'push edx
MachineCode(5) = &H51 'push ecx
MachineCode(6) = &H53 'push ebx
MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]
MachineCode(8) = &H45
MachineCode(9) = &H8
MachineCode(10) = &HF 'cpuid
MachineCode(11) = &HA2
MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]
MachineCode(13) = &H7D
MachineCode(14) = &HC
MachineCode(15) = &H89 'move dword ptr [edi],ebx
MachineCode(16) = &H1F
MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]
MachineCode(18) = &H7D
MachineCode(19) = &H10
MachineCode(20) = &H89 'move dword ptr [edi],ecx
MachineCode(21) = &HF
MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
MachineCode(23) = &H7D
MachineCode(24) = &H14
MachineCode(25) = &H89 'move dword ptr [edi],edx
MachineCode(26) = &H17
MachineCode(27) = &H58 'pop ebx
MachineCode(28) = &H59 'pop ecx
MachineCode(29) = &H5A 'pop edx
MachineCode(30) = &H55 'pop edi
MachineCode(31) = &HC9 'leave
MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
MachineCode(33) = &H10 ' but all produce the stack error
MachineCode(34) = &H0
'tell cpuid what we want
EAX = 0
'get address of Machine Code
VarAddr = VarPtr(MachineCode(0))
'get address of Sub Dummy
FunctAddr = GetAddress(AddressOf Dummy)
'copy the Machine Code to where it can be called
CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code
'call it
On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))
'Debug.Print Err; Err.Description
'MsgBox Err & Err.Description
On Error GoTo 0
GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName
End Function
Private Function GetAddress(Address As Long) As Long
GetAddress = Address
End Function
Private Sub Dummy()
'the code below just reserves some space to copy the machine code into
'it is never executed
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
End Sub
------------------------------end--------------------------------------
作者: wl3000wl
本贴绝对值得你珍藏.
下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.
工程文件分为一个form1.frm 和一个模块module1.bas
----------------------form1.frm的源文件---------------------
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 1965
ClientLeft = 60
ClientTop = 345
ClientWidth = 3105
LinkTopic = "Form1"
ScaleHeight = 1965
ScaleWidth = 3105
StartUpPosition = 2 'Bildschirmmitte
Begin VB.CommandButton Command1
Caption = "Get CPU Name"
Height = 495
Left = 840
TabIndex = 0
Top = 315
Width = 1425
End
Begin VB.Label Label2
Alignment = 2 'Zentriert
AutoSize = -1 'True
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 240
Left = 1515
TabIndex = 2
Top = 1065
Width = 60
End
Begin VB.Label Label1
Alignment = 2 'Zentriert
AutoSize = -1 'True
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 285
Left = 1515
TabIndex = 1
Top = 1350
Width = 75
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
Label1 = ""
Label2 = ""
End Sub
Private Sub Command1_Click()
Label1 = GetCpuName() & " CPU"
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")
End Sub
------------------------------end---------------------------------
下面是modu1e.bas的源代码
----------------------module1.bas的源文件--------------------------
Option Explicit
'
'This shows how to incorporate machine code into VB
'''''''''''''''''''''''''''''''''''''''''''''''''''
'The example fills the array with a few machine instructions and then copies
'them to a procedure address. The modified procedure is then called thru
'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.
'
'##########################################################################
'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody
'can fix that please let me know... UMGEDV@AOL.COM
'The Error is not present in the native compiled version; so I think it got
'something to do with the P-Code Calling Convention (strange though)...
'##########################################################################
'
'Sub Dummy serves to reserve some space to copy the machine instructions into.
'
'
'Tested on Intel and AMD CPU's (uncompiled and compiled)
'
'
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private x As Long
Public Function GetCpuName() As String
Dim MachineCode(0 To 35) As Byte
Dim VarAddr As Long
Dim FunctAddr As Long
Dim EAX As Long
Dim CPUName(1 To 12) As Byte
'set up machine code
MachineCode(0) = &H55 'push ebp
MachineCode(1) = &H8B 'move ebp,esp
MachineCode(2) = &HEC
MachineCode(3) = &H57 'push edi
MachineCode(4) = &H52 'push edx
MachineCode(5) = &H51 'push ecx
MachineCode(6) = &H53 'push ebx
MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]
MachineCode(8) = &H45
MachineCode(9) = &H8
MachineCode(10) = &HF 'cpuid
MachineCode(11) = &HA2
MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]
MachineCode(13) = &H7D
MachineCode(14) = &HC
MachineCode(15) = &H89 'move dword ptr [edi],ebx
MachineCode(16) = &H1F
MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]
MachineCode(18) = &H7D
MachineCode(19) = &H10
MachineCode(20) = &H89 'move dword ptr [edi],ecx
MachineCode(21) = &HF
MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
MachineCode(23) = &H7D
MachineCode(24) = &H14
MachineCode(25) = &H89 'move dword ptr [edi],edx
MachineCode(26) = &H17
MachineCode(27) = &H58 'pop ebx
MachineCode(28) = &H59 'pop ecx
MachineCode(29) = &H5A 'pop edx
MachineCode(30) = &H55 'pop edi
MachineCode(31) = &HC9 'leave
MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
MachineCode(33) = &H10 ' but all produce the stack error
MachineCode(34) = &H0
'tell cpuid what we want
EAX = 0
'get address of Machine Code
VarAddr = VarPtr(MachineCode(0))
'get address of Sub Dummy
FunctAddr = GetAddress(AddressOf Dummy)
'copy the Machine Code to where it can be called
CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code
'call it
On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))
'Debug.Print Err; Err.Description
'MsgBox Err & Err.Description
On Error GoTo 0
GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName
End Function
Private Function GetAddress(Address As Long) As Long
GetAddress = Address
End Function
Private Sub Dummy()
'the code below just reserves some space to copy the machine code into
'it is never executed
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
End Sub
------------------------------end--------------------------------------
#2
收藏!
#3
up!Cool!
#4
不是取的CPU号,只不过是取的名称而已.
#5
相关文章:
http://expert.csdn.net/Expert/topic/2048/2048806.xml?temp=.5135919
http://expert.csdn.net/Expert/topic/1916/1916649.xml?temp=.468075
http://expert.csdn.net/Expert/topic/2018/2018875.xml?temp=.1221277
http://expert.csdn.net/Expert/topic/1928/1928355.xml?temp=.4679834
http://expert.csdn.net/Expert/topic/1969/1969640.xml?temp=.4452631
http://expert.csdn.net/Expert/topic/1965/1965465.xml?temp=.8609735
http://expert.csdn.net/Expert/topic/1986/1986490.xml?temp=.1621973
http://expert.csdn.net/Expert/topic/2018/2018875.xml?temp=.1221277
http://expert.csdn.net/Expert/topic/2006/2006658.xml?temp=.5361292
http://expert.csdn.net/Expert/topic/2005/2005358.xml?temp=.1952021
http://expert.csdn.net/Expert/topic/2048/2048806.xml?temp=.5135919
http://vip.6to23.com/NowCan1/tech/vb_hd_info.htm
读取硬盘序列号的例子,可以用来为软件分配唯一的注册码
相关代码:
为你的VB程序程序加密
VB的好处我就不多说了。VB初学者模仿能力很强,总希望自己的程序看起来专业一点,如用密码登录、制作限次版、限时版、强行启动等等,其实这些东西并不神秘,在VB里只要用少量代码就可实现。
1.最简单的可执行文件密码登录:
在程序启动时加入以下代码:
Private Sub Form_Load()
Dim a as Variant
a=InputBox("请输入密码!")
If a<>"****" Then MsgBox "密码错误,您不能使用本软件!": End '****为预先设定的字符
End Sub
怎么样,能唬人吧。什么?太烂!
2.在硬盘上建立一个文件用于存放密码,这样就可以读写修改了
On Error GoTo sss
'若文件不存在,则捕获该错误,建立密码
Open ("c:abc.abc") For Input As #1
'文件存在,则打开文件
Input #1,b '将密码读入变量b
Close #1
a=InputBox("请输入密码!")
If a<>b Then MsgBox "密码错误,您不能使用本软件!":End
Exit Sub
sss:
a=InputBox("请建立密码!")
Open("c:abc.abc") For Output As #2 '在硬盘上建立存放密码的文件
Print #2,a
Close #2
MsgBox "建立密码成功!"
使用InputBox输入密码的缺点是密码被显示出来,大家可以另建一个窗体代替输入对话框,加入一TextBox并将其PasswordChar属性设为*就行了。文件abc.abc可用任何文本文件打开编辑,因此在你未学会加密算法之前可将文件命名为*.sys或*.dll,并放在windows或system目录下,甚至将其属性设为隐藏,哈哈,系统文件谁敢乱改!不过要小心不要覆盖真正的系统文件。
用启动登录的方法加密会令用户反感,最好只用在软件中较重要的修改数据部分或用于多用户登录。如果你想制作共享软件,那就先试试限次版吧。
3.软件限定使用次数说白了也是在硬盘中的某个地方作个标记,每启动一次就记数一次,当次数加到一定值时就不允许使用软件。以上代码稍加改动也能实现 :
Private Sub Form_Load()
On Error GoTo sss
'若文件不存在,则建立文件
Open("c:abc.abc") For Input As #1
'文件存在,则打开文件
Input #1,b '将数值读入变量b
Close #1
If b>100 Then MsgBox "对不起,您只能使用本软件100次!":End
'提示用户使用次数并退出程序
c=b+1 '计数器加1
Open("c:abc.abc") For Output As #3
Print #3,c '将加1后的数值写入文件
Close #3
Exit Sub
sss:
Open("c:abc.abc") For Output As #2
Print #2,1 '建立文件,并写入数值1
Close #2
End Sub
4.大家一定对win.ini和system.ini文件很熟悉吧,它是一种专门用来保存应用程序初始化信息和运行环境信息的文本文件,Windows软件的初始化参数的获取与保存是通过读取扩展名为.ini的文本文件来实现的。目前很多软件干脆就把软件密码保存在自己的ini文件中。VB只要利用API的GetPrivateProfileString和WritePrivateProfileString两个函数就可以很方便地读写ini文件,从而可以保存、读出和验证密码。首先认识一下ini文件。
ini文件的形式为:
[section1]
keyword1=value1
keyword2=value2
……
[section2]
keyword1=value1
keyword2=value2
……
section是段名,keyword是关键字名,value为关键字对应的设定值
首先用WritePrivateProfileSection创建新的段名和关键字名:
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String,ByVal lpString As String, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String,ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
A=WritePrivateProfileSection ("user","password","c:windowsuser.ini")
在windows目录下的user.ini文件中创建新段名user及该段名下的关键字password,如果目录下没有user.ini文件,则创建该文件
B=WritePrivateProfileString ("user","password","1234","c:windowsuser.ini"),设定关键字user的值为1234。这样在你的user.ini文件就会多出一段:
[user]
password=1234
利用GetPrivateProfileString函数可以读出password的值:
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String,ByVal lpKeyName As Any,ByVal lpDefault As String,ByVal lpReturnedString As String,ByVal nSize As Long,ByVal lpFileName As String) As Long
Dim key as String*255
c=GetPrivateProfileString("user","password","false",key,255,"c:windowsuser.ini")
If key="false" then
MsgBox"文件不存在或没有该字段"
Else: Form1.Print"The password is ";key
该函数将文件user.ini中password的值(即你设定的密码)赋予key,若发生错误(文件不存在或没有该段名)则key的值为“false”,注意一定要声明变量key的长度并与函数中的值一致。这样你就可以将key与登录密码进行对照或直接处理key的值来决定是否继续运行程序。
使用ini文件存储密码还有一个好处,就是设计者可以建立几个段名来存储不同的密码,从而可实现多用户登录。
5.在注册表中标记密码可能是保护你的劳动成果的最高境界了。主要方法是在注册表中创建一个键名,在键值里存放你的密码,以后运行时则取出该数据进行验证或处理,当满足条件时终止程序。可以认为注册表是"以乱取胜",只要你选择到一个隐蔽的位置做标记或存放数据,不用做任何加密算法的处理都应该是比较安全的。令人惊喜的是VB很容易利用API操作注册表。这里只简单介绍几个API函数,大家只要参照函数说明,正确引用变量传递数据,不需要任何技巧就可操作注册表。
RegCreateKeyEx:创建关键字,如果关键字已存在,则将只简单地将它打开
RegOpenKey:用于打开某键
RegSetValueEx:打开某键后,用于设置其键值
RegQueryValueEx:查询一个存在的值,如果此函数调用成功,会返回ERROR_SUCCESS标志
制作限时版只要会用几个函数如day、month、year、date就行了。例如到了2001年就不能执行程序:
a=Year(Date)
if a>=2001 then MsgBox"对不起,该软件已过期":End
你还可以用前面的方法使条件满足时在硬盘上作个标记,而用户通过修改系统时间、重新安装也不能再使用软件。
怎么样?学会了对硬盘的简单读写操作,这些东东一点也不神秘了吧!虽然不是很高明,但很多软件都确实使用这种方法进行简单加密;随着解密手段越来越高明,单一加密方法已成为过去,一些软件同时在ini文件和注册表等地方做标记,当然不是简单的把你输入的保存起来,win9x拨号上网时如果选择保存密码也会在硬盘上生成user.pwl文件,不过该文件加了密,强行用文本编辑器打开时只会看到些乱码。哈哈,又心痒痒想学其他招数了吧
http://expert.csdn.net/Expert/topic/2048/2048806.xml?temp=.5135919
http://expert.csdn.net/Expert/topic/1916/1916649.xml?temp=.468075
http://expert.csdn.net/Expert/topic/2018/2018875.xml?temp=.1221277
http://expert.csdn.net/Expert/topic/1928/1928355.xml?temp=.4679834
http://expert.csdn.net/Expert/topic/1969/1969640.xml?temp=.4452631
http://expert.csdn.net/Expert/topic/1965/1965465.xml?temp=.8609735
http://expert.csdn.net/Expert/topic/1986/1986490.xml?temp=.1621973
http://expert.csdn.net/Expert/topic/2018/2018875.xml?temp=.1221277
http://expert.csdn.net/Expert/topic/2006/2006658.xml?temp=.5361292
http://expert.csdn.net/Expert/topic/2005/2005358.xml?temp=.1952021
http://expert.csdn.net/Expert/topic/2048/2048806.xml?temp=.5135919
http://vip.6to23.com/NowCan1/tech/vb_hd_info.htm
读取硬盘序列号的例子,可以用来为软件分配唯一的注册码
相关代码:
为你的VB程序程序加密
VB的好处我就不多说了。VB初学者模仿能力很强,总希望自己的程序看起来专业一点,如用密码登录、制作限次版、限时版、强行启动等等,其实这些东西并不神秘,在VB里只要用少量代码就可实现。
1.最简单的可执行文件密码登录:
在程序启动时加入以下代码:
Private Sub Form_Load()
Dim a as Variant
a=InputBox("请输入密码!")
If a<>"****" Then MsgBox "密码错误,您不能使用本软件!": End '****为预先设定的字符
End Sub
怎么样,能唬人吧。什么?太烂!
2.在硬盘上建立一个文件用于存放密码,这样就可以读写修改了
On Error GoTo sss
'若文件不存在,则捕获该错误,建立密码
Open ("c:abc.abc") For Input As #1
'文件存在,则打开文件
Input #1,b '将密码读入变量b
Close #1
a=InputBox("请输入密码!")
If a<>b Then MsgBox "密码错误,您不能使用本软件!":End
Exit Sub
sss:
a=InputBox("请建立密码!")
Open("c:abc.abc") For Output As #2 '在硬盘上建立存放密码的文件
Print #2,a
Close #2
MsgBox "建立密码成功!"
使用InputBox输入密码的缺点是密码被显示出来,大家可以另建一个窗体代替输入对话框,加入一TextBox并将其PasswordChar属性设为*就行了。文件abc.abc可用任何文本文件打开编辑,因此在你未学会加密算法之前可将文件命名为*.sys或*.dll,并放在windows或system目录下,甚至将其属性设为隐藏,哈哈,系统文件谁敢乱改!不过要小心不要覆盖真正的系统文件。
用启动登录的方法加密会令用户反感,最好只用在软件中较重要的修改数据部分或用于多用户登录。如果你想制作共享软件,那就先试试限次版吧。
3.软件限定使用次数说白了也是在硬盘中的某个地方作个标记,每启动一次就记数一次,当次数加到一定值时就不允许使用软件。以上代码稍加改动也能实现 :
Private Sub Form_Load()
On Error GoTo sss
'若文件不存在,则建立文件
Open("c:abc.abc") For Input As #1
'文件存在,则打开文件
Input #1,b '将数值读入变量b
Close #1
If b>100 Then MsgBox "对不起,您只能使用本软件100次!":End
'提示用户使用次数并退出程序
c=b+1 '计数器加1
Open("c:abc.abc") For Output As #3
Print #3,c '将加1后的数值写入文件
Close #3
Exit Sub
sss:
Open("c:abc.abc") For Output As #2
Print #2,1 '建立文件,并写入数值1
Close #2
End Sub
4.大家一定对win.ini和system.ini文件很熟悉吧,它是一种专门用来保存应用程序初始化信息和运行环境信息的文本文件,Windows软件的初始化参数的获取与保存是通过读取扩展名为.ini的文本文件来实现的。目前很多软件干脆就把软件密码保存在自己的ini文件中。VB只要利用API的GetPrivateProfileString和WritePrivateProfileString两个函数就可以很方便地读写ini文件,从而可以保存、读出和验证密码。首先认识一下ini文件。
ini文件的形式为:
[section1]
keyword1=value1
keyword2=value2
……
[section2]
keyword1=value1
keyword2=value2
……
section是段名,keyword是关键字名,value为关键字对应的设定值
首先用WritePrivateProfileSection创建新的段名和关键字名:
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String,ByVal lpString As String, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String,ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
A=WritePrivateProfileSection ("user","password","c:windowsuser.ini")
在windows目录下的user.ini文件中创建新段名user及该段名下的关键字password,如果目录下没有user.ini文件,则创建该文件
B=WritePrivateProfileString ("user","password","1234","c:windowsuser.ini"),设定关键字user的值为1234。这样在你的user.ini文件就会多出一段:
[user]
password=1234
利用GetPrivateProfileString函数可以读出password的值:
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String,ByVal lpKeyName As Any,ByVal lpDefault As String,ByVal lpReturnedString As String,ByVal nSize As Long,ByVal lpFileName As String) As Long
Dim key as String*255
c=GetPrivateProfileString("user","password","false",key,255,"c:windowsuser.ini")
If key="false" then
MsgBox"文件不存在或没有该字段"
Else: Form1.Print"The password is ";key
该函数将文件user.ini中password的值(即你设定的密码)赋予key,若发生错误(文件不存在或没有该段名)则key的值为“false”,注意一定要声明变量key的长度并与函数中的值一致。这样你就可以将key与登录密码进行对照或直接处理key的值来决定是否继续运行程序。
使用ini文件存储密码还有一个好处,就是设计者可以建立几个段名来存储不同的密码,从而可实现多用户登录。
5.在注册表中标记密码可能是保护你的劳动成果的最高境界了。主要方法是在注册表中创建一个键名,在键值里存放你的密码,以后运行时则取出该数据进行验证或处理,当满足条件时终止程序。可以认为注册表是"以乱取胜",只要你选择到一个隐蔽的位置做标记或存放数据,不用做任何加密算法的处理都应该是比较安全的。令人惊喜的是VB很容易利用API操作注册表。这里只简单介绍几个API函数,大家只要参照函数说明,正确引用变量传递数据,不需要任何技巧就可操作注册表。
RegCreateKeyEx:创建关键字,如果关键字已存在,则将只简单地将它打开
RegOpenKey:用于打开某键
RegSetValueEx:打开某键后,用于设置其键值
RegQueryValueEx:查询一个存在的值,如果此函数调用成功,会返回ERROR_SUCCESS标志
制作限时版只要会用几个函数如day、month、year、date就行了。例如到了2001年就不能执行程序:
a=Year(Date)
if a>=2001 then MsgBox"对不起,该软件已过期":End
你还可以用前面的方法使条件满足时在硬盘上作个标记,而用户通过修改系统时间、重新安装也不能再使用软件。
怎么样?学会了对硬盘的简单读写操作,这些东东一点也不神秘了吧!虽然不是很高明,但很多软件都确实使用这种方法进行简单加密;随着解密手段越来越高明,单一加密方法已成为过去,一些软件同时在ini文件和注册表等地方做标记,当然不是简单的把你输入的保存起来,win9x拨号上网时如果选择保存密码也会在硬盘上生成user.pwl文件,不过该文件加了密,强行用文本编辑器打开时只会看到些乱码。哈哈,又心痒痒想学其他招数了吧
#6
不错
#7
不错是不错
简直是精妙
可是这样的程序有多少Visual Basic程序员能写?
所以搂主的问题
我绝的好的解决方法是用VC写读区CPU东东的DLL
然后在VB中调用
一家之言
简直是精妙
可是这样的程序有多少Visual Basic程序员能写?
所以搂主的问题
我绝的好的解决方法是用VC写读区CPU东东的DLL
然后在VB中调用
一家之言
#8
up
#9
帮你顶
#10
我曾经取过CPU的号,可这个号是有重复的
所以不能用以加密
而且不同的CPU,它的指令集也不尽相同
最好在网卡和硬盘上下功夫
所以不能用以加密
而且不同的CPU,它的指令集也不尽相同
最好在网卡和硬盘上下功夫
#11
http://expert.csdn.net/Expert/topic/1880/1880880.xml?temp=.325741
http://expert.csdn.net/Expert/topic/1972/1972386.xml?temp=.3529016
http://expert.csdn.net/Expert/topic/1969/1969640.xml?temp=.2918817
http://expert.csdn.net/Expert/topic/1880/1880883.xml?temp=.9266626
http://expert.csdn.net/Expert/topic/1838/1838722.xml?temp=.7783167
http://expert.csdn.net/Expert/topic/1680/1680715.xml?temp=.3237268
http://expert.csdn.net/Expert/topic/1328/1328644.xml?temp=.9751093
http://expert.csdn.net/Expert/topic/1972/1972386.xml?temp=.3529016
http://expert.csdn.net/Expert/topic/1969/1969640.xml?temp=.2918817
http://expert.csdn.net/Expert/topic/1880/1880883.xml?temp=.9266626
http://expert.csdn.net/Expert/topic/1838/1838722.xml?temp=.7783167
http://expert.csdn.net/Expert/topic/1680/1680715.xml?temp=.3237268
http://expert.csdn.net/Expert/topic/1328/1328644.xml?temp=.9751093