private void btnDecodeStart_Click(object sender, EventArgs e) { var isPNG = ImageWriter.HeaderMode.IsPNG(tbDecodeInput.Text); var decodePixels = cbDecodePixels.Checked; var decodeHeaders = isPNG && cbDecodeHeaders.Checked; //If at least one file was skipped var skipped = false; //If at least one file was decoded var decoded = false; if (!decodeHeaders && !decodePixels) { Warn("Please select at least one decoding method"); return; } if (decodeHeaders) { if (!isPNG) { Info("Header decoding is only supported for PNG images. This mode will be skipped."); } else { PNGHeader[] Headers = null; try { Headers = ImageWriter.HeaderMode .ReadPNG(tbDecodeInput.Text) .Where(m => m.IsDataHeader) .ToArray(); } catch (Exception ex) { Err("Failed to read the image as PNG. Reason: " + ex.Message, ex.GetType().Name); return; } foreach (var Header in Headers) { var Encrypted = false; var Data = Header.FileData; using (var MS = new MemoryStream(Data, false)) { Encrypted = crypt.Crypt.IsEncrypted(MS); } if (Encrypted) { using (var PW = new frmConfirmPassword(null)) { if (PW.ShowDialog() == DialogResult.OK) { Data = Tools.DecryptData(PW.PW, Data); if (Data == null) { Warn("Unable to decrypt " + Header.FileName + "\r\nWrong password?"); } } else { Data = null; } } } if (Data != null) { var FN = Path.Combine(Path.GetDirectoryName(tbDecodeInput.Text), Header.FileName); FN = Tools.BrowseFile("Save As...", Preselected: FN, IsSave: true); if (!string.IsNullOrEmpty(FN)) { File.WriteAllBytes(FN, Data); decoded = true; } else { skipped = true; } } else { skipped = true; } } } } if (decodePixels) { FileStream FS; try { FS = File.OpenRead(tbDecodeInput.Text); } catch (Exception ex) { Err("Unable to open source file for pixel data decoding. Reason: " + ex.Message, ex.GetType().Name); return; } using (FS) { var IF = ImageWriter.PixelMode.CreateFileFromImage(FS); if (!IF.IsEmpty) { var Encrypted = false; var Data = IF.Data; using (var MS = new MemoryStream(Data, false)) { Encrypted = crypt.Crypt.IsEncrypted(MS); } if (Encrypted) { using (var PW = new frmConfirmPassword(null)) { if (PW.ShowDialog() == DialogResult.OK) { Data = Tools.DecryptData(PW.PW, Data); if (Data == null) { Warn("Unable to decrypt " + IF.FileName + "\r\nWrong password?"); } } else { Data = null; } } } if (Data != null) { var FN = Path.Combine(Path.GetDirectoryName(tbDecodeInput.Text), IF.FileName); FN = Tools.BrowseFile("Save As...", Preselected: FN, IsSave: true); if (!string.IsNullOrEmpty(FN)) { File.WriteAllBytes(FN, Data); decoded = true; } else { skipped = true; } } else { skipped = true; } } else { Info("Pixel data does not contain an encoded file"); } } } if (decoded) { if (skipped) { Warn("Image decoding complete, but at least one file was skipped"); } else { Info("Image decoding complete"); } } else { Warn("No files were decoded from the image"); } }
private void btnEncodeStart_Click(object sender, EventArgs e) { var mode = (EncodeType)cbEncodeType.SelectedItem; var isHeaderMode = mode.Mode.HasFlag(OperationMode.UseHeader); var isPngOutput = tbEncodeOutput.Text.ToLower().EndsWith(".png"); var encrypt = cbEncrypt.Checked; if (!File.Exists(tbEncodeInput.Text)) { Warn("Please select a file to encode", "Input file"); btnEncodeBrowseInput.Focus(); return; } if (encrypt) { if (string.IsNullOrEmpty(tbEncodePassword.Text)) { Warn("Please enter a password or disable encryption", "Missing password"); tbEncodePassword.Focus(); return; } using (var pw = new frmConfirmPassword(tbEncodePassword.Text)) { if (pw.ShowDialog() != DialogResult.OK) { tbEncodePassword.Focus(); return; } } } if (isHeaderMode) { if (!isPngOutput) { Warn("Header mode only works with png output files", "Output image"); btnEncodeBrowseOutput.Focus(); return; } if (!ImageWriter.HeaderMode.IsPNG(tbEncodeSource.Text)) { Warn("The selected source image doesn't appears to be a png", "Source image"); btnEncodeBrowseSource.Focus(); return; } if (!File.Exists(tbEncodeSource.Text)) { Warn("Header mode requires you to select an existing png image", "Source image"); btnEncodeBrowseSource.Focus(); return; } try { byte[] Data; if (encrypt) { using (var IS = File.OpenRead(tbEncodeSource.Text)) { var Encrypted = Tools.EncryptData(tbEncodePassword.Text, File.ReadAllBytes(tbEncodeInput.Text)); using (var MS = new MemoryStream(Encrypted, false)) { Data = ImageWriter.HeaderMode.CreateImageFromFile( MS, Path.GetFileName(tbEncodeInput.Text), IS); } } } else { Data = ImageWriter.HeaderMode.CreateImageFromFile(tbEncodeInput.Text, tbEncodeSource.Text); } if (Data == null) { throw new Exception("Encoding image data failed"); } File.WriteAllBytes(tbEncodeOutput.Text, Data); } catch (Exception ex) { Err("Unable to encode data as image.\r\nReason: " + ex.Message, ex.GetType().Name); return; } } else { try { byte[] Data; if (encrypt) { var Encrypted = Tools.EncryptData(tbEncodePassword.Text, File.ReadAllBytes(tbEncodeInput.Text)); using (var MS = new MemoryStream(Encrypted, false)) { Data = ImageWriter.PixelMode.CreateImageFromFile( MS, Path.GetFileName(tbEncodeInput.Text), isPngOutput); } } else { Data = ImageWriter.PixelMode.CreateImageFromFile(tbEncodeInput.Text, isPngOutput, cbReadable.Checked); } if (Data == null) { throw new Exception("Encoding image data failed"); } File.WriteAllBytes(tbEncodeOutput.Text, Data); } catch (Exception ex) { Err("Unable to encode data as image.\r\nReason: " + ex.Message, ex.GetType().Name); return; } } Info("Your file has been encoded into an image", "File encoded"); }