Answer the question
In order to leave comments, you need to log in
Why is it decoded incorrectly?
When decrypting, the program gives the wrong text
There is a program for encrypting the text in the image,
when decrypting, it gives the wrong text
Tell me please, what could be the reason for this?
PS code is not mine
Project itself archive
file open button
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Filter = "Image Files (*.png, *.jpg) | *.png; *.jpg";
openDialog.InitialDirectory = @"C:\";
if (openDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openDialog.FileName.ToString();
pictureBox1.ImageLocation = textBox1.Text;
}
}
private void button3_Click(object sender, EventArgs e)
{
Bitmap img = new Bitmap(textBox1.Text);
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
Color pixel = img.GetPixel(i, j);
if (i < 1 && j < textBox2.TextLength)
{
Console.WriteLine("R = [" + i + "][" + j + "] = " + pixel.R);
Console.WriteLine("G = [" + i + "][" + j + "] = " + pixel.G);
Console.WriteLine("G = [" + i + "][" + j + "] = " + pixel.B);
char letter = Convert.ToChar(textBox2.Text.Substring(j, 1));
int value = Convert.ToInt32(letter);
Console.WriteLine("letter : " + letter + " value : " + value);
img.SetPixel(i, j, Color.FromArgb(pixel.R, pixel.G, value));
}
if (i == img.Width - 1 && j == img.Height - 1)
{
img.SetPixel(i, j, Color.FromArgb(pixel.R, pixel.G, textBox2.TextLength));
}
}
}
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "Image Files (*.png, *.jpg) | *.png; *.jpg";
saveFile.InitialDirectory = @"C:\";
if (saveFile.ShowDialog() == DialogResult.OK)
{
textBox1.Text = saveFile.FileName.ToString();
pictureBox1.ImageLocation = textBox1.Text;
img.Save(textBox1.Text);
}
}
private void button2_Click(object sender, EventArgs e)
{
Bitmap img = new Bitmap(textBox1.Text);
string message = "";
Color lastpixel = img.GetPixel(img.Width - 1, img.Height - 1);
int msgLength = lastpixel.B;
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
Color pixel = img.GetPixel(i, j);
if (i < 1 && j < msgLength)
{
int value = pixel.B;
char c = Convert.ToChar(value);
string letter = System.Text.Encoding.ASCII.GetString(new byte[] { Convert.ToByte(c) });
message = message + letter;
}
}
}
textBox2.Text = message;
}
Answer the question
In order to leave comments, you need to log in
Strange crap, why only a blue byte is used for encryption, why choose an image when encrypting if it should be created, and even the length of the array written in the blue color of the last pixel is limited to 255 characters. Krch rewrote everything.
Now the length of the array is written in the first 2 pixels, more precisely rgb of the first and r of the second. Krch is written to the whole int (4 bytes) and it is limited to 2147483647 bytes, not 255 as before.
Well, the image is now generated accordingly, the image size itself is selected.
private void button3_Click(object sender, EventArgs e) {//Шифруем текст в файл
string textDecode = textBox2.Text;
byte[] bytes = Encoding.UTF8.GetBytes(textDecode);
int wh = (int) Math.Ceiling(Math.Sqrt(Math.Ceiling((double) ((bytes.Length + 4)/3))));//Определяем размер изображения для шифрования, + 4 (4 байта для резервируем в начале для записи размера массива bytes), делим на три потому что RGB, тоесть в 1 пиксель может 3 байта всунуть
Bitmap img = new Bitmap(wh,wh);
byte[] length = BitConverter.GetBytes(bytes.Length);
//пишем длинну массива
img.SetPixel(0,0,Color.FromArgb(length[0], length[1], length[2]));
img.SetPixel(1,0,Color.FromArgb(length[3], 0,0));
//пишем шифрованный текст
int cur = 0;
for (int y = 0; y < img.Height; y++) {
for (int x = 0; x < img.Width && cur < bytes.Length; x++) {
if (y == 0 && x == 0) {
x = 1;
continue;
}
byte[] pixels = new byte[]{0,0,0};
for (int k = 0; k < 3 && cur < bytes.Length; k++)
pixels[k] = bytes[cur++];
img.SetPixel(x,y,Color.FromArgb(pixels[0], pixels[1], pixels[2]));
}
}
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "Image Files (*.png, *.jpg) | *.png; *.jpg";
saveFile.InitialDirectory = @"C:\";
if (saveFile.ShowDialog() == DialogResult.OK) {
textBox1.Text = saveFile.FileName.ToString();
img.Save(textBox1.Text);
pictureBox1.ImageLocation = textBox1.Text;
}
}
private void button2_Click(object sender, EventArgs e) {//Дешифруем текст из файла
if (pictureBox1.Image == null) {
MessageBox.Show("Изображение не выбрано");
return;
}
Bitmap img = new Bitmap(pictureBox1.Image);
int length = BitConverter.ToInt32(new byte[] {//Получаем длинну массива
img.GetPixel(0,0).R,
img.GetPixel(0,0).G,
img.GetPixel(0,0).B,
img.GetPixel(1,0).R
},0);
byte[] textBytes = new byte[length];
//читаем шифрованный текст
int cur = 0;
for (int y = 0; y < img.Height; y++) {
for (int x = 0; x < img.Width && cur < textBytes.Length; x++) {
if (y == 0 && x == 0) {
x = 1;
continue;
}
for (int k = 0; k < 3 && cur < textBytes.Length; k++)
textBytes[cur++] = (new byte[] {img.GetPixel(x, y).R, img.GetPixel(x, y).G, img.GetPixel(x, y).B}[k]);
}
}
string text = Encoding.UTF8.GetString(textBytes);
textBox2.Text = text;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question