Steganography ho GIF

Selelekela

Lumelang.
Hase khale haholo ha ke ntse ke ithuta univesithing, ho ne ho e-na le thupelo ea "Software mekhoa ea ts'ireletso ea tlhahisoleseding." Mosebetsi o ne o hloka hore re thehe lenaneo le kenyang molaetsa lifaeleng tsa GIF. Ke ile ka etsa qeto ea ho e etsa Java.

Sehloohong sena ke tla hlalosa lintlha tse ling tsa theory, hammoho le hore na lenaneo lena le lenyenyane le entsoe joang.

Karolo ea khopolo

GIF sebopeho

GIF (Graphics Interchange Format - sebopeho sa ho fapanyetsana litšoantšo) ke sebopeho sa ho boloka litšoantšo tse hlakileng, tse khonang ho boloka data e hatelitsoeng ntle le tahlehelo ea boleng ka sebopeho sa mebala e fihlang ho 256. Sebopeho sena se ntlafalitsoe ka 1987 (GIF87a) ke CompuServe bakeng sa ho fetisa litšoantšo tsa raster holim'a marang-rang. Ka 1989, sebopeho se ile sa fetoloa (GIF89a), tšehetso ea ponaletso le lipopae e ile ea eketsoa.

Lifaele tsa GIF li na le sebopeho sa block. Li-blocks tsena li lula li na le bolelele bo tsitsitseng (kapa ho itšetlehile ka lifolakha tse ling), kahoo ho batla ho le thata ho etsa phoso mabapi le hore na sebaka se seng le se seng se hokae. Sebopeho sa setšoantšo se bonolo ka ho fetesisa sa GIF se sa pheleng ka sebopeho sa GIF89a:

Steganography ho GIF

Har'a li-blocks tsohle tsa sebopeho, tabeng ena re tla thahasella "palette" ea lefats'e le liparamente tse ikarabellang bakeng sa phalete:

  • CT - boteng ba phalete ea lefats'e. Haeba folakha e setiloe, phalete ea lefats'e e tlameha ho qala hang ka mor'a ho tšoara skrine se utloahalang.
  • Size — boholo ba phalete le palo ya mebala e setshwantshong. Boleng ba parameter ena:

Size
Palo ea mebala
Boholo ba palette, li-byte

7
256
768

6
128
384

5
64
192

4
32
96

3
16
48

2
8
24

1
4
12

0
2
6

Mekhoa ea encryption

Mekhoa e latelang e tla sebelisoa ho patala melaetsa lifaeleng tsa litšoantšo:

  • Mokhoa oa LSB (Bonyane bo Bonyenyane).
  • Mokhoa oa ho eketsa palette

mokhoa oa LSB - mokhoa o tloaelehileng oa steganography. E kenyelletsa ho nkela likarolo tsa ho qetela tsa bohlokoa ka har'a sets'oants'o (ho rona, li-byte tsa lefats'e tsa palete) ka likotoana tsa molaetsa o patiloeng.

Lenaneo le tla sebelisa likotoana tse peli tsa ho qetela ho li-palette byte tsa lefats'e e le karolo ea mokhoa ona. Sena se bolela hore bakeng sa setšoantšo sa 24-bit, moo phalete ea mebala e leng li-byte tse tharo bakeng sa bofubelu, boputsoa le botala, ka mor'a ho kenya molaetsa ho eona, karolo e 'ngoe le e' ngoe ea mebala e tla fetoha ka boholo ba 3/255 gradations. Phetoho e joalo, ea pele, e tla ba e sa bonahaleng kapa e thata ho e hlokomela mahlong a motho, 'me ea bobeli, e ke ke ea bonahala ka lisebelisoa tsa boleng bo tlaase tsa tlhahiso ea boitsebiso.

Palo ea tlhahisoleseling e tla itšetleha ka ho toba ka boholo ba phalete ea setšoantšo. Kaha boholo bo boholo ba phalete ke mebala e 256, 'me haeba likotoana tse peli tsa melaetsa li ngotsoe karolong ea' mala o mong le o mong, joale bolelele ba molaetsa o moholo (ka palo e kholo ea setšoantšo setšoantšong) ke 192 bytes. Hang ha molaetsa o kenngoa setšoantšong, boholo ba faele ha bo fetohe.

