Beispiel #1
0
        public bool VerifyContentHash(byte[] computedHash)
        {
            if (this._contentHash == null)
            {
                byte tag = this._binaryReader.ReadByte();
                if (tag != 0x42)
                {
                    throw new InvalidDataException();
                }

                IFileStreamHandler <IDictionary <string, object> > dataHandler =
                    this._handlerFactory.GetHandler <IDictionary <string, object> >(tag);
                IDictionary <string, object> contentDirectory = dataHandler.Handle(this._binaryReader);
                contentDirectory.TryGetValue("type", out var typeValue);
                if ("metadata".Equals(typeValue))
                {
                    contentDirectory.TryGetValue("file_md5", out var md5Value);
                    string md5String = md5Value as string;
                    if (md5String == null)
                    {
                        throw new InvalidDataException();
                    }

                    this._contentHash = md5String;
                }
            }

            byte[] hashExpected = BytesUtils.HexStringToByteArray(this._contentHash);
            return(BytesUtils.ByteArrayCompare(hashExpected, computedHash));
        }
 public FileCommunicationHandler(Communication communication)
 {
     _conversionHandler = new ConversionHandler();
     _fileUtils         = new FileUtils();
     _communication     = communication;
     _fileStreamHandler = new FileStreamHandler(_fileUtils);
 }
Beispiel #3
0
        public FileMeta3 GetFileMeta()
        {
            byte metaTag = this._binaryReader.ReadByte();

            if (metaTag != 0x42)
            {
                throw new InvalidDataException($"File {this._fileItem.Name} fails to extract meta");
            }

            IFileStreamHandler <IDictionary <string, object> > metaHandler =
                this._handlerFactory.GetHandler <IDictionary <string, object> >(metaTag);
            IDictionary <string, object> metaDict = metaHandler.Handle(this._binaryReader);

            if (!(metaDict.ContainsKey("type") && "metadata".Equals(metaDict["type"])))
            {
                throw new InvalidDataException($"File {this._fileItem.Name} fails to extract meta");
            }

            return(FileMeta3.fromDictionary(metaDict));
        }
Beispiel #4
0
        public IEnumerable <byte[]> GetDataBlocks(IDecryptor decryptor)
        {
            byte[] buf = null;
            while (true)
            {
                if (buf != null)
                {
                    //yield return decryptor.DecryptBlock(buf, false);
                    yield return(buf);
                }

                byte tag = this._binaryReader.ReadByte();
                if (tag != 0x42)
                {
                    yield break;
                }

                IFileStreamHandler <IDictionary <string, object> > dataHandler =
                    this._handlerFactory.GetHandler <IDictionary <string, object> >(tag);
                IDictionary <string, object> contentDirectory = dataHandler.Handle(this._binaryReader);
                contentDirectory.TryGetValue("type", out var typeValue);
                string typeValueString = typeValue as string;
                if (!"data".Equals(typeValueString))
                {
                    if ("metadata".Equals(typeValueString))
                    {
                        contentDirectory.TryGetValue("file_md5", out var md5Value);
                        string md5String = md5Value as string;
                        if (md5String != null)
                        {
                            this._contentHash = md5String;
                        }
                    }

                    yield break;
                }

                buf = (byte[])contentDirectory["data"];
            }
        }
Beispiel #5
0
        public IDictionary <string, object> Handle(BinaryReader br)
        {
            IDictionary <string, object> returnDict = new Dictionary <string, object>();
            //read key, 0x10 expected
            Boolean hasNext = true;

            while (hasNext)
            {
                byte keyTag = br.ReadByte();
                switch (keyTag)
                {
                case 0x10:
                    IFileStreamHandler <string> stringHandler = this._factory.GetHandler <string>(keyTag);
                    string key = stringHandler.Handle(br);
                    //read value
                    byte valueTag = br.ReadByte();
                    IFileStreamHandler <object> valueHandler = this._factory.GetHandler <object>(valueTag);
                    if (valueHandler == null)
                    {
                        throw new InvalidDataException();
                    }

                    object value = valueHandler.Handle(br);
                    returnDict.Add(key, value);
                    break;

                case 0x40:
                    hasNext = false;
                    continue;

                default:
                    throw new InvalidDataException();
                }
            }

            return(returnDict);
        }
