Example #1
0
        private static String dateTimeFormat = "yyyy-MM-dd/hh:mm:ss"; // constant

        /// <summary>
        /// 从加密文本解密出 License 对象。如果解密失败,返回null。
        /// </summary>
        /// <param name="execPath">license-app的可执行文件路径</param>
        /// <param name="key">键,不能超过32个字节,否则返回null</param>
        /// <param name="cipher_text">加密文本</param>
        public static License decryptFrom(String execPath, byte[] key, byte[] cipher_text)
        {
            StringBuilder keyStr = convertKeyToString(key);

            StringBuilder cipherStr = new StringBuilder("[");

            for (int i = 0; i < cipher_text.Length; i++)
            {
                cipherStr.Append(cipher_text[i]);
                cipherStr.Append(',');
            }
            cipherStr.Remove(cipherStr.Length - 1, 1);
            cipherStr.Append(']');

            String args = String.Format("dec {0} {1}", keyStr, cipherStr);

            int    exitCode  = 0;
            String resultStr = CmdUtils.RunCommand(execPath, args, ref exitCode);

            License result = null;

            if (exitCode == 0)
            {
                String[] fields = resultStr.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);


                if (fields.Length >= 4)
                {
                    UInt32   siteCount      = UInt32.Parse(fields[0]);
                    DateTime expirationTime = DateTime.ParseExact(fields[1], dateTimeFormat, null);
                    result = new License(siteCount, expirationTime, fields[2], fields[3]);
                }
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// 生成给定的<code>License</code>对象的加密文本。如果加密失败,会返回null.
        /// 应当只在key不合规范时失败。
        /// </summary>
        /// <param name="execPath">license-app的可执行文件路径</param>
        /// <param name="key">键,不能超过32个字节</param>
        /// <param name="license">证书对象,不能为null</param>
        /// <returns></returns>
        public static byte[] encryptTo(String execPath, byte[] key, License license)
        {
            StringBuilder keyStr = convertKeyToString(key);

            StringBuilder licenseStr = new StringBuilder();

            licenseStr.Append(license.SiteCount);
            licenseStr.Append(' ');
            licenseStr.Append(license.ExpirationTime.ToString(dateTimeFormat));
            licenseStr.Append(' ');
            licenseStr.Append(license.CompanyName);
            licenseStr.Append(' ');
            licenseStr.Append(license.SoftwareName);

            String args = String.Format("enc {0} {1}", keyStr, licenseStr);

            int    exitCode  = 0;
            String resultStr = CmdUtils.RunCommand(execPath, args, ref exitCode);

            byte[] result = null;

            if (exitCode == 0 && resultStr != "")
            {
                string[] byteStrs = resultStr.Trim(new char[] { '[', ']', '\r', '\n', ' ' }).Split(',');
                //string[] byteStrs = resultStr.Substring(1, resultStr.Length - 2).Split(',');
                result = new byte[byteStrs.Length];
                for (int i = 0; i < byteStrs.Length; i++)
                {
                    result[i] = byte.Parse(byteStrs[i]);
                }
            }

            return(result);
        }