Mokhoa oa ho atolosa Palette, e sebetsang feela bakeng sa sebopeho sa GIF. E tla sebetsa ka ho fetisisa litšoantšong tse nang le pente e nyenyane. Ntho ea bohlokoa ea eona ke hore e eketsa boholo ba phalete, ka hona e fana ka sebaka se eketsehileng sa ho ngola li-byte tse hlokahalang sebakeng sa li-byte tsa mebala. Haeba re nka hore boholo bo fokolang ba pente ke mebala e 2 (li-byte tse 6), joale boholo bo boholo ba molaetsa o kentsoeng e ka ba 256 × 3-6 = 762 byte. Bobebe ke ts'ireletso e tlase ea "cryptographic"; molaetsa o kentsoeng o ka baloa ho sebelisoa mohlophisi ofe kapa ofe oa mongolo haeba molaetsa o sa kengoa ho encryption e eketsehileng.

Karolo e sebetsang

Moralo oa lenaneo

Lisebelisoa tsohle tse hlokahalang bakeng sa ho kenya ts'ebetsong li-algorithms tsa encryption le decryption li tla kenyelletsoa ka har'a sephutheloana com.tsarik.steganography. Sephutheloana sena se kenyelletsa sebopeho Encryptor ka mekhoa encrypt и decrypt, Sehlopha Binary, e fanang ka bokhoni ba ho sebetsa ka li-bit arrays, hammoho le lihlopha tse ikhethang UnableToEncryptException и UnableToDecryptException, e lokelang ho sebelisoa mekhoeng ea li-interface Encryptor maemong a liphoso tsa khouto le decoding ka ho latellana.

Sephutheloana sa lenaneo le ka sehloohong com.tsarik.programs.gifed e tla kenyelletsa sehlopha sa lenaneo le sebetsang ka mokhoa o tsitsitseng main, ho u lumella ho tsamaisa lenaneo; sehlopha se bolokang liparamente tsa lenaneo; le liphutheloana le lihlopha tse ling.

Ts'ebetsong ea li-algorithms ka botsona e tla hlahisoa ka har'a sephutheloana com.tsarik.programs.gifed.gif lihlopha GIFEncryptorByLSBMethod и GIFEncryptorByPaletteExtensionMethod. Lihlopha tsena ka bobeli li tla kenya tšebetsong interface Encryptor.

Ho ipapisitsoe le sebopeho sa sebopeho sa GIF, o ka theha algorithm e akaretsang ea ho kenyelletsa molaetsa phaleteng ea setšoantšo:

Steganography ho GIF

Ho fumana hore na molaetsa o teng setšoantšong, hoa hlokahala ho eketsa tatellano e itseng ea likotoana qalong ea molaetsa, oo decoder o o balang pele ebe o hlahloba ho nepahala. Haeba e sa lumellane, joale ho nkoa hore ha ho na molaetsa o patiloeng setšoantšong. Ka mor'a moo u lokela ho bolela bolelele ba molaetsa. Ebe temana ea molaetsa ka boeona.

Setšoantšo sa sehlopha sa ts'ebeliso eohle:

Steganography ho GIF

Ho kenngwa tshebetsong ha lenaneo

Ts'ebetsong ea lenaneo lohle e ka aroloa ka likarolo tse peli: ts'ebetsong ea mokhoa oa ho kenyelletsa sebopeho le mekhoa ea ho hlakola Encryptor, lihlopheng GIFEncryptorByLSBMethod и GIFEncryptorByPaletteExtensionMethod, le ts'ebetsong ea sebopeho sa mosebedisi.

Nahana ka sehlopha GIFEncryptorByLSBMethod.

Steganography ho GIF

masimo firstLSBit и secondLSBit e na le linomoro tsa li-bits tsa byte ka 'ngoe ea setšoantšo seo molaetsa o lokelang ho kenngoa ho sona le moo molaetsa o lokelang ho baloa teng. Tšimo checkSequence e boloka tatellano ea cheke ho etsa bonnete ba hore molaetsa o kentsoeng. Mokhoa o tsitsitseng getEncryptingFileParameters e khutlisa liparamente tsa faele e boletsoeng le litšobotsi tsa molaetsa o ka bang teng.

Algorithm ea mokhoa encrypt класса GIFEncryptorByLSBMethod:

Steganography ho GIF

Le khoutu ea eona:

