Скачиваний:
184
Добавлен:
15.06.2014
Размер:
6.5 Mб
Скачать

Приложение д. Исходный текст классаpatchworkinjector

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Collections;

using System.Security.Cryptography;

namespace WatermarkInjector

{

class PatchworkInjector

{

private Bitmap bmp;

private string srcfile;

public PatchworkInjector(Bitmap bmpInj,string filename)

{

bmp = bmpInj;

srcfile = filename;

}

private Color ChangeBrightness(Color c,int delta)

{

int cR = c.R + delta;

int cG = c.G + delta;

int cB = c.B + delta;

if (cR < 0) cR = 1;

if (cR > 255) cR = 255;

if (cG < 0) cG = 1;

if (cG > 255) cG = 255;

if (cB < 0) cB = 1;

if (cB > 255) cB = 255;

return Color.FromArgb(c.A,cR,cG,cB);

}

private int GetDelta(Color c1,Color c2)

{

return ((int)((c1.R-c2.R)+(c1.G-c2.G)+(c1.B-c2.B))/3);

}

public Bitmap Inject()

{

byte[] src = new byte[10000];

int delta = 5;

FileStream fs = new FileStream(srcfile, FileMode.Open);

BinaryReader br = new BinaryReader(fs);

byte[] slt = br.ReadBytes((int)fs.Length); //Затравка. это и будем внедрять (генерируется из входного файла)

Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes("WMInjector", slt, 1);

src = rfc.GetBytes(40000);

for (int i = 0; i <= src.Length-8; i+=8)

{

int x1 = (src[i] * 256 + src[i + 1]) % bmp.Width;

int y1 = (src[i + 2] * 256 + src[i + 3]) % bmp.Height;

int x2 = (src[i + 4] * 256 + src[i + 5]) % bmp.Width;

int y2 = (src[i + 6] * 256 + src[i + 7]) % bmp.Height;

bmp.SetPixel(x1,y1,ChangeBrightness(bmp.GetPixel(x1,y1),delta));

bmp.SetPixel(x2,y2, ChangeBrightness(bmp.GetPixel(x2,y2), -delta));

}

br.Close();

fs.Close();

return (Bitmap)bmp.Clone();

}

public bool Assure()

{

byte[] src = new byte[10000];

FileStream fs = new FileStream(srcfile, FileMode.Open);

BinaryReader br = new BinaryReader(fs);

byte[] slt = br.ReadBytes((int)fs.Length); //это и будем внедрять

Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes("WMInjector", slt, 1);

src = rfc.GetBytes(40000);

int[] asr = new int[10000];

float mS = 0;

for (int i = 0; i <= src.Length - 8; i += 8)

{

int x1 = (src[i] * 256 + src[i + 1]) % bmp.Width;

int y1 = (src[i + 2] * 256 + src[i + 3]) % bmp.Height;

int x2 = (src[i + 4] * 256 + src[i + 5]) % bmp.Width;

int y2 = (src[i + 6] * 256 + src[i + 7]) % bmp.Height;

mS += bmp.GetPixel(x1,y1).GetBrightness() - bmp.GetPixel(x2,y2).GetBrightness();

}

br.Close();

fs.Close();

return (Math.Abs(mS)>50);

}

}

}

54