Beispiel #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);
        }