@Override
public void encrypt(File in, File out, String text) throws UnableToEncodeException, NullPointerException, IOException {
	if (in == null) {
		throw new NullPointerException("Input file is null");
	}
	if (out == null) {
		throw new NullPointerException("Output file is null");
	}
	if (text == null) {
		throw new NullPointerException("Text is null");
	}
	
	// read bytes from input file
	byte[] bytes = new byte[(int)in.length()];
	InputStream is = new FileInputStream(in);
	is.read(bytes);
	is.close();
	
	// check format
	if (!(new String(bytes, 0, 6)).equals("GIF89a")) {
		throw new UnableToEncodeException("Input file has wrong GIF format");
	}
	
	// read palette size property from first three bits in the 10-th byte from the file
	byte[] b10 = Binary.toBitArray(bytes[10]);
	byte bsize = Binary.toByte(new byte[] {b10[0], b10[1], b10[2]});
	
	// calculate color count and possible message length
	int bOrigColorCount = (int)Math.pow(2, bsize+1);
	int possibleMessageLength = bOrigColorCount*3/4;
	int possibleTextLength = possibleMessageLength-2;// one byte for check and one byte for message length
	
	if (possibleTextLength < text.length()) {
		throw new UnableToEncodeException("Text is too big");
	}
	
	int n = 13;
	
	// write check sequence
	for (int i = 0; i < checkSequence.length/2; i++) {
		byte[] ba = Binary.toBitArray(bytes[n]);
		ba[firstLSBit] = checkSequence[2*i];
		ba[secondLSBit] = checkSequence[2*i+1];
		bytes[n] = Binary.toByte(ba);
		n++;
	}
	
	// write text length
	byte[] cl = Binary.toBitArray((byte)text.length());
	for (int i = 0; i < cl.length/2; i++) {
		byte[] ba = Binary.toBitArray(bytes[n]);
		ba[firstLSBit] = cl[2*i];
		ba[secondLSBit] = cl[2*i+1];
		bytes[n] = Binary.toByte(ba);
		n++;
	}
	
	// write message
	byte[] textBytes = text.getBytes();
	for (int i = 0; i < textBytes.length; i++) {
		byte[] c = Binary.toBitArray(textBytes[i]);
		for (int ci = 0; ci < c.length/2; ci++) {
			byte[] ba = Binary.toBitArray(bytes[n]);
			ba[firstLSBit] = c[2*ci];
			ba[secondLSBit] = c[2*ci+1];
			bytes[n] = Binary.toByte(ba);
			n++;
		}
	}
	
	// write output file
	OutputStream os = new FileOutputStream(out);
	os.write(bytes);
	os.close();
}

Algorithm le khoutu ea mohloli oa mokhoa decrypt класса GIFEncryptorByLSBMethod:

Steganography ho GIF

@Override
public String decrypt(File in) throws UnableToDecodeException, NullPointerException, IOException {
	if (in == null) {
		throw new NullPointerException("Input file is null");
	}
	
	// read bytes from input file
	byte[] bytes = new byte[(int)in.length()];
	InputStream is = new FileInputStream(in);
	is.read(bytes);
	is.close();
	
	// check format
	if (!(new String(bytes, 0, 6)).equals("GIF89a")) {
		throw new UnableToDecodeException("Input file has wrong GIF format");
	}
	
	// read palette size property from first three bits in the 10-th byte from the file
	byte[] b10 = Binary.toBitArray(bytes[10]);
	byte bsize = Binary.toByte(new byte[] {b10[0], b10[1], b10[2]});
	
	// calculate color count and possible message length
	int bOrigColorCount = (int)Math.pow(2, bsize+1);
	int possibleMessageLength = bOrigColorCount*3/4;
	int possibleTextLength = possibleMessageLength-2;	// one byte for check and one byte for message length
	
	int n = 13;
	
	// read check sequence
	byte[] csBits = new byte[checkSequence.length];
	for (int i = 0; i < 4; i++) {
		byte[] ba = Binary.toBitArray(bytes[n]);
		csBits[2*i] = ba[firstLSBit];
		csBits[2*i+1] = ba[secondLSBit];
		n++;
	}
	byte cs = Binary.toByte(csBits);
	
	if (cs != Binary.toByte(checkSequence)) {
		throw new UnableToDecodeException("There is no encrypted message in the image (Check sequence is incorrect)");
	}
	
	// read text length
	byte[] cl = new byte[8];
	for (int i = 0; i < 4; i++) {
		byte[] ba = Binary.toBitArray(bytes[n]);
		cl[2*i] = ba[firstLSBit];
		cl[2*i+1] = ba[secondLSBit];
		n++;
	}
	byte textLength = Binary.toByte(cl);
	
	if (textLength < 0) {
		throw new UnableToDecodeException("Decoded text length is less than 0");
	}
	if (possibleTextLength < textLength) {
		throw new UnableToDecodeException("There is no messages (Decoded message length (" + textLength + ") is less than Possible message length (" + possibleTextLength + "))");
	}
	
	// read text bits and make text bytes
	byte[] bt = new byte[textLength];
	for (int i = 0; i < bt.length; i++) {
		byte[] bc = new byte[8];
		for (int bci = 0; bci < bc.length/2; bci++) {
			byte[] ba = Binary.toBitArray(bytes[n]);
			bc[2*bci] = ba[firstLSBit];
			bc[2*bci+1] = ba[secondLSBit];
			n++;
		}
		bt[i] = Binary.toByte(bc);
	}
	
	return new String(bt);
}

