Esempio n. 1
0
        private void ReciveNewFile(Message message)
        {
            var fields = message.StringMessage.Split('$');

            if (fields.Length < 3)
            {
                SendResult(false, "Параметры неверны");
            }
            var transferId     = fields[0];
            var rsaParamKey    = fields[1];
            var newFileKey     = fields[2];
            var transferParams = _dbController.GetTransferParams(transferId);

            if (transferParams == null)
            {
                SendResult(false, "Файл не найден");
                return;
            }
            SHA1 hash             = SHA1.Create();
            var  rsaParamsKeyHash = ByteToStringConverter(hash.ComputeHash(StringToByteConverter(rsaParamKey)));
            var  rsaFilePath      = _dbController.GetPrivateKeyPath(_userId, rsaParamsKeyHash);

            if (rsaFilePath == null)
            {
                SendResult(false, "Неверный ключ обмена");
                return;
            }
            CryptoController crypto = new CryptoController();
            var oldFileKeyBytes     = crypto.DecryptDataWithRsaFile(rsaFilePath, rsaParamKey,
                                                                    StringToByteConverter(transferParams[2]));

            if (oldFileKeyBytes == null)
            {
                SendResult(false, "Ошибка извлечения ключа файла");
                return;
            }
            var oldFileKey = ByteToStringConverter(oldFileKeyBytes);

            crypto.ChangeEncryptKey(transferParams[0], oldFileKey, newFileKey);
            string fileHash;
            string fileLength;

            using (FileStream fs = new FileStream(transferParams[0], FileMode.Open))
            {
                fileHash   = ByteToStringConverter(hash.ComputeHash(fs));
                fileLength = fs.Length.ToString();
            }
            var newFileHash = ByteToStringConverter(hash.ComputeHash(StringToByteConverter(newFileKey)));

            _dbController.SaveNewPrivateFile(transferParams[1], transferParams[0],
                                             fileLength, fileHash, true, newFileHash);
            _dbController.UpdateTransferStatus(transferId);
            SendResult(true);
        }