Example #1
0
        //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();
        }