C# 超级狗 二次开发 读写数据 激活验证 存储数据库连接字符串

本文主要讲解如果使用C#语言来对超级狗进行二次开发,如果仅仅是做个激活的功能,可以参照另一篇博客,地址:http://www.cnblogs.com/dathlin/p/8487842.html

 

 

继续主题研究,如果使用C#来对超级狗进行二次开发。

楼主在研究这个超级狗的时候,直接某宝购买了开发狗和子狗两个狗,并没有购买demo狗,所以后来在研究开发套件里的Demo项目时,老是提醒找不到超级狗,一定要开发套件里生成的公司的独一无二的组件进行访问。

 

第一步,当然是下载公司的API库了

其中开发商代码文件很关键,等会要用到

然后耐心的等待下载完成就好了。

 

 

准备资源


接下来要找到两个重要的东西才能进行开发,一个是组件库,一个是开发商代码文件。

独一无二的组件库在我的电脑的文档里(可能不同的电脑会不一致)

把所有的组件拷贝出来,放到你自己的项目里去,然后引用dog_net_windows.dll即可

还有下面这个文件夹里的这个文件的内容,也是非常的重要,涉及到是否成功访问超级狗的关键。

 

然后在项目里新建一个类。把里面的字符串拷贝出来,如下:

 

 

应用篇

情景一:在软件中显示当前的激活状态


虽然一般来说,软件可运行就代表着激活了,但是对于超级狗而言,有很多种方式,可能是永久激活的,可能是限制了次数的,可能是限制了日期的,不管超级狗的特征是什么类型的,我们都希望在软件界面上友好的激活。

例如界面显示当前为永久激活,界面上显示当前激活天数只剩30天之类的

 

        public DogStatus ReadDogActiveString( int feature, string verdorCode, out string msg )
        {
            string scope = "<dogscope />";
            DogStatus status;
            Dog curDog = new Dog( new DogFeature( feature ) );

            status = curDog.Login( verdorCode, scope );
            if (status != DogStatus.StatusOk)
            {
                if (status == DogStatus.InvalidVendorCode)
                {
                    msg = "Invalid vendor code.";
                }
                else if (status == DogStatus.UnknownVcode)
                {
                    msg = "Vendor Code not recognized by API.";
                }
                else
                {
                    msg = "Login to feature failed with status: " + status;
                }
                return status;
            }

            string info = string.Empty;
            DogStatus read = curDog.GetSessionInfo( "<dogformat><feature><attribute name=\"id\"/><element name=\"license\"/></feature></dogformat>", ref info );
            if (read == DogStatus.StatusOk)
            {
                // 读取特征成功
                XElement xElement = XElement.Parse( info ).Element( "feature" ).Element( "license" );
                string type = xElement.Element( "license_type" ).Value;
                if (type == "perpetual")
                {
                    // 永久激活方式
                    msg = "产品激活成功";
                }
                else if (type == "executions")
                {
                    // 执行次数的验证方式
                    msg = "总激活数:" + xElement.Element( "counter_fix" ).Value + " 当前激活次数:" + xElement.Element( "counter_var" ).Value;
                }
                else if (type == "expiration")
                {
                    // 到期时间的激活方式
                    string time_second = xElement.Element( "exp_date" ).Value;
                    DateTime dateTime = new DateTime( 1970, 1, 1 ).AddSeconds( double.Parse( time_second ) );
                    msg = "到期时间:" + dateTime.ToString( "yyyy-MM-dd" );
                }
                else if (type == "trial")
                {
                    // 测试版
                    DateTime dateTime = new DateTime( 1970, 1, 1 ).AddSeconds( double.Parse( xElement.Element( "time_start" ).Value ) );
                    int days = int.Parse( xElement.Element( "total_time" ).Value ) / 3600 / 24;
                    DateTime timeNow = DateTime.Now;
                    curDog.GetTime( ref timeNow );
                    msg = "剩余激活天数:" + (days - (int)(timeNow - dateTime).TotalDays);
                }
                else
                {
                    msg = "激活方式未知";
                }
            }
            else
            {
                msg = "ReadFailed: " + read;
            }

            curDog.Logout( );
            curDog.Dispose( );
            return read;
        }

 此处提供一个通用的方法,读取超级狗的激活状态,并返回msg。使用方式如下:

        private void button3_Click( object sender, EventArgs e )
        {
            if(!int.TryParse(textBox1.Text,out int feature))
            {
                MessageBox.Show( "特征输入不正确" );
            }

            DogStatus status = ReadDogActiveString( feature, VendorCode.strVendorCode, out string msg );
            label2.Text = msg;
        }

 效果如下:

 

 

 

