//Try uploading the image for decryption public static IMAGE_UPLOAD_RESULT UploadImage(System.Web.UI.WebControls.FileUpload fileUpload, ref string _name) { IMAGE_UPLOAD_RESULT result = IMAGE_UPLOAD_RESULT.SUCCESS; //Get the file name string filename = Path.GetFileName(fileUpload.FileName); //Generate a new random name (GUID) for the image filename = Guid.NewGuid().ToString() + "_" + filename; //Save the file in the proper path with the proper name fileUpload.SaveAs(FolderPath + filename); //Check is it's not an image if (!isImage(FolderPath + filename)) { result = IMAGE_UPLOAD_RESULT.FAILED; } else { //If it's an image try { //Read the image data to check if it's valid using (System.Drawing.Image myImage = System.Drawing.Image.FromStream(fileUpload.PostedFile.InputStream)) { /* //If it's not a square * if (myImage.Height != myImage.Width) * { * //Display error because image isn't square * result = IMAGE_UPLOAD_RESULT.DIMNOTEQUAL; * }*/ //Check if the image width is greater than the maximum width allowed (for memroy reasons) if (myImage.Width > MAXWIDTH) { //Display error because image is too big result = IMAGE_UPLOAD_RESULT.BIGFILE; } } } catch (Exception exp) { //Display error because uploading has failed result = IMAGE_UPLOAD_RESULT.FAILED; } } //If uploading was a success, set _name to the filename if (result == IMAGE_UPLOAD_RESULT.SUCCESS) { _name = filename; } else { //If it wasn't successful, delete the uploaded file DeleteFile(FolderPath + filename); } //Return the upload result return(result); }
protected void ValidateImage() { string fileName = ""; //Upload the image IMAGE_UPLOAD_RESULT result = Helpers.UploadImage(FileUpload1, ref fileName); //Handle upload errors by displaying a warning message if (result == IMAGE_UPLOAD_RESULT.FAILED) { Helpers.WarningMessage(Panel2, "Error: The selected file is not a proper image file"); } else if (result == IMAGE_UPLOAD_RESULT.BIGFILE) { Helpers.WarningMessage(Panel2, "Error: File size is too big."); } else if (result == IMAGE_UPLOAD_RESULT.DIMNOTEQUAL) { Helpers.WarningMessage(Panel2, "Error: Image dimensions aren't equal."); } else if (result == IMAGE_UPLOAD_RESULT.NOTBMP) { Helpers.WarningMessage(Panel2, "Error: Only BMP files are allowed."); } //If Upload was successful else { //Try decrypting the message string key = KeyBox.Text; string path = Helpers.FolderPath + fileName; string bin = Helpers.ReadBinaryFromImage(path); string msg = Encryptor.FinalDecrypt(bin, key); //Decryption failed if (msg == null) { Helpers.WarningMessage(Panel2, "Error: Invalid file or key"); } //Decryption succeeded else { //Replace panel controls with the result for (int i = Panel2.Controls.Count - 1; i >= 0; i--) { Control c = Panel2.Controls[i]; if (c != TextArea1) { Panel2.Controls.Remove(c); c.Dispose(); } } TextArea1.Attributes.Remove("hidden"); TextArea1.InnerText = msg; TextArea1.Style.Clear(); TextArea1.Style.Add(HtmlTextWriterStyle.Width, "100%"); TextArea1.Style.Add(HtmlTextWriterStyle.Height, "100%"); TextArea1.Style.Add(HtmlTextWriterStyle.Display, "Block"); } } }