/// <summary> /// Decrypt a encrypted <see cref="Bitmap"/> to a <see cref="string"/> /// </summary> /// <param name="salt">The salt used for the Encryption</param> /// <param name="encryptedBitmap">The <see cref="BmpPwd"/> Encrypted <see cref="Bitmap"/></param> /// <param name="cryptScheme">The Scheme/Interface Used for Decryption</param> /// <param name="drawingScheme">The <see cref="DrawingScheme"/> to use for Drawing the Image</param> /// <returns>The decrypted Text from the Bitmap</returns> public static string Decrypt( string salt, Bitmap encryptedBitmap, ICrypt cryptScheme = null, DrawingScheme drawingScheme = DrawingScheme.Line, ColorScheme colorScheme = ColorScheme.RedMixed) { if (cryptScheme == null) { cryptScheme = new Cipher(); } //Set Width and Y for Image Reading int y = 0, width = 0; SetWidthY(drawingScheme, ref y, ref width, encryptedBitmap.Width); //Get all Colors from the Bitmap Color[] colors = GetPixelsFromBitmap(width, y, drawingScheme, colorScheme, encryptedBitmap); //Fill ASCII Values with Color's R Value (R = G = B) byte[] asciiValues = new byte[width]; for (int i = 0; i < width; i++) { asciiValues[i] = GetAsciiValue(colorScheme, colors[i]); } //Decrypt result string decrypted = Encoding.Unicode.GetString(asciiValues); decrypted = cryptScheme.Decrypt(salt, decrypted); return(decrypted); }
/// <summary> /// Get all Pixels from a <see cref="Bitmap"/> into a <see cref="Color[]"/> /// </summary> /// <param name="width">Width of the Image</param> /// <param name="y">The Y index of the Image to read from</param> /// <param name="drawingScheme">The <see cref="DrawingScheme"/> to read</param> /// <param name="encryptedBitmap">The <see cref="Bitmap"/> to read from</param> /// <returns>The filled <see cref="Color[]"/></returns> private static Color[] GetPixelsFromBitmap(int width, int y, DrawingScheme drawingScheme, ColorScheme colorScheme, Bitmap encryptedBitmap) { //Get all Pixels from Bitmap Color[] colors = new Color[width]; for (int i = 0; i < width; i++) { switch (drawingScheme) { case DrawingScheme.Circular: //Circular has dynamic height -> y = height/2 colors[i] = encryptedBitmap.GetPixel(i, y); break; case DrawingScheme.Square: //Square has dynamic height -> y = height/2 colors[i] = encryptedBitmap.GetPixel(i, y); break; case DrawingScheme.Line: //Line has only 1 Pixel Height -> y = 0 colors[i] = encryptedBitmap.GetPixel(i, 0); break; } } return(colors); }
/// <summary> /// Encrypt Text to a Bitmap with default Cipher Encryption /// </summary> /// <param name="salt">The salt used for the Encryption</param> /// <param name="unencryptedText">The original unencrypted Text</param> /// <param name="cryptScheme">The Scheme/Interface Used for Encryption</param> /// <param name="drawingScheme">The <see cref="DrawingScheme"/> to use for Drawing the Image</param> /// <returns>The Encrypted Bitmap</returns> public static Bitmap Encrypt( string salt, string unencryptedText, ICrypt cryptScheme = null, DrawingScheme drawingScheme = DrawingScheme.Line, ColorScheme colorScheme = ColorScheme.RedMixed) { if (cryptScheme == null) { cryptScheme = new Cipher(); } //Get the encrypted Text string encryptedText = cryptScheme.Encrypt(salt, unencryptedText); //Get all ASCII values byte[] asciiValues = Encoding.Unicode.GetBytes(encryptedText); //Set correct Width and Height values int width = 0, height = 0; SetWidthHeight(drawingScheme, ref height, ref width, asciiValues.Length); //Create Bitmap with correct Sizes Bitmap encryptedBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); //Draw onto the Bitmap DrawCorrectScheme(encryptedBitmap, drawingScheme, colorScheme, asciiValues); return(encryptedBitmap); }
/// <summary> /// Sets width and y values depending on the <see cref="DrawingScheme"/> /// </summary> /// <param name="scheme">The <see cref="DrawingScheme"/> to use</param> /// <param name="y">The y value to be set</param> /// <param name="width">The width value to be set</param> /// <param name="imageWidth">The <see cref="Bitmap"/>'s width</param> private static void SetWidthY(DrawingScheme scheme, ref int y, ref int width, int imageWidth) { //Set width and y values for different DrawingSchemes switch (scheme) { case DrawingScheme.Circular: //Circular has radius of textlength -> width = Bitmap.Width / 2 width = imageWidth / 2; y = (imageWidth / 2) - 1; break; case DrawingScheme.Square: //Square has dynamic height -> width = Bitmap.Width / 2 width = imageWidth / 2; y = (imageWidth / 2) - 1; break; case DrawingScheme.Line: //Line has only height of 1 (Index = 0) width = imageWidth; y = 0; break; default: //Default is Line width = imageWidth; y = 0; break; } }
/// <summary> /// Sets width and height values depending on the <see cref="DrawingScheme"/> /// </summary> /// <param name="scheme">The <see cref="DrawingScheme"/> to use</param> /// <param name="height">The height value to be set</param> /// <param name="width">The width value to be set</param> /// <param name="textLength">The Encrypted Text's length</param> private static void SetWidthHeight(DrawingScheme scheme, ref int height, ref int width, int textLength) { //Set correct Width and Height values for different DrawingSchemes switch (scheme) { case DrawingScheme.Circular: //Circular has radius of bytes -> width & height = textlength * 2 width = textLength * 2; height = textLength * 2; break; case DrawingScheme.Square: //Square has dynamic height -> width & height = textlength * 2 width = textLength * 2; height = textLength * 2; break; case DrawingScheme.Line: //Line has only height of 1 (default values) height = 1; width = textLength; break; default: //Default is Line height = 1; width = textLength; break; } }
/// <summary> /// Encrypt Text to an Image with a custom encryption implementing <see cref="ICrypt" /> /// </summary> /// <param name="key">The key used for the Encryption</param> /// <param name="unencryptedText">The original unencrypted Text</param> /// <param name="cryptScheme">The Scheme/Interface Used for Encryption</param> /// <param name="drawingScheme">The <see cref="DrawingScheme" /> to use for Drawing the Image</param> /// <param name="colorScheme">The <see cref="ColorScheme" /> to use for colorizing the Image</param> /// <returns>The Encrypted <see cref="Image" /></returns> public static Image Encrypt( string key, string unencryptedText, ICrypt cryptScheme, DrawingScheme drawingScheme = DrawingScheme.Line, ColorScheme colorScheme = ColorScheme.RedMixed) { if (cryptScheme == null) { cryptScheme = new Cipher(); } //Get the encrypted Text string encryptedText = cryptScheme.Encrypt(key, unencryptedText); //Get all ASCII values var asciiValues = Convert.FromBase64String(encryptedText); //Set correct Width and Height values Helper.SetWidthHeight(drawingScheme, out int height, out int width, asciiValues.Length); //Create Image with correct Sizes var encryptedImage = new Bitmap(width, height, PixelFormat.Format32bppArgb); //Draw onto the Image Helper.DrawCorrectScheme(encryptedImage, drawingScheme, colorScheme, asciiValues); return(encryptedImage); }
/// <summary> /// Decrypt a encrypted <see cref="Image" /> to a <see cref="string" /> /// </summary> /// <param name="key">The key used for the Encryption</param> /// <param name="encryptedImage">The <see cref="BmpPwd" /> Encrypted <see cref="Image" /></param> /// <param name="cryptScheme">The Scheme/Interface Used for Decryption</param> /// <param name="drawingScheme">The <see cref="DrawingScheme" /> to use for Drawing the Image</param> /// <param name="colorScheme">The <see cref="ColorScheme" /> to use for colorizing the Image</param> /// <returns>The decrypted Text from the Image</returns> public static string Decrypt( string key, Image encryptedImage, ICrypt cryptScheme, DrawingScheme drawingScheme = DrawingScheme.Line, ColorScheme colorScheme = ColorScheme.RedMixed) { if (cryptScheme == null) { cryptScheme = new Cipher(); } //Set Width and Y for Image Reading Helper.SetWidthY(drawingScheme, out int y, out int width, encryptedImage.Width); //Get all Colors from the Image var colors = Helper.GetPixelsFromImage(width, y, drawingScheme, colorScheme, encryptedImage); //Fill ASCII Values with Color's R Value (R = G = B) var asciiValues = new byte[width]; for (int i = 0; i < width; i++) { asciiValues[i] = Helper.GetAsciiValue(colorScheme, colors[i]); } // Decrypt result string base64 = Convert.ToBase64String(asciiValues); return(cryptScheme.Decrypt(key, base64)); }
/// <summary> /// Get all Pixels from a <see cref="Image" /> into a <see cref="Color" />[] /// </summary> /// <param name="width">Width of the Image</param> /// <param name="y">The Y index of the Image to read from</param> /// <param name="drawingScheme">The <see cref="DrawingScheme" /> to read</param> /// <param name="encryptedImage">The <see cref="Image" /> to read from</param> /// <param name="colorScheme">The <see cref="ColorScheme" /> to apply on the Color picking</param> /// <returns>The filled <see cref="Color" />[]</returns> internal static Color[] GetPixelsFromImage(int width, int y, DrawingScheme drawingScheme, ColorScheme colorScheme, Image encryptedImage) { //Get all Pixels from Image var colors = new Color[width]; using (var bitmap = new Bitmap(encryptedImage)) { for (int i = 0; i < width; i++) { switch (drawingScheme) { case DrawingScheme.Circular: //Circular has dynamic height -> y = height/2 colors[i] = bitmap.GetPixel(i, y); break; case DrawingScheme.Square: //Square has dynamic height -> y = height/2 colors[i] = bitmap.GetPixel(i, y); break; case DrawingScheme.Line: //Line has only 1 Pixel Height -> y = 0 colors[i] = bitmap.GetPixel(i, 0); break; } } } return(colors); }
/// <summary> /// Sets width and y values depending on the <see cref="DrawingScheme" /> /// </summary> /// <param name="scheme">The <see cref="DrawingScheme" /> to use</param> /// <param name="y">The y value to be set</param> /// <param name="width">The width value to be set</param> /// <param name="imageWidth">The <see cref="Image" />'s width</param> internal static void SetWidthY(DrawingScheme scheme, out int y, out int width, int imageWidth) { //Set width and y values for different DrawingSchemes switch (scheme) { case DrawingScheme.Circular: //Circular has radius of text length -> width = Image.Width / 2 width = imageWidth / 2; y = imageWidth / 2; break; case DrawingScheme.Square: //Square has dynamic height -> width = Image.Width / 2 width = imageWidth / 2; y = imageWidth / 2 - 1; break; case DrawingScheme.Line: default: //Line has only height of 1 (Index = 0) width = imageWidth; y = 0; break; } }
private void FormBox_Changed(object sender, SelectionChangedEventArgs e) { try { var cbox = sender as ComboBox; _scheme = (DrawingScheme)Enum.Parse(typeof(DrawingScheme), (cbox.SelectedItem as ComboBoxItem).Content as string); } catch { // ignored } }
//Helpers for different Schemes or Configs #region Helpers /// <summary> /// Creates Graphics and draws all the Text (ASCII Values/bytes) onto a Bitmap with the correct <see cref="DrawingScheme"/> /// </summary> /// <param name="encryptedBitmap">The <see cref="Bitmap"/> to draw on</param> /// <param name="drawingScheme">The <see cref="DrawingScheme"/> to use for the drawing Process</param> /// <param name="asciiValues">All the ASCII values of the Text to draw</param> private static void DrawCorrectScheme(Bitmap encryptedBitmap, DrawingScheme drawingScheme, ColorScheme colorScheme, byte[] asciiValues) { //Initialize Graphics using (Graphics gfx = Graphics.FromImage(encryptedBitmap)) { //Position & Diameter of Bitmap int position = 0; int diameter = encryptedBitmap.Width; //For random Green & Blue Random random = new Random(); #region Drawing //Loop through each Pixel foreach (byte b in asciiValues) { //The correct color used for drawing (b * 2 because b's max value is 128) Color color = GetColor(colorScheme, b); //Set Pixel to ASCII Values (change Color.FromArg() values for different colors) using (SolidBrush brush = new SolidBrush(color)) { using (Pen pen = new Pen(brush, 2)) { //Draw different Schemes switch (drawingScheme) { case DrawingScheme.Circular: //Circular has dynamic height -> y = height/2 gfx.FillEllipse(brush, position, position, diameter, diameter); break; case DrawingScheme.Line: //Line has only 1 Pixel Height -> y = 0 gfx.FillRectangle(brush, position, 0, 1, 1); break; case DrawingScheme.Square: //Square has dynamic height -> y = height/2 gfx.FillRectangle(brush, position, position, diameter, diameter); break; } } } position++; diameter -= 2; } #endregion } }
/// <summary> /// Creates Graphics and draws all the Text (ASCII Values/bytes) onto a Image with the correct /// <see cref="DrawingScheme" /> /// </summary> /// <param name="encryptedImage">The <see cref="Image" /> to draw on</param> /// <param name="drawingScheme">The <see cref="DrawingScheme" /> to use for the drawing Process</param> /// <param name="colorScheme">The <see cref="ColorScheme" /> to apply on the Color picking</param> /// <param name="asciiValues">All the ASCII values of the Text to draw</param> internal static void DrawCorrectScheme(Image encryptedImage, DrawingScheme drawingScheme, ColorScheme colorScheme, byte[] asciiValues) { //Initialize Graphics using (var gfx = Graphics.FromImage(encryptedImage)) { using (var brush = new SolidBrush(Color.White)) { //Position & Diameter of Image int position = 0; int diameter = encryptedImage.Width; #region Drawing //Loop through each Pixel foreach (byte b in asciiValues) { //The correct color used for drawing brush.Color = GetColor(colorScheme, b); //Set Pixel to ASCII Values (change Color.FromArg() values for different colors) //Draw different Schemes switch (drawingScheme) { case DrawingScheme.Circular: //Circular has dynamic height -> y = height/2 gfx.FillEllipse(brush, position, position, diameter, diameter); break; case DrawingScheme.Line: //Line has only 1 Pixel Height -> y = 0 gfx.FillRectangle(brush, position, 0, 1, 1); break; case DrawingScheme.Square: //Square has dynamic height -> y = height/2 gfx.FillRectangle(brush, position, position, diameter, diameter); break; } position++; diameter -= 2; } } #endregion } }