软件注册与加密(1)

为了使开发的软件能被更广泛地使用,开发者希望更多的用户能试用软件,而另一方面,又不想让用户长时间免费使用未经授权的软件,这就需要设计软件注册程序。下面通过几个典型实例介绍保护软件安全的方法。
实例468 利用INI文件对软件进行注册
文本框:图16.6  软件注册实例说明
本实例实现使用INI文件对软件的用户信息进行注册的功能。运行程序,输入登录名称、登录口令和注册码,单击【注册】按钮进行注册,如果注册成功,则给出提示;如果信息已注册,系统给出提示信息。实例运行结果如图16.6所示。
技术要点
实现本实例功能主要用到API函数WritePrivateProfileString和GetPrivateProfileString函数。下面分别进行介绍。
(1)WritePrivateProfileString函数
此函数实现对INI文件的写操作。
函数声明如下。
[ DllImport ( "kernel32" ) ]
   private static extern long WritePrivateProfileString ( string section ,string key , string val , string filePath ) ;
参数说明如下。
l     section:INI文件中的段落。
l     key:INI文件中的关键字。
l     val:INI文件中关键字的数值。
l     filePath:INI文件完整的路径和名称。
(2)GetPrivateProfileString函数
此函数实现对INI文件的读操作。
函数声明如下。
[DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
参数说明如表16.5所示。
表16.5                                                                参数说明
参 数 值
说  明
section
INI文件中的段落名称
key
INI文件中的关键字
def
无法读取时候的缺省数值
retVal
读取数值
size
数值的大小
filePath
INI文件的完整路径和名称
注意:使用API函数必须引用System.Runtime.InteropServices命名空间。
实现过程
(1)新建一个Windows应用程序,将其命名为Ex16_07,默认窗体为Form1。
(2)在Form1窗体中,主要添加3个TextBox控件,用于输入注册信息;添加两个Button控件,用来执行注册和退出操作。
(3)主要程序代码。
注册用户信息的实现代码如下:
private void Form1_Load(object sender, EventArgs e)
        {
            FileStream c = new FileStream("C://desck.ini",FileMode.OpenOrCreate,FileAccess.Write);
        }
        [ DllImport ( "kernel32" ) ]
        private static extern long WritePrivateProfileString ( string section ,string key , string val , string filePath ) ;
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder temp = new StringBuilder(200);
            string FileName = "C://desck.ini";//NI文件的完整的路径和名称。
            foreach (object ct in Controls)
            {
                if (ct.GetType().ToString() == "System.Windows.Forms.TextBox")
                {
                    TextBox tx = (TextBox)ct;
                    if (tx.Text == "")
                    {
                        MessageBox.Show(tx.Tag.ToString()+"不能为空");
                    }
                }
            }
            string section = textBox3.Text;//INI文件中的段落
            string key = textBox1.Text;//INI文件中的关键字
             string keyValue = textBox2.Text;//INI文件中的关键字
            int i = GetPrivateProfileString(section, key, "无法读取对应数值!", temp, 200, FileName);//判断是否注册过
            if (temp.ToString() == "无法读取对应数值!")
            {
                WritePrivateProfileString(section, key, keyValue, FileName);
                MessageBox.Show("注册成功写入INI文件!", "信息");
            }
            else
            {
                MessageBox.Show("此信息已注册过了");
            }
        }
