示例#1
0
        /// <summary>
        /// 日志解密
        /// </summary>
        /// <param name="sourceFilePath">加密后的日志文件</param>
        /// <param name="targetFilePath">解密后的日志文件</param>
        /// <param name="key">//密钥(长度必须8位以上)</param>
        public static void DecryptFile(string sourceFilePath, string targetFilePath, string key)
        {
            using (var streamReader = File.OpenText(sourceFilePath))
            {
                if (File.Exists(targetFilePath))
                {
                    File.SetAttributes(targetFilePath, FileAttributes.Normal);
                    File.Delete(targetFilePath);
                }

                using (var streamWriter = File.CreateText(targetFilePath))
                {
                    while (!streamReader.EndOfStream)
                    {
                        var    line = streamReader.ReadLine();
                        string decryptString;
                        //将加密串首尾加上"VvvvV"以区分加密串和非加密串混合出现的情况
                        if ((line.StartsWith("VvvvV") && line.EndsWith("VvvvV")))
                        {
                            decryptString = DesEncryptHelper.DecryptString(line.Trim("VvvvV".ToCharArray()), key);
                        }
                        else
                        {
                            decryptString = line + "\r\n";
                        }

                        streamWriter.Write(decryptString);
                    }

                    streamWriter.Flush();
                    //streamWriter.Close();
                }
            }
        }
示例#2
0
        private static string GetConnectionString(Operation operation, string connectionString, string key, string passwordPart, string separator)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentNullException("connectionString");
            }

            var passwordUnit = connectionString
                               .Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                               .FirstOrDefault(
                t => t.ToUpper().StartsWith(passwordPart.ToUpper())
                );

            if (string.IsNullOrEmpty(passwordUnit))
            {
                return(connectionString);
            }

            var passwordParts = passwordUnit.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);

            if (passwordParts.Length < 2)
            {
                return(connectionString);
            }

            switch (operation)
            {
            case Operation.Decrypt:
                passwordParts[1] = DesEncryptHelper.DecryptString(passwordParts[1], key);
                break;

            case Operation.Encrypt:
                passwordParts[1] = DesEncryptHelper.EncryptString(passwordParts[1], key);
                break;
            }

            return(connectionString.Replace(passwordUnit, string.Join("=", passwordParts)));
        }