Beispiel #6
0
        private void ParseFile()
        {
            var    directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var    path      = $"{directory}\\Resources\\crypto\\encrypted_jpg_01.jpg";
            string magic     = "__CLOUDSYNC_ENC__";

            byte[] magicBytes = Encoding.ASCII.GetBytes(magic);

            using FileStream fs   = new FileStream(path, FileMode.Open);
            using BinaryReader br = new BinaryReader(fs);

            byte[] value = br.ReadBytes(magicBytes.Length);
            Console.WriteLine(ByteArrayCompare(magicBytes, value));

            //check "__CLOUDSYNC_ENC__" Md5 hash
            value = br.ReadBytes(32);
            byte[] expectedMagicHash =
                Encoding.ASCII.GetBytes(BytesUtils.ByteArrayToLowerHexString(Md5(magicBytes)));
            Assert.AreEqual(expectedMagicHash, value);

            //
            HandlerFactory     handlerFactory   = new HandlerFactory();
            StringHandler      stringHandler    = new StringHandler();
            IntHandler         intHandler       = new IntHandler();
            ByteSteamHandler   byteSteamHandler = new ByteSteamHandler();
            OrderedDictHandler dictHandler      = new OrderedDictHandler(handlerFactory);

            handlerFactory.AddHandler(stringHandler);
            handlerFactory.AddHandler(dictHandler);
            handlerFactory.AddHandler(intHandler);
            handlerFactory.AddHandler(byteSteamHandler);

            byte metaTag = br.ReadByte();

            if (metaTag != 0x42)
            {
                throw new InvalidDataException();
            }

            IFileStreamHandler <IDictionary <string, object> > metaHandler =
                handlerFactory.GetHandler <IDictionary <string, object> >(metaTag);
            IDictionary <string, object> metaDict = metaHandler.Handle(br);

            if (!(metaDict.ContainsKey("type") && "metadata".Equals(metaDict["type"])))
            {
                throw new InvalidDataException();
            }

            FileMeta3 fileMeta = FileMeta3.fromDictionary(metaDict);

            Console.WriteLine(fileMeta);

            AsymmetricKeyParameter akp = CryptoUtils.readPemPk(this.privateKey);

            byte[] sessionKeyCharArray = CryptoUtils.RsaOaepDeciper(fileMeta.EncKey2, akp);

            string computedSessionKeyHash = CryptoUtils.SaltedMd5(
                fileMeta.SessionKeyHash.Substring(0, 10), sessionKeyCharArray);

            if (!fileMeta.SessionKeyHash.Equals(computedSessionKeyHash))
            {
                throw new InvalidDataException("key is incorrect");
            }

            //decrypt content
            byte[] sessionKey = BytesUtils.HexStringToByteArray(
                Encoding.ASCII.GetString(sessionKeyCharArray));
            Console.Write(sessionKey);
            ParametersWithIV keys =
                CryptoUtils.DeriveAESKeyParameters(sessionKey, null);
            AesCbcCryptor decryptor =
                new AesCbcCryptor(((KeyParameter)keys.Parameters).GetKey(), keys.GetIV());
            List <byte[]> decryptedData = new List <byte[]>();

            byte[] buf    = null;
            byte[] decBuf = null;
            IDictionary <string, object> dataResult = null;

            while (true)
            {
                byte dataTag = br.ReadByte();
                if (dataTag == 0x40)
                {
                    Console.WriteLine("come here");
                }

                if (dataTag != 0x42)
                {
                    decBuf = decryptor.DecryptBlock(buf, true);
                    decryptedData.Add(decBuf);
                    break;
                }

                if (buf != null)
                {
                    decBuf = decryptor.DecryptBlock(buf, false);
                    decryptedData.Add(decBuf);
                }

                IFileStreamHandler <IDictionary <string, object> > dataHandler =
                    handlerFactory.GetHandler <IDictionary <string, object> >(metaTag);
                dataResult = dataHandler.Handle(br);
                object typeValue = null;
                dataResult.TryGetValue("type", out typeValue);
                string typeValueString = typeValue as string;
                if (!"data".Equals(typeValueString))
                {
                    break;
                }

                buf = (byte[])dataResult["data"];
            }

            byte[] decData = CryptoUtils.Concat(decryptedData.ToArray());
            File.WriteAllBytes("z:\\123.jpg.lz4", decData);

            //last directory

            //TODO validate dataResult["type"] == "metadata"
            string fileMd5 = (string)dataResult["file_md5"];

            Console.Write(fileMd5);
        }
Beispiel #7
0
 public void AddHandler(IFileStreamHandler <object> handler)
 {
     this._handlerDict[handler.SupportedTag] = handler;
 }