举一反三
根据本实例,读者可以实现以下功能。
* 对INI文件加密保存注册信息。
* 对组合INI文件加密保存注册信息。
实例469 利用注册表设计软件注册程序
实例说明
文本框:图16.7  动态变化的软件注册程序大多数应用软件会将用户输入的注册信息写进注册表中,程序运行过程中,可以将这些信息从注册表中读出。本实例主要实现在程序中对注册表进行操作的功能,运行程序,单击【注册】按钮,会将用户输入的信息写入注册表中。实例运行结果如图16.7所示。
技术要点
实现本实例功能主要用到了Microsoft.Win32命名空间下的Registry类的CurrentUser属性、RegistryKey类的OpenSubKey( )方法、GetSubKeyNames( )方法、SetValue( )方法和CreateSubKey( )方法。下面分别进行介绍。
(1)Microsoft.Win32命名空间
Microsoft.Win32命名空间提供两种类型的类:处理由操作系统引发的事件的类和操作系统注册表的类。
(2)RegistryKey类
此类表示Windows注册表中的项级节点,此类是注册表封装。
语法格式为:
public sealed class RegistryKey : MarshalByRefObject, IDisposable
注意:要获取RegistryKey实例,需要使用 Registry类的静态成员之一。
(3)Registry类
此类提供表示Windows注册表中的根项的RegistryKey对象,并提供访问项/值对的static( )方法。
语法格式为:
public static class Registry
(4)CurrentUser属性
此属性包含有关当前用户首选项的信息,该字段读取Windows 注册表中的HKEY_ CURRENT_USER注册表项。
语法格式为:
public static readonly RegistryKey CurrentUser
注意:存储在此项中的信息包括环境变量的设置和有关程序组、颜色、打印机、网络连接和应用程序首选项的数据,此项使建立当前用户的设置更容易。在此项中,软件供应商存储要在其应用程序中使用的当前用户特定的首选项。
(5)OpenSubKey( )方法
此方法检索指定的子项。
语法格式为:
public RegistryKey OpenSubKey (string name,bool writable)
参数说明如下。
l     name:要打开的子项的名称或路径。
l     writable:如果需要项的写访问权限,则设置为True。
l     返回值:请求的子项;如果操作失败,则为空引用。
(6)CreateSubKey( )方法
此方法创建一个新子项或打开一个现有子项以进行写访问。字符串subkey不区分大小写。
语法格式为:
public RegistryKey CreateSubKey (string subkey)
参数说明如下。
l     subkey:要创建或打开的子项的名称或路径。
l     返回值:RegistryKey对象,表示新建的子项或空引用。如果为subkey指定了零长度字符串,则返回当前的RegistryKey对象。
(7)GetSubKeyNames( )方法
此方法检索包含所有子项名称的字符串数组。
语法格式为:
public string[] GetSubKeyNames ()
l     返回值:包含当前项的子项名称的字符串数组。
(8)SetValue( )方法
此方法设置指定的名称/值对。
语法格式为:
public void SetValue (string name,Object value)
参数说明如下。
l     name:要存储的值的名称。
l     value:要存储的数据。
注意:对注册表操作使用RegistryKey类和Registry类时,必须引用Microsoft.Win32 命名空间。
实现过程
(1)新建一个Windows应用程序,将其命名为Ex16_07,默认窗体为Form1。
(2)在Form1窗体中,主要添加3个TextBox控件,用于输入注册信息;添加两个Button控件,用来执行注册和退出操作。
(3)主要程序代码。
     private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text=="")
            {
                MessageBox.Show("公司名称不能为空"); return;
            }
                if(textBox2.Text=="")
                { MessageBox.Show("用户名称不能为空"); return; }
                if (textBox3.Text == "")
                { MessageBox.Show("注册码不能为空"); return; }
                //实例RegistryKey 类对象
            Microsoft.Win32.RegistryKey retkey1 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software").OpenSubKey("ZHY").OpenSubKey("ZHY.INI", true);
            foreach (string strName in retkey1.GetSubKeyNames())//判断注册码是否过期
            {
                 if (strName == textBox3.Text)
                {
                    MessageBox.Show("此注册码已经过期");
                    return;
                }
            }//开始注册信息
            Microsoft.Win32.RegistryKey retkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software", true).CreateSubKey("ZHY").CreateSubKey("ZHY.INI").CreateSubKey(textBox3.Text.TrimEnd());
            retkey.SetValue("UserName", textBox2.Text);
            retkey.SetValue("capataz", textBox1.Text);
            retkey.SetValue("Code", textBox3.Text);
            MessageBox.Show("注册成功,您可以使用本软件");
            Application.Exit();
        }
举一反三
根据本实例,读者可以实现以下功能。
* 注册信息加密后存入注册表。
* 记录用户试用次数的注册程序。 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你的程序加密过OD MD5值支持二次开发使用 防破解验证也可实现一机一码 VC++ 开发 STARTUPINFO startup; PROCESS_INFORMATION process; CString g_strCompanyName1 = ""; CString g_strCompanyName2 = ""; CString g_strCompanyName3 = ""; CString g_strCompanyName = "**"; BOOL CTaiShanApp::InitInstance() { AfxEnableControlContainer(); //#ifdef ZJH m_gMessageID = ::RegisterWindowMessage("WsSendMessageHqData"); CFileFind fnd; if(S_OK != ::CoInitialize (NULL)) return FALSE; // ReadDiskIDPartCwd(); // if(!FyRegister::IsValidUser()) // return FALSE; memset( &startup, 0, sizeof( startup ) ); startup.cb = sizeof( startup ); memset( &process, 0, sizeof( process ) ); if(fnd.FindFile ("WsSendMessageShare.exe")) { m_gbUseExe = true; } hAppMutex=::CreateMutex(NULL,TRUE,m_pszExeName); if(GetLastError() == ERROR_ALREADY_EXISTS) { CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD); while(pPrevWnd) { if(::GetProp(pPrevWnd->GetSafeHwnd(),m_pszExeName)) { if(pPrevWnd->IsIconic()) pPrevWnd->ShowWindow(SW_RESTORE); pPrevWnd->SetForegroundWindow(); pPrevWnd->GetLastActivePopup()->SetForegroundWindow(); return false; } pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT); } return false; } //#endif #ifndef _NET_AUTHEN HMODULE hModule; hModule = LoadLibrary("ide21201.dll"); if (hModule==NULL) { AfxMessageBox("Can't find ide21201.dll"); return FALSE; } char *(WINAPI * GetIdeSerial)(); GetIdeSerial = (char *(WINAPI *)())GetProcAddress(hModule, "GetIdeSerial"); if (GetIdeSerial==NULL) { AfxMessageBox("Can't find GetIdeSerial in ide21201.dll"); return FALSE; } CString strSerialNumber;// = SERIAL_NUMBER; strSerialNumber = GetIdeSerial(); strSerialNumber.TrimLeft(" "); if (strSerialNumber.Compare(SERIAL_NUMBER)!=0) { AfxMessageBox("序列号错误"); return FALSE; } #else CDlgLogin dlgLogin; int nResponse = dlgLogin.DoModal(); if (nResponse!=1) return FALSE; #endif /* CDialogShowInformation dlg; dlg.DoModal();*/ int nResult; m_bAppAuthorized=TRUE; // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. CTaiTestSplash *m_splash; BOOL SplashOpen=FALSE; m_splash = new CTaiTestSplash; SplashOpen=m_splash->Create(); if( SplashOpen ) m_splash->ShowWindow(SW_SHOW); DWORD Currenttime=GetTickCount(); BeginWaitCursor(); #ifdef TEST_USER1 t = CTime::GetCurrentTime(); CTime t2 = g_timeUseEnd; if(t >= t2) { // AfxMessageBox("试用期已过,若想继续使用,请购买正式版!",MB_OK | MB_ICONSTOP); return false; } else 以上为部分代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值