Phethahatso ea sehlopha GIFEncryptorByPaletteExtensionMethod e tla tšoana, ke mokhoa oa ho boloka/ho bala feela o fapaneng.

Ka sehlopheng MainFrame mekhoa ea wrapper e hlalosoa: encryptImage(Encryptor encryptor) и decryptImage(Encryptor encryptor), ho sebetsana le liphetho tsa mekhoa ea li-interface Encryptor le ho sebelisana le mosebedisi, ke hore, ho bula puisano ea khetho ea lifaele, ho bontša melaetsa ea liphoso, joalo-joalo; hammoho le mekhoa e meng: openImage(), e lumellang mosebelisi ho khetha setšoantšo, exit(), e hlahang ts'ebetsong. Mekhoa ena e bitsoa ho tloha Action's lintho tse tsamaellanang tsa menu. Sehlopha sena se boetse se sebelisa mekhoa e thusang: createComponents() - ho theha likarolo tsa foromo, loadImageFile(File f) — ho kenya senepe ho karolo e ikgethang ho tswa faeleng. Phethahatso ea sehlopha GIFEncryptorByPaletteExtensionMethod e ts'oanang le ts'ebetsong ea sehlopha GIFEncryptorByLSBMethod, phapang e khōlō ke tsela eo li-byte tsa melaetsa li ngotsoeng ka eona le ho baloa ho tloha phaleteng.

Ts'ebetso ea lenaneo

mokhoa oa LBS

Ha re re ho na le setšoantšo se kang sena:

Steganography ho GIF

Setšoantšong sena, phalete e na le mebala e 256 (kaha Paint e e boloka). Mebala ea pele e mene ke: e tšoeu, e ntšo, e khubelu, e tala. Mebala e meng ke e ntšo. Tatelano ea li-palette tsa lefats'e e tla ba ka tsela e latelang:

11111111 11111111 11111111 00000000 00000000 00000000 11111111 00000000 00000000 00000000 11111111 00000000...

Steganography ho GIF

Hang ha molaetsa o kentsoe, likotoana tse thaliloeng li tla nkeloa sebaka ke likotoana tsa molaetsa. Setšoantšo se hlahisoang ke hoo e ka bang ha se fapane le sa pele.

Ntho ea pele
Setšoantšo se nang le molaetsa o kentsoeng

Steganography ho GIF
Steganography ho GIF

Mokhoa oa ho atolosa Palette

Ha o bula setšoantšo se nang le molaetsa o sebelisang mokhoa ona, o tla bona setšoantšo se latelang:

Steganography ho GIF

Ho hlakile hore mokhoa ona o ke ke oa sebetsa bakeng sa mesebetsi e felletseng ea bohloela, mme o ka hloka ho ngolisoa ho eketsehileng ha molaetsa.

Encryption/decryption litšoantšong tse animated e sebetsa joalo ka litšoantšong tse tloaelehileng tse tsitsitseng, empa animation ha e robehe.

Mehloli e sebelisitsoeng:

Khoasolla:

Source: www.habr.com

Eketsa ka tlhaloso