hunterlan

LSB

Jun 4th, 2021
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. using System;
  2. using System.Net.Mime;
  3. using System.Text;
  4. using ImageMagick;
  5.  
  6. namespace Lab2_4
  7. {
  8.     public class LSB
  9.     {
  10.         private readonly ushort ONE_BYTE = 8;
  11.        
  12.         public bool EncodeTextToImage(string text, string path)
  13.         {
  14.             int length = Math.Min(text.Length, 255);
  15.             MagickImage image = null;
  16.             try
  17.             {
  18.                 image = new MagickImage(path);
  19.             }
  20.             catch
  21.             {
  22.                 return false;
  23.             }
  24.            
  25.             for (int i = 0; i < ONE_BYTE; i++)
  26.             {
  27.                 var pixels = image.GetPixels();
  28.                 var color = pixels[i, image.Height - 1].ToColor();
  29.                 byte red = color.R;
  30.  
  31.                 red = Convert.ToByte( (red & 254) | ((length & (1 << i)) > 0 ? 1 : 0) );
  32.                 var newColors = new MagickColor
  33.                 {
  34.                     A = color.A,
  35.                     B = color.B,
  36.                     G = color.G,
  37.                     K = color.K,
  38.                     R = red
  39.                 };
  40.                 pixels.SetPixel(new Pixel(i, image.Height - 1, newColors.ToByteArray()));
  41.             }
  42.  
  43.             int x = ONE_BYTE;
  44.             int y = image.Height - 1;
  45.             for (int i = 0; i < length; i++)
  46.             {
  47.                 int character = text[i];
  48.                 for (int j = 0; j < ONE_BYTE; j++)
  49.                 {
  50.                     if (x >= image.Width)
  51.                     {
  52.                         y--;
  53.                         x = 0;
  54.                     }
  55.                     var pixels = image.GetPixels();
  56.                     var color = pixels[x, y].ToColor();
  57.                     byte red = color.R;
  58.  
  59.                     red = Convert.ToByte( (red & 254) | ((character & (1 << i)) > 0 ? 1 : 0) );
  60.                     var newColors = new MagickColor
  61.                     {
  62.                         A = color.A,
  63.                         B = color.B,
  64.                         G = color.G,
  65.                         K = color.K,
  66.                         R = red
  67.                     };
  68.                     pixels.SetPixel(new Pixel(i, image.Height - 1, newColors.ToByteArray()));
  69.                     x++;
  70.                 }
  71.             }
  72.  
  73.             string[] result_path = path.Split('.');
  74.             result_path[0] = string.Concat(result_path[0], "_resultEncrypting.");
  75.             try
  76.             {
  77.                 image.Write(string.Concat(result_path));
  78.             }
  79.             catch
  80.             {
  81.                 return false;
  82.             }
  83.             return true;
  84.         }
  85.  
  86.         public string DecodeTextFromImage(string path)
  87.         {
  88.             var decodedText = string.Empty;
  89.             int length = 0;
  90.             MagickImage image = null;
  91.             try
  92.             {
  93.                 image = new MagickImage(path);
  94.             }
  95.             catch
  96.             {
  97.                 return null;
  98.             }
  99.  
  100.             for (int i = 0; i < ONE_BYTE; i++)
  101.             {
  102.                 var color = image.GetPixels()[i, image.Height - 1].ToColor();
  103.  
  104.                 length = length | ((color.R & 1) << 1);
  105.             }
  106.            
  107.             int x = ONE_BYTE;
  108.             int y = image.Height - 1;
  109.             for (int i = 0; i < length; i++)
  110.             {
  111.                 int character = 0;
  112.                 for (int j = 0; j < ONE_BYTE; j++)
  113.                 {
  114.                     if (x >= image.Width)
  115.                     {
  116.                         y--;
  117.                         x = 0;
  118.                     }
  119.                     var color = image.GetPixels()[i, image.Height - 1].ToColor();
  120.                     int r = color.R;
  121.  
  122.                     character |= ((r & 1) << j);
  123.                 }
  124.  
  125.                 decodedText += Convert.ToChar(character);
  126.             }
  127.  
  128.             return decodedText;
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment