예제 #1
0
        /// <inheritdoc/>
        public byte[] Decrypt(byte[] packedCipher, SecureString password)
        {
            if (packedCipher == null)
            {
                throw new ArgumentNullException("packedCipher");
            }

            CryptoHeader header;

            byte[] cipher;
            CryptoHeaderPacker.UnpackHeaderAndCipher(packedCipher, PackageName, out header, out cipher);

            ISymmetricEncryptionAlgorithm decryptor = new SymmetricEncryptionAlgorithmFactory().CreateAlgorithm(header.AlgorithmName);
            IKeyDerivationFunction        kdf       = new KeyDerivationFactory().CreateKdf(header.KdfName);

            try
            {
                int    cost    = int.Parse(header.Cost);
                byte[] key     = kdf.DeriveKeyFromPassword(password, decryptor.ExpectedKeySize, header.Salt, cost);
                byte[] message = decryptor.Decrypt(cipher, key, header.Nonce);

                if (string.Equals(CompressionGzip, header.Compression, StringComparison.InvariantCultureIgnoreCase))
                {
                    message = CompressUtils.Decompress(message);
                }

                return(message);
            }
            catch (Exception ex)
            {
                throw new CryptoDecryptionException("Could not decrypt cipher, probably because the key is wrong.", ex);
            }
        }
예제 #2
0
        /// <inheritdoc/>
        public byte[] Decrypt(byte[] packedCipher, byte[] key)
        {
            if (packedCipher == null)
            {
                throw new ArgumentNullException("packedCipher");
            }

            CryptoHeader header;

            byte[] cipher;
            CryptoHeaderPacker.UnpackHeaderAndCipher(packedCipher, PackageName, out header, out cipher);

            ISymmetricEncryptionAlgorithm decryptor = new SymmetricEncryptionAlgorithmFactory().CreateAlgorithm(header.AlgorithmName);

            try
            {
                byte[] truncatedKey = CryptoUtils.TruncateKey(key, decryptor.ExpectedKeySize);
                byte[] message      = decryptor.Decrypt(cipher, truncatedKey, header.Nonce);

                if (string.Equals(CompressionGzip, header.Compression, StringComparison.InvariantCultureIgnoreCase))
                {
                    message = CompressUtils.Decompress(message);
                }

                return(message);
            }
            catch (Exception ex)
            {
                throw new CryptoDecryptionException("Could not decrypt cipher, probably because the key is wrong.", ex);
            }
        }
예제 #3
0
        public static T Load <T>(Stream input, Type[] extraTypes, string password)
        {
            //using (MemoryStream memoryStream = new MemoryStream())
            //{
            //    bool compress = !string.IsNullOrEmpty(password);
            //    if (compress)
            //    {
            //        CompressUtils.Decompress(input, memoryStream, password);
            //        memoryStream.Seek(0, SeekOrigin.Begin);
            //    }
            //    var rawStream = compress ? memoryStream : input;

            //    XmlSerializer serializer = new XmlSerializer(typeof(T), extraTypes);
            //    return (T)serializer.Deserialize(rawStream);
            //}
            bool compress     = !string.IsNullOrEmpty(password);
            var  tempFilePath = Path.GetTempFileName();

            if (compress)
            {
                using (FileStream output = new FileStream(tempFilePath, FileMode.Open))
                {
                    CompressUtils.Decompress(input, output, password);
                    output.Seek(0, SeekOrigin.Begin);
                }
            }
            Stream        rawStream  = compress ? new FileStream(tempFilePath, FileMode.Open) : input;
            XmlSerializer serializer = new XmlSerializer(typeof(T), extraTypes);

            return((T)serializer.Deserialize(rawStream));
        }
