Example #1
0
 public void ReloadCryptedMethod2()
 {
     if (IsEncrypted == CryptMethods.Method0_Plain || IsEncrypted == CryptMethods.Method2_CBC_CarotDAV)
     {
         _DisplayName = null;
         IsEncrypted  = CryptMethods.Unknown;
     }
     ProcessCryption();
 }
 protected override string InnerApplyAlgorithm(string password)
 {
     return(CryptMethods.MD5HashWithPrivateSalt(password));
 }
Example #3
0
        public async Task <Stream> downloadFile(FileMetadata_Info target, long?from = null, long?to = null, string enckey = null, bool autodecrypt = true, CancellationToken ct = default(CancellationToken))
        {
            string       id        = target.id;
            string       filename  = target.name;
            CryptMethods Encrypted = CryptMethods.Method0_Plain;

            if (enckey != null)
            {
                Encrypted = CryptMethods.Method1_CTR;
            }
            else
            {
                if (filename.StartsWith(Config.CarotDAV_CryptNameHeader))
                {
                    Encrypted = CryptMethods.Method2_CBC_CarotDAV;
                    enckey    = "";
                }
                else if (Regex.IsMatch(filename, ".*?\\.[a-z0-9]{8}\\.enc$"))
                {
                    Encrypted = CryptMethods.Method1_CTR;
                    enckey    = Path.GetFileNameWithoutExtension(filename);
                }
                else if (Regex.IsMatch(filename, "^[\u2800-\u28ff]+$"))
                {
                    enckey = DriveData.DecryptFilename(target);
                    if (enckey != null)
                    {
                        Encrypted = CryptMethods.Method1_CTR;
                    }
                }

                if (enckey == null)
                {
                    Encrypted = CryptMethods.Method0_Plain;
                }
            }
            if (!autodecrypt)
            {
                Encrypted = CryptMethods.Method0_Plain;
            }
            Config.Log.LogOut("\t[downloadFile] " + id);
            string error_str = "";
            var    client    = new HttpClient();

            client.Timeout = TimeSpan.FromDays(1);
            try
            {
                long?fix_from = from, fix_to = to;

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Authkey.access_token);
                if (from != null || to != null)
                {
                    if (Encrypted == CryptMethods.Method2_CBC_CarotDAV)
                    {
                        if (fix_from != null)
                        {
                            // ひとつ前のブロックから要求する
                            fix_from -= CryptCarotDAV.BlockSizeByte;
                            if (fix_from < CryptCarotDAV.BlockSizeByte)
                            {
                                // 先頭ブロックを取得するときはファイルの先頭から
                                fix_from = 0;
                            }
                            else
                            {
                                // ブロックにアライメントを合わせる
                                fix_from -= ((fix_from - 1) % CryptCarotDAV.BlockSizeByte + 1);
                                // 途中のブロックを要求された場合は、ヘッダをスキップ
                                fix_from += CryptCarotDAV.CryptHeaderByte;
                            }
                        }
                        if (fix_to != null)
                        {
                            if (fix_to >= target.OrignalLength)
                            {
                                // 末尾まで読み込むときは、ハッシュチェックのために最後まで読み込む
                                fix_to = null;
                            }
                            else
                            {
                                // オリジナルの位置を、暗号化済みの位置に変更
                                fix_to += CryptCarotDAV.CryptHeaderByte;
                            }
                        }
                        if (fix_from != null || fix_to != null)
                        {
                            client.DefaultRequestHeaders.Range = new RangeHeaderValue(fix_from, fix_to);
                        }
                    }
                    else
                    {
                        client.DefaultRequestHeaders.Range = new RangeHeaderValue(from, to);
                    }
                }
                string url      = Config.contentUrl + "nodes/" + id + "/content?download=false";
                var    response = await client.GetAsync(
                    url,
                    HttpCompletionOption.ResponseHeadersRead,
                    ct).ConfigureAwait(false);

                response.EnsureSuccessStatusCode();
                if (Encrypted == CryptMethods.Method1_CTR)
                {
                    return(new CryptCTR.AES256CTR_CryptStream(new ThrottleDownloadStream(new HashStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), new MD5CryptoServiceProvider()), ct),
                                                              enckey,
                                                              from ?? 0));
                }
                else if (Encrypted == CryptMethods.Method2_CBC_CarotDAV)
                {
                    return(new CryptCarotDAV.CryptCarotDAV_DecryptStream(new ThrottleDownloadStream(new HashStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), new MD5CryptoServiceProvider()), ct),
                                                                         from ?? 0,
                                                                         fix_from ?? 0,
                                                                         target.contentProperties?.size ?? -1
                                                                         ));
                }
                else
                {
                    return(new ThrottleDownloadStream(new HashStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), new MD5CryptoServiceProvider()), ct));
                }
            }
            catch (HttpRequestException ex)
            {
                error_str = ex.Message;
                Config.Log.LogOut("\t[downloadFile] " + error_str);
                throw;
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception ex)
            {
                error_str = ex.ToString();
                Config.Log.LogOut("\t[downloadFile] " + error_str);
                throw;
            }
            throw new SystemException("fileDownload failed. " + error_str);
        }
