示例#1
0
        public HttpResponseMessage DoCryptography(InputDataModel model)
        {
            ErrorMsg status        = ErrorMsg.Ok;
            string   resultContent = null;
            string   fileExtention = null;
            Guid     guid          = Guid.NewGuid();

            if (model == null)
            {
                status = ErrorMsg.ModelNotLoaded;
            }
            else //some checks in case js in frontend was abused
            {
                VigenereEncryptor.Operation op = model.ToEncrypt ? VigenereEncryptor.Operation.Encrypt : VigenereEncryptor.Operation.Decrypt;
                if (model.FromFile)
                {
                    if (model.InputFile == null)
                    {
                        status = ErrorMsg.FileLoadingError;
                    }
                    else
                    {
                        string fileName       = System.IO.Path.GetFileName(model.InputFile.FileName);
                        string loadedFilePath = WebApiApplication._LoadedFilesDir + guid + '.' + fileName.Split('.').Last();
                        System.IO.File.WriteAllBytes(loadedFilePath, model.InputFile.Buffer);

                        status = TryEncryptFile(loadedFilePath, model.KeyWord, guid.ToString(), op, out resultContent, out fileExtention);
                        System.IO.File.Delete(loadedFilePath); //we don't need uploaded file anymore
                    }
                }
                else //from text area input
                {
                    status        = TryEncryptRawText(model.InputText, model.KeyWord, guid.ToString(), op, out resultContent);
                    fileExtention = "txt";
                }
            }
            if (status != ErrorMsg.Ok)
            {
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new EncryptionResultModel()
                {
                    IsError = true, Content = status.GetDescription()
                }));
            }
            var response = Request.CreateResponse(HttpStatusCode.OK, new EncryptionResultModel()
            {
                IsError = false, Content = resultContent
            });

            response.Headers.AddCookies(new CookieHeaderValue[] { new CookieHeaderValue(_cookieName, guid.ToString() + '.' + fileExtention) });
            return(response);
        }
示例#2
0
 private static ErrorMsg TryEncryptTxt(string filePath, string keyWord, string guid, VigenereEncryptor.Operation op, out string result)
 {
     result = System.IO.File.ReadAllText(filePath);
     if (result.Contains(Convert.ToChar(0xFFFD)))
     {
         result = System.IO.File.ReadAllText(filePath, Encoding.Default);
     }
     result = VigenereEncryptor.Encrypt(result, keyWord, op);
     if (result == null)
     {
         return(ErrorMsg.InvalidKeyWord);
     }
     System.IO.File.WriteAllText(WebApiApplication._ResultFilesDir + guid + ".txt", result);
     return(ErrorMsg.Ok);
 }
示例#3
0
        private static ErrorMsg TryEncryptRawText(string text, string keyWord, string guid, VigenereEncryptor.Operation op, out string rawResult)
        {
            rawResult = VigenereEncryptor.Encrypt(text, keyWord, op);
            var status = rawResult == null ? ErrorMsg.InvalidKeyWord : ErrorMsg.Ok;

            if (status == ErrorMsg.Ok)
            {
                System.IO.File.WriteAllText(WebApiApplication._ResultFilesDir + guid + ".txt", rawResult);
            }
            return(status);
        }
示例#4
0
 private static ErrorMsg TryEncryptDocx(string filePath, string keyWord, string guid, VigenereEncryptor.Operation op, out string rawResult)
 {
     rawResult = null; //used for preview
     try
     {
         Document          docx = new Document(filePath);
         VigenereEncryptor ve   = new VigenereEncryptor(keyWord, op);
         for (int i = 1; i < docx.Sections[0].Body.Paragraphs.Count; i++)
         {
             string ciph = ve.Encrypt(docx.Sections[0].Body.Paragraphs[i].GetText());
             docx.Sections[0].Body.Paragraphs[i].Runs[0].Text = ciph;
             if (ciph == null)
             {
                 return(ErrorMsg.InvalidKeyWord);
             }
             rawResult += ciph + "\n";
         }
         OoxmlSaveOptions opt = new OoxmlSaveOptions(SaveFormat.Docx);
         opt.Compliance = OoxmlCompliance.Ecma376_2006;
         docx.Save(WebApiApplication._ResultFilesDir + guid + ".docx", opt);
     }
     catch (Exception)
     {
         return(ErrorMsg.InvalidFileContent);
     }
     return(ErrorMsg.Ok);
 }
示例#5
0
        private static ErrorMsg TryEncryptFile(string filePath, string keyWord, string guid, VigenereEncryptor.Operation op, out string result, out string fileExtention)
        {
            result        = null;
            fileExtention = null;
            ErrorMsg status = ErrorMsg.Ok;

            if (VigenereEncryptor.ValidateKeyWord(keyWord))
            {
                fileExtention = filePath.Split('.').Last();
                if (fileExtention == "docx")
                {
                    status = TryEncryptDocx(filePath, keyWord, guid, op, out result);
                }
                else if (fileExtention == "txt")
                {
                    status = TryEncryptTxt(filePath, keyWord, guid, op, out result);
                }
                else
                {
                    status = ErrorMsg.InvalidFileExtention;
                }
            }
            else
            {
                status = ErrorMsg.InvalidKeyWord;
            }
            return(status);
        }