예제 #4
0
        public void CompressAndDecompressReturnsOriginalData()
        {
            ICryptoRandomService randomGenerator = CommonMocksAndStubs.CryptoRandomService();
            int byteCount = 1;

            while (byteCount < 1000)
            {
                byte[] data             = randomGenerator.GetRandomBytes(byteCount);
                byte[] compressedData   = CompressUtils.Compress(data);
                byte[] decompressedData = CompressUtils.Decompress(compressedData);

                Assert.AreEqual(data, decompressedData);
                byteCount = byteCount * 2;
            }
        }
예제 #5
0
        public void CompressHandlesNullData()
        {
            // Null data
            byte[] data           = null;
            byte[] compressedData = CompressUtils.Compress(data);
            Assert.IsNull(compressedData);

            // Empty data
            data           = new byte[0];
            compressedData = CompressUtils.Compress(data);
            byte[] decompressedData = CompressUtils.Decompress(compressedData);
            Assert.IsNotNull(compressedData);
            Assert.AreEqual(0, compressedData.Length);
            Assert.IsNotNull(decompressedData);
            Assert.AreEqual(data, decompressedData);
        }
예제 #6
0
        static void PrintHelp()
        {
            PrintVersion();
            System.Console.WriteLine(Encoding.UTF8.GetString(CompressUtils.Decompress(art_console)));
            System.Console.WriteLine($"Copyright (C) 2020. Commnunity Crawler Developer");
            System.Console.WriteLine($"E-Mail: [email protected]");
            System.Console.WriteLine($"Source-code: https://github.com/rollrat/com_crawler");
            System.Console.WriteLine($"");
            System.Console.WriteLine("Usage: ./com_crawler.Console [OPTIONS...] <URL> [URL OPTIONS ...]");

            var builder = new StringBuilder();

            CommandLineParser.GetFields(typeof(Options)).ToList().ForEach(
                x =>
            {
                var key = x.Key;
                if (!key.StartsWith("--"))
                {
                    return;
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.ShortOption))
                {
                    key = $"{x.Value.Item2.ShortOption}, " + key;
                }
                var help = "";
                if (!string.IsNullOrEmpty(x.Value.Item2.Help))
                {
                    help = $"[{x.Value.Item2.Help}]";
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.Info))
                {
                    builder.Append($"   {key}".PadRight(30) + $" {x.Value.Item2.Info} {help}\r\n");
                }
                else
                {
                    builder.Append($"   {key}".PadRight(30) + $" {help}\r\n");
                }
            });
            System.Console.Write(builder.ToString());

            System.Console.WriteLine($"");
            System.Console.WriteLine("Enter './com_crawler.Console --list-extractor' to get more url options.");
        }
예제 #7
0
        static void PrintHelp()
        {
            PrintVersion();
            Console.WriteLine(Encoding.UTF8.GetString(CompressUtils.Decompress(art_console)));
            Console.WriteLine($"Copyright (C) 2020. Inha Univ AlarmBot(Notification Server) Project.");
            Console.WriteLine($"E-Mail: [email protected]");
            Console.WriteLine($"Source-code: https://github.com/rollrat/inha-alarm");
            Console.WriteLine($"Source-code: https://github.com/rollrat/INHANoti");
            Console.WriteLine($"");
            Console.WriteLine("Usage: ./NotiServer [OPTIONS...]");

            var builder = new StringBuilder();

            CommandLineParser.GetFields(typeof(Options)).ToList().ForEach(
                x =>
            {
                var key = x.Key;
                if (!key.StartsWith("--"))
                {
                    return;
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.ShortOption))
                {
                    key = $"{x.Value.Item2.ShortOption}, " + key;
                }
                var help = "";
                if (!string.IsNullOrEmpty(x.Value.Item2.Help))
                {
                    help = $"[{x.Value.Item2.Help}]";
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.Info))
                {
                    builder.Append($"   {key}".PadRight(30) + $" {x.Value.Item2.Info} {help}\r\n");
                }
                else
                {
                    builder.Append($"   {key}".PadRight(30) + $" {help}\r\n");
                }
            });
            Console.Write(builder.ToString());
        }