Example #4
0
        private void ProcessCryption()
        {
            if (IsEncrypted == CryptMethods.Unknown)
            {
                if (info?.name?.StartsWith(Config.CarotDAV_CryptNameHeader) ?? false)
                {
                    IsEncrypted = CryptMethods.Method2_CBC_CarotDAV;
                }
                else if (Regex.IsMatch(info?.name ?? "", ".*?\\.[a-z0-9]{8}\\.enc$"))
                {
                    IsEncrypted  = CryptMethods.Method1_CTR;
                    _DisplayName = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(info.name));
                    return;
                }
                else if (Regex.IsMatch(info?.name ?? "", "^[\u2800-\u28ff]+$"))
                {
                    IsEncrypted = CryptMethods.Method1_CTR;
                    var decodename = DriveData.DecryptFilename(info);
                    if (decodename != null)
                    {
                        IsEncrypted  = CryptMethods.Method1_CTR;
                        _DisplayName = Path.GetFileNameWithoutExtension(decodename);
                        return;
                    }
                    _DisplayName = info?.name;
                    CryptError   = true;
                }
                else
                {
                    IsEncrypted = CryptMethods.Method0_Plain;
                }
            }
            switch (IsEncrypted)
            {
            case CryptMethods.Method0_Plain:
                _DisplayName = info?.name;
                break;

            case CryptMethods.Method1_CTR:
                if (Regex.IsMatch(info?.name ?? "", ".*?\\.[a-z0-9]{8}\\.enc$"))
                {
                    _DisplayName = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(info.name));
                    CryptError   = false;
                }
                else if (Regex.IsMatch(info?.name ?? "", "^[\u2800-\u28ff]+$"))
                {
                    var decodename = DriveData.DecryptFilename(info);
                    if (decodename != null)
                    {
                        _DisplayName = Path.GetFileNameWithoutExtension(decodename);
                        CryptError   = false;
                    }
                    else
                    {
                        _DisplayName = info?.name;
                        CryptError   = true;
                    }
                }
                else
                {
                    IsEncrypted  = CryptMethods.Method0_Plain;
                    _DisplayName = info?.name;
                }
                break;

            case CryptMethods.Method2_CBC_CarotDAV:
            {
                var decodename = CryptCarotDAV.DecryptFilename(info?.name);
                if (decodename != null)
                {
                    _DisplayName = decodename;
                    CryptError   = false;
                }
                else
                {
                    _DisplayName = info?.name;
                    CryptError   = true;
                }
            }
            break;
            }
        }
