コード例 #1
0
        private static void InitBeans()
        {
            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);
            CloudSyncKey      cloudSyncKey     = new CloudSyncKey(KeyFilePath);
            IExceptionHandler exceptionHandler = new ConsoleExceptionHandler();

            _facade = new CloudSyncFileProcessorFacade(handlerFactory, cloudSyncKey, exceptionHandler);
        }
コード例 #2
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);
        }