情景二:存储隐私数据,密码或是数据库连接字符串


想必这个应用也是非常广泛的了,如果你要验证用户的密码输入,如果不使用数据库实现,那么就只能直接判断了

            string password = "123456";

            if(password=="123456")
            {
                // 密码正确
            }
            else
            {
                // 密码错误
            }

 但是这样就会非常的危险,对付一般的小白用户可能无所谓,但是只要是知道些反编译技术的人,用反编译工具就可以很快知道这个密码了,然后就可以进去做某些XXXXX的事了。

 

可能有人会觉得把正确的密码存放在数据库好了,然后软件启动的时候加载数据库里的密码,这样不就安全了吗?但是你也公开了一个信息,那就是数据库连接字符串

Server=(local);Database=pubs;Uid=sa;Pwd=asdasd;

 别人就知道了你的数据库的地址,用户名,密码,然后入侵到你的数据库服务器里,这样所有的数据都暴露了。

 

 

那么我们怎么用加密狗解决这个问题呢?加密狗里有数据文件,允许进行读写操作,OK,那么我们来试一下

 

我们新增一个数据文件,然后按照下面进行勾选,我们发现可以创建三种模式的数据文件,读写,只读,写一次,很好理解,这里我们创建个可读写的,长度为128字节。

然后点击确定,回到上个界面,点击编程超级狗,将信息写入到超级狗里。

 

刚才写的是文件ID是5,所以接下来就需要在代码上操作,我们先读取这个数据

        public DogStatus ReadString( int feature, int fileId, string verdorCode, out string msg )
        {
            string scope = "<dogscope />";
            DogStatus status;
            Dog curDog = new Dog( new DogFeature( feature ) );

            status = curDog.Login( VendorCode.strVendorCode, scope );
            if (status != DogStatus.StatusOk)
            {
                if (status == DogStatus.InvalidVendorCode)
                {
                    msg = "Invalid vendor code.\n" ;
                }
                else if (status == DogStatus.UnknownVcode)
                {
                    msg = "Vendor Code not recognized by API.\n" ;
                }
                else
                {
                    msg = "Login to feature failed with status: " + status ;
                }
                return status;
            }

            DateTime time = DateTime.Now;
            DogFile file = curDog.GetFile( fileId );

            int size = 0;
            file.FileSize( ref size );
            byte[] buffer = new byte[size];
            file.Read( buffer, 0, size );

            msg = Encoding.ASCII.GetString( buffer ).Trim( '\0' );


            curDog.Logout( );

            return status;
        }

 调用该方法即可读取指定的数据文件的信息,比如,数据文件5,读取出来就是Sample Data File

private void button2_Click( object sender, EventArgs e )
        {
            ReadString( 1, 5, VendorCode.strVendorCode, out string msg );
            label2.Text = msg;
        }

 写入的通用方法

public DogStatus WriteString( int feature, int fileId, string verdorCode, out string msg ,string content)
        {
            string scope = "<dogscope />";
            DogStatus status;
            Dog curDog = new Dog( new DogFeature( feature ) );

            status = curDog.Login( VendorCode.strVendorCode, scope );
            if (status != DogStatus.StatusOk)
            {
                if (status == DogStatus.InvalidVendorCode)
                {
                    msg = "Invalid vendor code.\n";
                }
                else if (status == DogStatus.UnknownVcode)
                {
                    msg = "Vendor Code not recognized by API.\n";
                }
                else
                {
                    msg = "Login to feature failed with status: " + status;
                }
                return status;
            }

            DateTime time = DateTime.Now;
            DogFile file = curDog.GetFile( fileId );

            byte[] buffer = Encoding.ASCII.GetBytes( content );
            file.Write( buffer, 0, buffer.Length );

            msg = "success";
            curDog.Logout( );

            return status;
        }

 下面就是调用演示的方法

private void button5_Click( object sender, EventArgs e )
        {
            WriteString( 1, 5, VendorCode.strVendorCode, out string msg, "Server=(local);Database=pubs;Uid=sa;Pwd=asdasd;" );
        }

 

我们把数据库的信息写入到超级狗里,然后再读取出来就,相对的提升了安全性。然后再读取这个数据

注意:如果你在写入了"123456",然后再读取字符串信息 "123456=(local);Database=pubs;Uid=sa;Pwd=asdasd;"

写入了时候需要外带清空后面的字符串。

 

posted @ 2018-03-13 17:21  dathlin  阅读(3517)  评论(0编辑  收藏  举报