Example #5
0
        static Config()
        {
            DrivePassword = null;
            var serializer = new DataContractSerializer(typeof(Savedata));

            try
            {
                using (var xmlr = XmlReader.Create(filepath))
                {
                    var data = (Savedata)serializer.ReadObject(xmlr);
                    if (data.LogToFile)
                    {
                        LogToFile = data.LogToFile;
                    }
                    enc_refresh_token = data.refresh_token;
                    if (!string.IsNullOrWhiteSpace(data.SendToHost))
                    {
                        SendToHost = data.SendToHost;
                    }
                    if (data.SendToPort != default(int))
                    {
                        SendToPort = data.SendToPort;
                    }
                    if (data.SendPacketNum != default(int))
                    {
                        SendPacketNum = data.SendPacketNum;
                    }
                    if (data.SendDelay != default(int))
                    {
                        SendDelay = data.SendDelay;
                    }
                    if (data.SendLongOffset != default(int))
                    {
                        SendLongOffset = data.SendLongOffset;
                    }
                    if (data.SendRatebySendCount != default(int))
                    {
                        SendRatebySendCount = data.SendRatebySendCount;
                    }
                    if (data.SendRatebyTOTCount != default(int))
                    {
                        SendRatebyTOTCount = data.SendRatebyTOTCount;
                    }
                    if (data.SendVK != default(System.Windows.Forms.Keys))
                    {
                        SendVK = data.SendVK;
                    }
                    if (!string.IsNullOrWhiteSpace(data.SendVK_Application))
                    {
                        SendVK_Application = data.SendVK_Application;
                    }
                    if (data.UploadBandwidthLimit != default(double))
                    {
                        UploadLimit = data.UploadBandwidthLimit;
                    }
                    if (data.DownloadBandwidthLimit != default(double))
                    {
                        DownloadLimit = data.DownloadBandwidthLimit;
                    }
                    if (data.FFmoduleKeybinds != null)
                    {
                        if (data.FFmoduleKeybinds.Count() >= FFmoduleKeybinds.Count())
                        {
                            FFmoduleKeybinds = data.FFmoduleKeybinds;
                        }
                        else
                        {
                            data.FFmoduleKeybinds.ToList().ForEach(x => FFmoduleKeybinds[x.Key] = x.Value);
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(data.FontFilepath))
                    {
                        FontFilepath = data.FontFilepath;
                    }
                    if (data.FontPtSize != default(int))
                    {
                        FontPtSize = data.FontPtSize;
                    }
                    if (data.FFmodule_TransferLimit != default(double))
                    {
                        FFmodule_TransferLimit = data.FFmodule_TransferLimit;
                    }
                    if (data.FFmodule_AutoResize != true)
                    {
                        FFmodule_AutoResize = data.FFmodule_AutoResize;
                    }
                    enc_drive_password       = data.DrivePassword;
                    enc_Check_drive_password = data.DrivePasswordCheck;
                    if (data.LockPassword != default(bool))
                    {
                        LockPassword = data.LockPassword;
                    }
                    if (data.UseEncryption != default(bool))
                    {
                        UseEncryption = data.UseEncryption;
                    }
                    if (data.UseFilenameEncryption != default(bool))
                    {
                        UseFilenameEncryption = data.UseFilenameEncryption;
                    }
                    if (data.Language != default(string))
                    {
                        Language = data.Language;
                    }
                    if (data.CryptMethod != default(CryptMethods))
                    {
                        CryptMethod = data.CryptMethod;
                    }
                    if (data.AutoDecode != true)
                    {
                        AutoDecode = data.AutoDecode;
                    }
                    if (data.CarotDAV_CryptNameHeader != default(string))
                    {
                        CarotDAV_CryptNameHeader = data.CarotDAV_CryptNameHeader;
                    }
                    if (data.UploadBufferSize != default(int))
                    {
                        UploadBufferSize = data.UploadBufferSize;
                    }
                    if (data.DownloadBufferSize != default(int))
                    {
                        DownloadBufferSize = data.DownloadBufferSize;
                    }
                    if (data.UploadTrick1 != default(bool))
                    {
                        UploadTrick1 = data.UploadTrick1;
                    }
                    if (data.ParallelDownload != default(int))
                    {
                        ParallelDownload = data.ParallelDownload;
                    }
                    if (data.ParallelUpload != default(int))
                    {
                        ParallelUpload = data.ParallelUpload;
                    }
                    if (data.FFmodule_fullscreen != default(bool))
                    {
                        FFmodule_display = data.FFmodule_display;
                    }
                    if (data.FFmodule_volume != default(double))
                    {
                        FFmodule_volume = data.FFmodule_volume;
                    }
                    if (data.FFmodule_mute != default(bool))
                    {
                        FFmodule_mute = data.FFmodule_mute;
                    }
                    if (data.FFmodule_Size != null)
                    {
                        FFmodule_width = data.FFmodule_Size.Value.Width;
                        FFmodule_hight = data.FFmodule_Size.Value.Height;
                    }
                    if (data.FFmodule_Location != null)
                    {
                        FFmodule_x = data.FFmodule_Location.Value.X;
                        FFmodule_y = data.FFmodule_Location.Value.Y;
                    }
                    if (data.Main_Size != null)
                    {
                        Main_Size = data.Main_Size;
                    }
                    if (data.Main_Location != null)
                    {
                        Main_Location = data.Main_Location;
                    }
                    contentUrl  = data.contentUrl;
                    metadataUrl = data.metadataUrl;
                    if (data.URL_time < DateTime.Now)
                    {
                        URL_time = data.URL_time;
                    }
                }
            }
            catch (Exception)
            {
                Save();
            }
        }