Beispiel #1
0
        public static void TestArchive(string filename, byte[] key = null)
        {
            // Prepare our encryption object, if we have a key.
            BinFileCrypter binFileCrypter = null;

            if (key != null)
            {
                binFileCrypter = new BinFileCrypter(key);
            }

            // Start reading the file!
            using (var fileStream = File.OpenRead(filename))
            {
                Console.WriteLine("Loading and parsing {0} (size={1})", filename, fileStream.Length);
                var binArchive = Serializer.Deserialize <BinArchive>(fileStream);
                Console.WriteLine("Completed bin-archive parse, contains {0} normal files and {1} crypted files.", binArchive.binFiles.Count, binArchive.cryptedBinFiles.Count);
                Console.WriteLine("Version={0}", binArchive.version);
                Console.WriteLine("Time={0}", Helpers.convertUnixTimeMillisecondsToDateTime(binArchive.time).ToString());
                Console.WriteLine("");

                foreach (var binFile in binArchive.binFiles)
                {
                    PrintBinFile(binFile);
                }

                foreach (var cryptedBinFile in binArchive.cryptedBinFiles)
                {
                    Console.WriteLine("Found crypted file (restrictToValue={0}, IV={1}, size={2}), trying to decrypt...",
                                      cryptedBinFile.restrictToValue,
                                      Convert.ToBase64String(cryptedBinFile.aesInitializationVector),
                                      cryptedBinFile.cryptedBinFile.Length);

                    if (binFileCrypter != null)
                    {
                        var binFile = binFileCrypter.Decrypt(cryptedBinFile);
                        if (binFile != null)
                        {
                            PrintBinFile(binFile);
                        }
                        else
                        {
                            Console.WriteLine("Failed to decrypt.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Decrypt failed: No valid -k supplied, ");
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Appends the file with the properties that you configured on this object.
        /// It will be encrypted if you have set the Key propertie.
        /// </summary>
        /// <returns>True when all went ok, false when something failed for some reason.</returns>
        public bool Append()
        {
            if (this.binArchive != null)
            {
                var binFile = new BinFile()
                {
                    version = this.Version,
                    content = File.ReadAllBytes(this.Filename)   // TODO: Will this always fit in memory? For now we just use very simple and small files.
                };

                // Unfortunate we can't everything into the object initialiser:
                binFile.typeValues.AddRange(this.TypeValues);
                binFile.size = (uint)binFile.content.Length;

                // Calculate the checksum in a proper way for our file data. We now that we should place 20 bytes into our test file!
                binFile.check = 3141592653;
                binFile.crc   = calculateCrc32(binFile.content);
                binFile.sha1  = calculateSha1(binFile.content);    // Should always be 20 bytes...

                binFile.time = Helpers.convertToUnixTimeMilliseconds(File.GetCreationTimeUtc(this.Filename));

                // Now add the binFile!
                if (this.Key == null)
                {
                    this.binArchive.binFiles.Add(binFile);
                }
                else
                {
                    // Encrypt it when a key was given!
                    var binFileCrypter = new BinFileCrypter(this.Key);
                    var cryptedBinFile = binFileCrypter.convert(binFile, this.RestrictToValue);
                    this.binArchive.cryptedBinFiles.Add(cryptedBinFile);
                }

                // Also increase our append call, so that we know that we need to finish it really at the end.
                this.appendCall++;
            }
            else
            {
                throw new InvalidOperationException("Trying to call Append() without calling Prepare() first!");
            }

            return(true);
        }