コード例 #1
0
 public static string Generate(string fileName, EncryptAction encryptAction)
 {
     switch (encryptAction)
     {
         case EncryptAction.Encrypt:
             string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
             return fileName.Replace(fileNameWithoutExtension, fileNameWithoutExtension + "_encrypted");
         case EncryptAction.Decrypt:
             return fileName.Replace("_encrypted", "");
         default:
             throw new ArgumentOutOfRangeException("encryptAction");
     }
 }
コード例 #2
0
ファイル: Reducers.cs プロジェクト: duyanhv/file-encryption
        public static List <DocumentModel> EncryptDocumentReducer(List <DocumentModel> previousState, EncryptAction action)
        {
            var isUserLoggedIn   = App.Store.GetState().UserProfile != null;
            var documentService  = new DocumentService(App.Store.GetState().DocumentConnectionString);
            var userService      = new UserService(App.Store.GetState().UserConnectionString);
            var newListDocuments = previousState;

            foreach (var document in action.createDocumentParams)
            {
                if (!File.Exists(document.Path) || !CanReadFile.Check(document.Path))
                {
                    continue;
                }
                var rc4 = new RC4(document.Password.Trim(), File.ReadAllBytes(document.Path));
                var encryptedFileRaw  = rc4.Encrypt();
                var newDocumentName   = string.Join("_", Path.GetFileNameWithoutExtension(document.Path), string.Format("{0:yyyy-MM-dd_hh-mm-ss-fff}", DateTime.Now));
                var encryptedFilePath = Path.Combine(App.Store.GetState().DocumentFolder, newDocumentName);
                File.WriteAllBytes(encryptedFilePath, encryptedFileRaw);
                var documentId       = document.Id == Guid.Empty ? Guid.NewGuid() : document.Id;
                var encyptedDocument = new DocumentModel
                {
                    Id          = documentId,
                    Path        = encryptedFilePath,
                    IsEncrypted = true,
                    Name        = Path.GetFileName(newDocumentName),
                    Password    = document.Password.Trim(),
                    FileExt     = Path.GetExtension(document.Path)
                };
                if (isUserLoggedIn)
                {
                    documentService.CreateOrUpdate(encyptedDocument);
                    userService.UpdateUserDocument(App.Store.GetState().UserProfile.Id, documentId);
                }
                else
                {
                    newListDocuments.Add(encyptedDocument);
                }
            }
            return(newListDocuments);
        }