软件加license的一种实现方法

以前从没干过破解的勾当,这次确实必须要去破解一个,于是下了个反编译工具。 最终拿到反编译出来的文件,欣赏了一把它的license检测代码。原谅我的无知,以下代码在我看来还是比较新鲜,犬神请不要鄙视:

internal static void CheckLicense()
        {
            if (!License.licenseChecked)
            {
                try
                {
                    using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly())
                    {
                        using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("xxx.lic", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, isolatedStorageFile))
                        {
                            long storageLength = isolatedStorageFileStream.Length;
                            if (storageLength == (long)0)
                            {
                                Encoding uTF8 = Encoding.UTF8;
                                DateTime dateTime = DateTime.Now.AddDays(30);
                                byte[] bytes = uTF8.GetBytes(dateTime.Ticks.ToString());
                                isolatedStorageFileStream.Write(bytes, 0, (int)bytes.Length);
                            }
                            else
                            {
                                byte[] bytes = new byte[checked(storageLength)];
                                int bytesRead = isolatedStorageFileStream.Read(bytes, 0, (int)bytes.Length);
                                DateTime expiryDateTime = new DateTime(long.Parse(Encoding.UTF8.GetString(bytes, 0, bytesRead)));
                                if (DateTime.Now > expiryDateTime)
                                {
                                    License.licenseExpired = true;
                                }
                            }
                        }
                    }
                    License.licenseChecked = true;
                }
                catch (Exception exception)
                {
                }
            }
            if (License.licenseExpired)
            {
                throw new InvalidOperationException("The trial period has expired. Please contact us at xxx.com for further information.");
            }
        }
CheckLicense()方法可以放在代码中任何地方使用。
基本原理就是在第一次运行时,检测是否生成了license,并把过期日期算好写进去,以后每次检测是否到期。
关键类是
IsolatedStorageFile 。它创建的文件在文件系统中,不需要特殊路径,一般权限的托管代码是不能访问其他dll文件的IsolatedStorageFile 文件的,高级权限的托管代码可以访问其他dll的文件,非托管代码可以访问任何IsolatedStorageFile 文件。


原文地址:https://www.cnblogs.com/wusong/p/3339178.html