예제 #8
0
        protected GetRecordsResult DecorateRecords(GetRecordsResult result)
        {
            //解压
            if (_disConfig.IsDataCompressEnabled())
            {
                if (result.Records != null)
                {
                    for (int i = 0; i < result.Records.Count; i++)
                    {
                        byte[] input = result.Records[i].Data;
                        try
                        {
                            byte[] uncompressedInput = CompressUtils.Decompress(input);
                            result.Records[i].Data = uncompressedInput;
                        }
                        catch (IOException e)
                        {
                            logger.Error(e.Message, e);
                            throw new Exception(e.Message);
                        }
                    }
                }
            }

            //解密
            if (IsEncrypt())
            {
                List <Record> records = result.Records;
                if (records != null)
                {
                    foreach (var record in records)
                    {
                        record.Data = Decrypt(record.Data);
                    }
                }
            }

            return(result);
        }
예제 #9
0
        static void PrintHelp()
        {
            PrintVersion();
            Console.WriteLine(Encoding.UTF8.GetString(CompressUtils.Decompress(art_console)));
            Console.WriteLine($"Copyright (C) 2020-2021. project violet-server.");
            Console.WriteLine("Usage: ./hsync [OPTIONS...]");

            var builder = new StringBuilder();

            CommandLineParser.GetFields(typeof(Options)).ToList().ForEach(
                x =>
            {
                var key = x.Key;
                if (!key.StartsWith("--"))
                {
                    return;
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.ShortOption))
                {
                    key = $"{x.Value.Item2.ShortOption}, " + key;
                }
                var help = "";
                if (!string.IsNullOrEmpty(x.Value.Item2.Help))
                {
                    help = $"[{x.Value.Item2.Help}]";
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.Info))
                {
                    builder.Append($"   {key}".PadRight(30) + $" {x.Value.Item2.Info} {help}\r\n");
                }
                else
                {
                    builder.Append($"   {key}".PadRight(30) + $" {help}\r\n");
                }
            });
            Console.Write(builder.ToString());
        }
예제 #10
0
        public static XmlDB Load(string filename, string password)
        {
            XmlDB db           = null;
            var   memoryStream = new MemoryStream();

            try
            {
                if (File.Exists(filename))
                {
                    var types    = new List <Type>();
                    var compress = !string.IsNullOrEmpty(password) && CompressUtils.CheckZip(filename, password);
                    if (compress)
                    {
                        using (var fileStream = new FileStream(filename, FileMode.Open))
                        {
                            CompressUtils.Decompress(fileStream, memoryStream, password);
                        }
                        memoryStream.Seek(0, SeekOrigin.Begin);
                    }

                    using (var xmlStream = compress ? (Stream)memoryStream : new FileStream(filename, FileMode.Open))
                    {
                        using (var reader = XmlReader.Create(xmlStream))
                        {
                            while (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "DBEntity")
                                {
                                    var typeName = reader.GetAttribute("xsi:type");
                                    types.Add(TypeCache.Instance.GetTypeFromCache(typeof(DBEntity), typeName));
                                }
                            }
                        }

                        types.AddRange(ReflectionUtils.FindTypes(typeof(DBEntity).Assembly, typeof(Geometry)));

                        xmlStream.Seek(0, SeekOrigin.Begin);
                        db = XmlSerializeUtils.Load <XmlDB>(xmlStream, types.Distinct().ToArray()
                                                            /*GetExtraTypes(NAMESPACES)*/, string.Empty);
                        db.DecodeUnprited();
                    }
                }
                else
                {
                    db = new XmlDB {
                        FileName = filename, Tables = new List <XmlTable>()
                    };
                }
            }
            catch (Exception ex)//check it later
            {
                db = new XmlDB {
                    FileName = filename, Tables = new List <XmlTable>()
                };
                LogManager.Instance.Error(ex);
            }
            finally
            {
                db.FileName = filename;
                db.BuildIndex();
                memoryStream.Dispose();
            }
            return(db);
        }