The C # program can hide text files in a bitmap, or it can be exported!

Source: Internet
Author: User
Tags bool command line valid
Program//Usage:
Bmpsafe.exe/file2bmp (input BMP) (input file to hide) [Output FILE]
Bmpsafe.exe/bmp2file (data BMP) [Output FILE]

Using System;
Using System.IO;
Using System.Drawing;

public class Bitmap24writer
{
protected Bitmap bmp;
protected int CurX, Cury, Irgb;
protected uint Bitsleft, bitstotal;
Protected byte R, G, B;

Public Bitmap24writer (Bitmap BMP)
{
if (BMP. PixelFormat!= SYSTEM.DRAWING.IMAGING.PIXELFORMAT.FORMAT24BPPRGB)
throw new ArgumentException ();
Assign VARs
CurX = Cury = Irgb = 0;
This.bmp = BMP;
Bitsleft = Bitstotal = (UINT) BMP. Height * (UINT) BMP. Width * 3;
}

Public UINT Getunusedbitcount ()
{
return bitsleft;
}

Public UINT Getmaxbitstoragecount ()
{
return bitstotal;
}

Public Bitmap Getbitmap ()
{
return BMP;
}

public bool WriteByte (byte by)
{
if (Bitsleft < 8)
return false;

UINT BITS2DO = 8;

for (; CurX < BMP. Width; curx++)
{
if (Cury >= bmp. Height)
Cury = 0;

for (; Cury < BMP. Height; cury++)
{
if (bits2do = 0)
return true;

Color col = bmp. GetPixel (CurX, Cury);
R = Col. R
g = Col. G
b = Col. B

for (;;)
{
byte curbit = (Byte) (by & 1);

Switch (IRGB)
{
Case 0:
r = (byte) (R & 0xFE);
R |= Curbit;
Break

Case 1:
g = (byte) (G & 0xFE);
G |= curbit;
Break

Case 2:
b = (byte) (b & 0xFE);
b |= Curbit;
Break
}
--bits2do;
--bitsleft;
by >>= 1;
Bmp. SetPixel (CurX, Cury, Color.FromArgb (R, G, b));
if (Irgb = 2)
{
Irgb = 0;
Break
}
irgb++;
if (bits2do = 0)
return true;
}
}
}
return true;
}
}

public class Bitmap24reader
{
protected Bitmap bmp;
protected int CurX, Cury, Irgb;
protected uint Bitsleft, bitstotal;

Public Bitmap24reader (Bitmap BMP)
{
if (BMP. PixelFormat!= SYSTEM.DRAWING.IMAGING.PIXELFORMAT.FORMAT24BPPRGB)
throw new ArgumentException ();
CurX = Cury = Irgb = 0;
This.bmp = BMP;
Bitsleft = Bitstotal = (UINT) BMP. Height * (UINT) BMP. Width * 3;
}

Public UINT Getunusedbitcount ()
{
return bitsleft;
}

Public UINT Getmaxbitstoragecount ()
{
return bitstotal;
}

Public Bitmap Getbitmap ()
{
return BMP;
}

public bool ReadByte (out of Byte by)
{
by = 0;

if (Bitsleft < 8)
return false;

byte bit = 0;
UINT BITS2DO = 8;
for (; CurX < BMP. Width; curx++)
{
if (Cury >= bmp. Height)
Cury = 0;

for (; Cury < BMP. Height; cury++)
{
if (bits2do = 0)
return true;

Color col = bmp. GetPixel (CurX, Cury);
for (;;)
{
Switch (IRGB)
{
Case 0:
bit = (byte) (Col. R & 1);
Break

Case 1:
bit = (byte) (Col. G & 1);
Break

Case 2:
bit = (byte) (Col. B & 1);
Break
}
--bits2do;
--bitsleft;
by |= (byte) (bit << 7);
if (bits2do!= 0)
by >>= 1;
if (Irgb = 2)
{
Irgb = 0;
Break
}
irgb++;
if (bits2do = 0)
return true;
}
}
}
return true;
}
}

