BlackHawke

Encrypt text

Feb 6th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. public static Image EncryptText(Bitmap img, string text)
  2. {
  3. // Size of image :
  4. int height = img.Height;
  5. int width = img.Width;
  6.  
  7. // Length of string :
  8. int len = text.Length;
  9.  
  10. // Image Response :
  11. Bitmap response = img;
  12.  
  13. // Check if there is the place to incode text
  14. bool validArguments = (height * width >= len * 1.5 );
  15. if (validArguments)
  16. {
  17. Console.WriteLine("Start encrypt text ...");
  18. // Create a liste of 4bit Code of string
  19. int lenBuffer = 2 * len + 2;
  20. int[] buffer = new int[lenBuffer];
  21.  
  22. for (int i = 0; i < len ; i++)
  23. {
  24. buffer[2*i] = text[i] / 16;
  25. buffer[2*i + 1] = text[i] % 16;
  26. }
  27.  
  28. // Encode the text in the image
  29. int itt = 0;
  30. int selectColor = 0;
  31.  
  32. while (itt < lenBuffer)
  33. {
  34. Color oldPixel = img.GetPixel(itt % width, itt / width);
  35. int r = oldPixel.R;
  36. int g = oldPixel.G;
  37. int b = oldPixel.B;
  38.  
  39. if(selectColor == 0)
  40. {
  41. r = ((oldPixel.R / 16) * 16) + buffer[itt];
  42. }
  43. else if (selectColor == 1)
  44. {
  45. g = ((oldPixel.G / 16) * 16) + buffer[itt];
  46. }
  47. else if (selectColor == 2)
  48. {
  49. b = ((oldPixel.B / 16) * 16) + buffer[itt];
  50. }
  51.  
  52. Color newPixel = Color.FromArgb(oldPixel.A, r, g, b);
  53. response.SetPixel(itt % width, itt / width, newPixel);
  54.  
  55. selectColor++;
  56. if(selectColor%3 == 0)
  57. {
  58. selectColor = 0;
  59. itt++;
  60. }
  61. }
  62.  
  63.  
  64. }
  65. Console.WriteLine("Done Encrypt text");
  66. return response;
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment