//Create 1 file Stego from input public void CreateStegoFile(string inPath1, string message, string password, string inPath2) { //Checking input if (inPath1 == "") { throw new Exception("You have not selected the image yet"); } if (inPath2 == "") { throw new Exception("You have not defined where the image will be saved"); } if (message == "") { throw new Exception("Message doesn't be allowed to be empty"); } if (password == "") { throw new Exception("Password doesn't be allowed to be empty"); } // Mo file dau vao FileStream inStream = new FileStream(inPath1, FileMode.Open, FileAccess.Read); // kiem tra xem co phai anh bitmap 24 bits khong char b = (char)inStream.ReadByte(); char m = (char)inStream.ReadByte(); if (!(b == 'B' && m == 'M')) { throw new Exception("Image must be bitmap format"); } // kiem tra xem co phai la anh 24 bit hay k inStream.Seek(28, 0); // dua con tro ve vi tri byte thu 28 byte[] temp = new byte[2]; inStream.Read(temp, 0, 2); // so bit/1pixel duoc luu bang 2 byte Int16 nBit = BitConverter.ToInt16(temp, 0); // chuyen mang byte temp[] ve so nguyen 16 bit if (nBit != 24) { throw new Exception("It's not a 24 bit image"); } // Doc 54 byte phan header roi dua vao trong outStream int offset = 54; inStream.Seek(0, 0); byte[] header = new byte[offset]; inStream.Read(header, 0, offset); //viet 54 byte nay vao trong file stego ( file dau ra) FileStream outStream = new FileStream(inPath2, FileMode.Create, FileAccess.Write); outStream.Write(header, 0, offset); // ma hoa thong diep va mat khau thanh 1 thong diep duy nhat UnicodeEncoding unicode = new UnicodeEncoding(); byte[] newMessageByte = AddMessLengthToAhead(unicode.GetBytes(message));// them 4 byte do dai cua message vao dau cua thong diep // thuc hien tron newMessageByte = Crypto.Encrypt(newMessageByte, password); // thuc hien giau thong diep nay vao trong anh inStream.Seek(offset, 0); // dua con tro ve noi bat dau cua Data o vi tri thu 54 (offset=54) LSB.Encode(inStream, newMessageByte, outStream); //thay tung bit cua thong diep vao LSB cua inStream va ghi ra outStream inStream.Close(); // dong file anh dau vao outStream.Close(); }
private void button3_Click(object sender, EventArgs e) // thuc hien trich thong tin tu Stego file { // kiem tra xem dau vao co hop le khong? neu khong tao ra cac exception if (inPath2 == "") { throw new Exception("You have not selected the image containing message"); } string password = textBox3.Text; if (password == "") { throw new Exception("Password doesn't be allowed to be empty"); } // kiem tra xem file dau vao co phai la file Bitmap 24 bit khong? FileStream inStream = new FileStream(inPath2, FileMode.Open, FileAccess.Read); inStream.Seek(0, 0); // dua con tro ve dau file char b = (char)inStream.ReadByte(); char m = (char)inStream.ReadByte(); if (!(b == 'B' && m == 'M')) { throw new Exception("Image must be bitmap format"); } // kiem tra xem co phai anh bitmap 24 bit khong int offset = 28; inStream.Seek(offset, 0); byte[] temp = new byte[2]; // doc vao 2 byte inStream.Read(temp, 0, 2); // vi tri 28 va 29 // chuyen temp[] ve so nguyen 16 bit Int16 numOfBit = BitConverter.ToInt16(temp, 0); if (numOfBit != 24) { throw new Exception("It's not a 24 bit image"); } // bat dau tham nhap vao phan data offset = 54; inStream.Seek(offset, 0); byte[] bLen = new byte[4]; // 4 byte luu tru do dai thong diep bLen = LSB.Decode(inStream, 4); // cho nay khong the dung method FileStream.Read duoc,do 4 byte nay thuc chat la 32 byte trong inStream //decrypt 4 byte nay de duoc 4 byte thuc su ban dau (do ca 4 byte nay cung duoc ma hoa boi khoa Key[128]) bLen = Crypto.Decrypt(bLen, password); int length = BitConverter.ToInt32(bLen, 0); // chuyen tu mang byte thanh so nguyen // thuc hien doc ra mang thong diep an (van bi Encrypt) inStream.Seek(offset + 4 * 8, 0); // 32 byte dau tien de luu do dai cua thong diep byte[] buffer = new byte[length]; // su dung mang nay de luu tru tam try { buffer = LSB.Decode(inStream, length); } catch { throw new Exception("This image has contained message or your password is incorrect."); } // trong qua trinh trich xuat ra thong diep giau ,neu gap 1 ngoai le nao do,coi nhu trich xuat khong thanh cong ( thong thuong ngoai le phat sinh se la khong du bo nho) byte[] realHidenMessage = new byte[4 + buffer.Length]; realHidenMessage = ConcatTwoByteArray(bLen, buffer); // them 4 byte vao dau de tien cho viec Decrypt realHidenMessage = Crypto.Decrypt(realHidenMessage, password); // bay gio ta da duoc mang thong diep thuc su byte[] hidenMessage = new byte[length]; // bay gio ta chi quan tam den phan thong diep for (int i = 0; i < length; i++) { hidenMessage[i] = realHidenMessage[i + 4]; } // chuyen ve dang string UnicodeEncoding unicode = new UnicodeEncoding(); string result = unicode.GetString(hidenMessage); textBox4.Text = result; // hien thi ket qua ra textbox inStream.Close(); // dong file dau vao }