using System;
using System.Drawing;
using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Imaging.ColorReduction;
using AForge.Math;
class SteganographyAForge
{
const int bitsPerChannel = 1;
public static void EncryptTextToImage(string inputImagePath, string outputImagePath, string textToEncrypt)
{
Bitmap bitmap = (Bitmap)System.Drawing.Image.FromFile(inputImagePath);
int textLength = textToEncrypt.Length;
int charIndex = 0;
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
Color originalColor = bitmap.GetPixel(x, y);
if (charIndex < textLength)
{
char charToEncode = textToEncrypt[charIndex++];
byte[] colorBytes = new byte[] { originalColor.R, originalColor.G, originalColor.B };
for (int i = 0; i < 3; i++)
{
int bitIndex = (bitsPerChannel * i);
if (bitIndex < 8)
{
colorBytes[i] = (byte)((colorBytes[i] & ~1) | ((charToEncode >> bitIndex) & 1));
}
}
bitmap.SetPixel(x, y, Color.FromArgb(colorBytes[0], colorBytes[1], colorBytes[2]));
}
else
{
break;
}
}
}
bitmap.Save(outputImagePath);
}
public static string DecryptTextFromImage(string inputImagePath, int textLength)
{
Bitmap bitmap = (Bitmap)System.Drawing.Image.FromFile(inputImagePath);
string encryptedText = string.Empty;
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
Color encodedColor = bitmap.GetPixel(x, y);
byte charCode = 0;
for (int i = 0; i < 3; i++)
{
int bitIndex = (bitsPerChannel * i);
if (bitIndex < 8)
{
charCode |= (byte)((encodedColor.ToArgb() >> bitIndex) & 1);
if (bitIndex <= 2)
{
charCode <<= bitsPerChannel;
}
}
}
encryptedText += (char)charCode;
if (encryptedText.Length == textLength)
{
break;
}
}
if (encryptedText.Length == textLength)
{
break;
}
}
return encryptedText;
}
static void Main()
{
string inputImagePath = "C:\\Users\\motor\\OneDrive\\Изображения\\дрейн.jpg";
string outputImagePath = "C:\\Users\\motor\\OneDrive\\Изображения\\newimage.jpg";
string textToEncrypt = "Hello, secret message!";
EncryptTextToImage(inputImagePath, outputImagePath, textToEncrypt);
string decryptedText = DecryptTextFromImage(outputImagePath, textToEncrypt.Length);
Console.WriteLine("Decrypted Text: " + decryptedText);
}
}
Decrypted Text: ♫♫♫♠♠♠♫♠♠
☻☻♀☻♀♦♀☻♫
char charToEncode = textToEncrypt[charIndex++];
byte[] colorBytes = new byte[] { originalColor.R, originalColor.G, originalColor.B };
for (int i = 0; i < 3; i++)
{
int bitIndex = (bitsPerChannel * i);
if (bitIndex < 8)
{
colorBytes[i] = (byte)((colorBytes[i] & ~1) | ((charToEncode >> bitIndex) & 1));
}
}
for (int i = 0; i < 3; i++)
{
int bitIndex = (bitsPerChannel * i);
if (bitIndex < 8)
{
charCode |= (byte)((encodedColor.ToArgb() >> bitIndex) & 1);
if (bitIndex <= 2)
{
charCode <<= bitsPerChannel;
}
}
}