public class Bitmapworks
{
public static bool Data2bmp (FileStream dataStream, FileStream bmpstream, String outfname, out string error)
{
Error = "";

Bitmap bmp;
Try
{
BMP = new Bitmap (bmpstream);
}
Catch
{
Error = "Are you sure the specified bitmap is a valid bitmap?";
return false;
}
if (BMP. PixelFormat!= SYSTEM.DRAWING.IMAGING.PIXELFORMAT.FORMAT24BPPRGB)
{
Error = "Please drop only 24bit bitmaps,thanks!";
return false;
}

Error + + "-> Bitmap info:height=" + BMP. Height + "width=" + BMP. Width + "... \ n";

if (datastream.length = 0)
{
Error = "Input data has a not supported size of 0!";
return false;
}
UINT Maxbytestorage = (UINT) BMP. Height * (UINT) BMP. Width * 3)/8;
Error + + "-> up to" + (maxByteStorage-4) + "bytes could is stored in this bitmap...\n";
if (Maxbytestorage < Datastream.length + 4)
{
Error = "Not enough pixel into target bitmap to hide the input data block!";
return false;
}

Bitmap24writer bmpwriter = new Bitmap24writer (BMP);
UINT datasize = (UINT) datastream.length;
Try
{
for (UINT u = 0; u < 4; u++)
{
Bmpwriter.writebyte ((byte) datasize);
DataSize >>= 8;
}
for (UINT u = 0; u < datastream.length; u++)
Bmpwriter.writebyte ((Byte) datastream.readbyte ());
}
Catch
{
Error = "Error while modifing the bitmap ' s pixels!";
return false;
}

Double maxbitstorage = Bmpwriter.getmaxbitstoragecount ();
Double spaceused = (Maxbitstorage-bmpwriter.getunusedbitcount ())/maxbitstorage;
Error + + "-> space used:" + convert.touint32 (spaceused) + "%...\n";

Try
{
if (file.exists (outfname))
File.delete (Outfname);
Bmpwriter.getbitmap (). Save (Outfname);
}
Catch
{
Error = "couldn ' t save output to" + Outfname + "!";
return false;
}

Error + + "-> Output saved to:" + outfname + "... \ n";

return true;
}

public static bool Bmp2data (FileStream Bmpstream, String outfname, out string error)
{
Error = "";

Bitmap bmp;
Try
{
BMP = new Bitmap (bmpstream);
}
Catch
{
Error = "Are you sure the specified bitmap is a valid bitmap?";
return false;
}

Bitmap24reader Bmpreader;
Try
{
Bmpreader = new Bitmap24reader (BMP);
}
catch (ArgumentException)
{
Error = "This isn ' t a 24bit bitmap!";
return false;
}

FileStream OutStream;
Try
{
OutStream = File.create (outfname);
}
Catch
{
Error = "couldn ' t create output file:" + Outfname + "!";
return false;
}
UINT DataSize = 0;
BYTE Outbyte;
Try
{
for (UINT u = 0; u < 4; u++)
{
if (!bmpreader.readbyte (out outbyte))
throw new Exception ();
DataSize |= (UINT) (Outbyte << 8*3);
if (U!= 3)
DataSize >>= 8;
}
Error + + "-> Size of Hidden Data:" + datasize + "bytes...\n";
for (UINT u = 0; u < datasize; u++)
{
if (!bmpreader.readbyte (out outbyte))
throw new Exception ();
Outstream.writebyte (Outbyte);
}
}
Catch
{
Error = "Exception caught while reading the hidden data!";
return false;
}
Finally
{
Outstream.close ();
}

Error + + "-> Output saved to:" + outfname + "... \ n";

return true;
}

}

Class Bmpsafe
{
public static string cmdline =
"Command line:\n" +
"Bmpsafe.exe/file2bmp (input BMP) (input file to hide) [output file]\n] +
"Bmpsafe.exe/bmp2file (data BMP) [Output file]";

private static string ServiceOne = "/file2bmp";
private static string servicetwo = "/bmp2file";

[STAThread]
static void Main (string[] args)
{
Console.WriteLine (
"Bmpsafe-hide files in 24bit bitmaps\n" +
"A little steganografie implementation\n" +
"By yoda\n" +
"-------------------------------------------------------------------------------\ n");

String inFile = "", Inbmp, outfile;
BOOL Bfile2bmp;
if (args. Length < 2)
{
Console.WriteLine ("!!) Invalid Number of arguments:(");
Console.WriteLine (CmdLine);
Return Err
}
if (String.Compare (Args[0], serviceone, true) = = 0)
Bfile2bmp = true;
else if (String.Compare (args[0), servicetwo, true) = = 0)
Bfile2bmp = false;
Else
{
Console.WriteLine ("!!) The parameters must be either \/file2bmp\ or \/bmp2file\ "!");
Return
}

Inbmp = args[1];
if (bfile2bmp)
{
if (args. Length < 3)
{
Console.WriteLine ("!!) Invalid Number of arguments:(");
Console.WriteLine (CmdLine);
Return
}
InFile = args[2];
if (args. Length > 3)
outfile = args[3];
Else
outfile = "Secret.bmp";
}
Else
{
if (args. Length > 2)
outfile = args[2];
Else
outfile = "Secret.bin";
}

Console.WriteLine ("-> processing input ...");
Try
{
string err;
BOOL ret;

if (bfile2bmp)
ret = Bitmapworks.data2bmp (
File.openread (InFile),
File.openread (Inbmp),
OutFile,
Out err);
Else
ret = Bitmapworks.bmp2data (
File.openread (Inbmp),
OutFile,
Out err);
if (!ret)
{
Console.WriteLine ("!!" + err);
Return
}
Else
Console.Write (ERR);
}
catch (FileNotFoundException)
{
Console.WriteLine ("!!) At least one file could is found:(");
Return
}
Catch
{
Console.WriteLine ("!!) An exception occurred:(");
Return
}

Console.WriteLine ("-> Job done ...");

Return
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.