Tena koe.
Kaore i roa i mua, i a au e ako ana i te whare wananga, he kaupapa ako mo te ako "Nga tikanga mo te haumaru korero." Ko te taumahi i hiahia matou ki te hanga i tetahi papatono ka whakauru i tetahi karere ki nga konae GIF. I whakatau ahau ki te mahi i Java.
I roto i tenei tuhinga ka whakaahuahia e au etahi kaupapa whakaaro, me te pehea i hangaia ai tenei kaupapa iti.
Wāhanga ariā
hōputu GIF
Ko te GIF (Graphics Interchange Format - he whakatakotoranga mo te whakawhiti whakaahua) he whakatakotoranga mo te penapena whakaahua whakairoiro, ka taea te penapena i nga raraunga kōpeke me te kore e ngaro te kounga i roto i te whakatakotoranga tae atu ki te 256 tae. I whakawhanakehia tenei whakatakotoranga i te tau 1987 (GIF87a) e CompuServe mo te tuku whakaahua raster ki runga whatunga. I te tau 1989, i whakarereketia te whakatakotoranga (GIF89a), i taapirihia te tautoko mo te maramatanga me te pakiwaituhi.
He hanganga poraka nga konae GIF. He roa tonu te roa o enei poraka (kei runga ranei i etahi haki), no reira kare e taea te pohehe ki te waahi kei hea ia poraka. Ko te hanganga o te ahua GIF kore-waiata ngawari ki te whakatakotoranga GIF89a:
Mai i nga poraka katoa o te hanganga, i tenei keehi ka aro atu tatou ki te poraka papatae o te ao me nga tawhā e haangai ana mo te papatae:
CT — te aroaro o te papatae ao. Mena kua tautuhia tenei haki, me timata te papatae o te ao i muri tonu i te kakau mata arorau.
Size — te rahi o te papatae me te maha o nga tae o te pikitia. Uara mo tenei tawhā:
Rahinga
Te maha o nga tae
Rahi papatae, paita
7
256
768
6
128
384
5
64
192
4
32
96
3
16
48
2
8
24
1
4
12
0
2
6
Nga tikanga whakamunatanga
Ka whakamahia nga tikanga e whai ake nei hei whakamuna i nga karere ki nga konae atahanga:
Tikanga LSB (Moka Iti Rawa).
Tikanga taapiri papatae
tikanga LSB - he tikanga mo te steganography. Kei roto ko te whakakapi i nga moka nui whakamutunga i roto i te ipu (i a maatau, ko nga paita papatae o te ao) me nga moka o te karere huna.
Ka whakamahia e te papatono nga moka whakamutunga e rua i roto i nga paita papatae o te ao hei waahanga o tenei tikanga. Ko te tikanga mo te ahua 24-bit, ko te papatae tae e toru nga paita mo te whero, te puru, me te kaakaariki, i muri i te whakauru i tetahi karere ki roto, ka huri ia waahanga tae ma te teitei o te 3/255 karaehe. Ko taua huringa, tuatahi, kare e kitea, he uaua ranei ki te titiro atu ki te kanohi o te tangata, tuarua, karekau e kitea i runga i nga taputapu whakaputa korero kounga iti.
Ko te nui o nga korero ka whakawhirinaki tika ki te rahi o te papata ahua. I te mea he 256 nga tae te rahi o te papatae, a ki te tuhia e rua nga moka karere ki roto i te waahanga o ia tae, katahi ka 192 paita te roanga o te karere (me te papatae morahi o te ahua). Kia whakauruhia te karere ki roto i te ahua, kaore e rereke te rahi o te konae.
Tikanga roha papatae, e mahi ana mo te hanganga GIF anake. Ka tino whai hua ki nga whakaahua me te papanga iti. Ko te mea nui ko te whakanui ake i te rahi o te papatae, na reira he waahi taapiri mo te tuhi i nga paita e tika ana hei whakakapi i nga paita tae. Ki te whakaaro tatou ko te iti rawa o te papatae he 2 tae (6 paita), katahi ka taea te 256 × 3–6 = 762 paita te rahi morahi o te karere whakauru. Ko te ngoikoretanga ko te iti o te haumarutanga cryptographic; ka taea te panui i te karere whakauru ma te whakamahi i tetahi ētita tuhinga mena kaore i tukuna te karere ki etahi atu whakamunatanga.
Wāhanga whaihua
Te hoahoa hōtaka
Ko nga taputapu katoa e tika ana mo te whakatinana i te whakamunatanga me te whakamunatanga algorithms ka whakauruhia ki roto i te kete com.tsarik.steganography. Kei roto i tenei kete te atanga Encryptor me nga tikanga encrypt и decrypt, Karaehe Binary, e whakarato ana i te kaha ki te mahi me nga huinga moka, me nga karaehe rereke UnableToEncryptException и UnableToDecryptException, me whakamahi i nga tikanga atanga Encryptor ina he hapa whakawaehere me te wetewete.
Mōkihi hōtaka matua com.tsarik.programs.gifed ka whakaurua he karaehe papatono ka taea te whakahaere me te tikanga pateko main, ka taea e koe te whakahaere i te papatono; he karaehe e pupuri ana i nga tawhā hotaka; me nga kohinga me etahi atu akomanga.
Ko te whakatinanatanga o nga algorithms ake ka whakaatuhia i roto i te kete com.tsarik.programs.gifed.gif akomanga GIFEncryptorByLSBMethod и GIFEncryptorByPaletteExtensionMethod. Ka whakatinanahia e enei akomanga e rua te atanga Encryptor.
I runga i te hanganga o te whakatakotoranga GIF, ka taea e koe te hanga i tetahi algorithm whanui mo te whakauru i tetahi karere ki roto i te papanga whakaahua:
Hei whakatau i te ahua o te karere i roto i te ahua, me taapiri etahi raupapa moka ki te timatanga o te karere, ka panuihia e te kaiwaehere tuatahi ka tirotirohia mo te tika. Mena kaore i te rite, ka whakaarohia kaore he karere huna i roto i te ahua. I muri mai me tohu e koe te roa o te karere. Na ko te tuhinga o te karere ake.
Hoahoa akomanga o te tono katoa:
Te whakatinanatanga o te kaupapa
Ko te whakatinanatanga o te kaupapa katoa ka taea te wehewehe ki nga waahanga e rua: te whakatinanatanga o te whakamunatanga atanga me nga tikanga wetemuna Encryptor, i roto i nga karaehe GIFEncryptorByLSBMethod и GIFEncryptorByPaletteExtensionMethod, me te whakatinanatanga o te atanga kaiwhakamahi.
Whakaarohia te akomanga GIFEncryptorByLSBMethod.
mara firstLSBit и secondLSBit kei roto te maha o nga moka o ia paita o te atahanga hei whakauru i te karere me te wahi e panui ai te karere. Mara checkSequence pupuru ana i te raupapa moka haki hei whakapumau i te mohiotanga o te karere whakauru. Tikanga pateko getEncryptingFileParameters ka whakahoki i nga tawhā o te konae kua tohua me nga ahuatanga o te karere pea.
@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 me te waehere puna o te tikanga decrypt akomanga GIFEncryptorByLSBMethod:
@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);
}
Te whakatinanatanga o te karaehe GIFEncryptorByPaletteExtensionMethod ka rite, ko te tikanga mo te penapena/te panui korero he rerekee.
I roto i te akomanga MainFrame Kua whakaahuahia nga tikanga takai: encryptImage(Encryptor encryptor) и decryptImage(Encryptor encryptor), te tukatuka i nga hua o nga tikanga atanga Encryptor me te mahi tahi me te kaiwhakamahi, ara, te whakatuwhera i te korero whiriwhiringa konae, te whakaatu i nga karere hapa, aha atu; me etahi atu tikanga: openImage(), ka taea e te kaiwhakamahi te kowhiri i tetahi ahua, exit(), ka puta i te tono. Ko enei tikanga ka kiia mai i ActionKo nga taonga tahua e rite ana. Ka whakatinanahia e tenei akomanga nga tikanga awhina: createComponents() - te hanga i nga waahanga ahua, loadImageFile(File f) — te uta atahanga ki tetahi waahanga motuhake mai i te konae. Te whakatinanatanga o te karaehe GIFEncryptorByPaletteExtensionMethod he rite ki te whakatinanatanga o te karaehe GIFEncryptorByLSBMethod, ko te rereketanga nui ko te tuhi me te panui o nga paita karere mai i te papatae.
Te whakahaeretanga o te hōtaka
tikanga LBS
Me kii he ahua penei:
I roto i tenei ahua, e 256 nga tae o te papatae (na te peita e penapena ana). Ko nga tae tuatahi e wha: ma, pango, whero, matomato. He pango etahi atu tae. Ko te raupapa moka papatae o te ao ka penei: