Пример #1
0
        /// <summary>
        /// Convert a BinFile into a CryptedBinFile.
        /// Only call this once! The iv should be recreated if you want to restart this!
        /// Just create a new object if you want this!
        /// TODO: protobuf-net library does not handle the optional field for some reason, so we must add a restrict value. Value 0 shall be ignored!
        /// </summary>
        /// <param name="binFile">The BinFile that you want to encrypt. Make sure that it's fully filled.</param>
        /// <param name="restrictedToValue">The value that you want to restrict to. 0 shall mean that it's not restricted in our use case.</param>
        public CryptedBinFile convert(BinFile binFile, UInt32 restrictedToValue = 0)
        {
            var binFileStream = new MemoryStream();

            Serializer.Serialize(binFileStream, binFile);

            var binFileBytes = binFileStream.ToArray();

            // For this moment we also just write it out as a bytestream, just to keep things simple.
            //Console.WriteLine("FirmwareFile encoded into: " + Convert.ToBase64String(memoryStream.ToArray()));

            this.fillIV(restrictedToValue);
            var initialiszationVector = new byte[16];

            this.rijndaelManaged.IV.CopyTo(initialiszationVector, 0);

            var cryptedBytes = encryptAndExportStream(binFileBytes);

            // Also try to decrypt it for a moment! We should retreive two blocks, it's now 32 byte big. Some zeros should be added on the end... Then we should now how many zeros we have, or how many bytes...
            initialiszationVector.CopyTo(this.rijndaelManaged.IV, 0);
            var decryptedBytes = decryptByteArray(cryptedBytes);

            CryptedBinFile result = new CryptedBinFile()
            {
                aesInitializationVector = initialiszationVector,
                restrictToValue         = restrictedToValue,
                cryptedBinFile          = cryptedBytes
            };

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Decrypts a CryptedBinFile into a readable BinFile.
        /// </summary>
        /// <param name="cryptedFirmwareFile">The file that you want to decrypt.</param>
        /// <returns>An unencrypted file if all went well. Returns null if the decrypt was not succesfull.</returns>
        public BinFile Decrypt(CryptedBinFile cryptedFirmwareFile)
        {
            this.rijndaelManaged.IV = cryptedFirmwareFile.aesInitializationVector;
            var decrypted = this.decryptByteArray(cryptedFirmwareFile.cryptedBinFile);

            // TODO: Return a seperate object that returns the error that happened.
            return(Serializer.Deserialize <BinFile>(new MemoryStream(decrypted)));
        }