示例#1
0
        private void CreateNewFileObject(byte[] DataArray, string fdFileName)
        {
            try
            {
                string     NewgGuid      = Helpers.Helpers.CreateNewGuid;
                byte[]     EncryptedData = default(byte[]);
                string     psw           = Helpers.Helpers.CreateNewGuidWithoutDash();
                FileObject NewObject     = new FileObject()
                {
                    FileGuid           = NewgGuid,
                    FileName           = fdFileName,
                    FileEncryptionDate = DateTime.Now,
                    FileExtension      = Path.GetExtension(fdFileName),
                    IsKeyEncrypted     = false,
                    OriginalFileSize   = DataArray.Length.ToString()
                };

                if (EncryptKeyWithRSA)
                {
                    Tuple <string, string> Keys = _RSA.GenerateKey();
                    psw = _RSA.Encrypt(psw, Keys.Item1);
                    NewObject.IsKeyEncrypted = true;
                    NewObject.PublicRsa      = Keys.Item1;
                    NewObject.PrivateRSA     = Keys.Item2;
                }

                switch (SelectedEncryptionMethod)
                {
                case CryptMethod.AES:
                    NewObject.FileDecryptionPassword = psw;
                    NewObject.FileEncryptionMethod   = SelectedEncryptionMethod;
                    EncryptedData               = _AES.Encrypt(DataArray, NewObject.FileDecryptionPassword);
                    NewObject.FilePath          = SaveFileContentToFile(EncryptedData, $"{NewgGuid}{Path.GetExtension(fdFileName)}");
                    NewObject.EncryptedFileSize = EncryptedData.Length.ToString();
                    break;

                case CryptMethod.DES:
                    NewObject.FileDecryptionPassword = psw;
                    NewObject.FileEncryptionMethod   = SelectedEncryptionMethod;
                    EncryptedData               = _DES.Encrypt(DataArray, NewObject.FileDecryptionPassword);
                    NewObject.FilePath          = SaveFileContentToFile(EncryptedData, $"{NewgGuid}{Path.GetExtension(fdFileName)}");
                    NewObject.EncryptedFileSize = EncryptedData.Length.ToString();
                    break;

                case CryptMethod.TripleDES:
                    NewObject.FileDecryptionPassword = psw;
                    NewObject.FileEncryptionMethod   = SelectedEncryptionMethod;
                    EncryptedData               = _3DES.Encrypt(DataArray, NewObject.FileDecryptionPassword);
                    NewObject.FilePath          = SaveFileContentToFile(EncryptedData, $"{NewgGuid}{Path.GetExtension(fdFileName)}");
                    NewObject.EncryptedFileSize = EncryptedData.Length.ToString();
                    break;

                default:
                    NewObject.FileDecryptionPassword = psw;
                    NewObject.FileEncryptionMethod   = SelectedEncryptionMethod;
                    EncryptedData               = _AES.Encrypt(DataArray, NewObject.FileDecryptionPassword);
                    NewObject.FilePath          = SaveFileContentToFile(EncryptedData, $"{NewgGuid}{Path.GetExtension(fdFileName)}");
                    NewObject.EncryptedFileSize = EncryptedData.Length.ToString();
                    break;
                }

                JsonService.AddFileToJsonList(NewObject);
            }
            catch (Exception ex)
            {
                UserDialogsService.DisplayException(ex);
            }
        }