当前位置: 首页 > 编程日记 > 正文

C#程序可将文本文件藏于位图中,也可导出

//使用方法:
// 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 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 be stored in this bitmap.../n";
if (maxByteStorage < dataStream.Length + 4)
{
error = "Not enough pixel in 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 = (100 * (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("!! First 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 not be found :(");
return;
}
catch
{
Console.WriteLine("!! An exception occurred :(");
return;
}

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

return;
}
}

相关文章:

溢价 5 倍欲将 SiFive 收入麾下,英特尔的绝地反击战

作者 | 马超责编 | 张红月出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;受任于败军之际&#xff0c;奉命于危难之间。近几年硅谷史上的传奇、英特尔几位掌门葛洛夫与欧德宁相继离世&#xff0c;虽然他们早已脱离一线多年&#xff0c;但是他们的离去可能还是让英特…

教你用Vue渐进式搭建聊天室,从JavaScript=TypeScript

前言 VueSocket.io这个轮子已经有很多人造过了&#xff0c;为了不重复造轮子&#xff0c;我将本项目以三阶段实现&#xff08;大家可以在github中的Releases查看&#xff09;&#xff1a; 纯前端&#xff08;Vuex&#xff09;后端前端&#xff08;JavaScript&#xff09;后端前…

如何学习linux编程

如果想学习UNIX/LINUX的编程&#xff0c;《APUE》绝对经典的教材&#xff0c;加深一下功底&#xff0c;学习《UNP》的第二卷。这样基本上系统方面的就可以掌握了。如果继续网络编程&#xff0c;建议看《TCP/IP进行网际互连》的第三卷&#xff0c;里面有很多关于应用协议telnet、…

HTML中的form表单有一个关键属性 enctype

HTML中的form表单有一个关键属性 enctype&#xff1d;application/x-www-form-urlencoded 或multipart/form-data。 1、enctype"application/x-www-form-urlencoded"是默认的编码方式&#xff0c;当以这种方式提交数据时&#xff0c;HTTP报文中的内容是&#xff1a; …

赠书 | JavaScript 武力值飙升!用 TensorFlow.js 轻松在浏览器里搞深度学习

近年来&#xff0c;AI 与人类的生活越来越紧密&#xff0c;慢慢变得无处不在。那么提到 AI &#xff0c;我们会想到什么&#xff1f;小编最先想到的是机器人。早在小学作文中&#xff0c;我就写到 2021 年到处都是机器人&#xff0c;机器人汽车到处飞。结果 2021 年到来&#x…

[译] JWT 与 Spring Cloud 微服务

keyholesoftware.com/2016/06/20/…作者&#xff1a;THOMAS KENDALL译者&#xff1a;oopsguy.com 微服务安全是架构的一个重要部分。具体来说&#xff0c;就是认证和授权模式。 微服务认证和授权处理方式有几种选择&#xff0c;但本文只介绍 JSON Web Token 的使用。 JSON Web …

20步打造最安全的Nginx Web服务器

Nginx是一个轻量级的&#xff0c;高性能的Web服务器以及反向代理和邮箱(IMAP/POP3)代理服务器。它运行在UNIX,GNU/Linux,BSD各种版本&#xff0c;Mac OS X,Solaris和Windows。根据调查统计&#xff0c;6%的网站使用Nginx Web服务器。Nginx是少数能处理C10K问题的服务器之一。跟…

C#创建和调用DLL

一、写在前面 C# 语言是一种简单但功能强大的编程语言&#xff0c;用于编写企业应用程序。 C# 语言从C和 C语言演化而来&#xff0c;在语句、表达式和运算符方面使用了许多 C 功能。 C# 语言在类型安全性、版本转换、事件和垃圾回收等方面进行了相当大的改进和创新。 C# 语言提…

死磕算法!35 篇算法设计实例+6 本必读书打包送你

算法为什么难学&#xff1f;算法在程序中扮演着非常重要的角色&#xff0c;有人将数据结构比喻为程序的骨架&#xff0c;将算法比喻为程序的灵魂&#xff0c;这一点也不为过&#xff0c;正是因为这一点&#xff0c;很多朋友都立志要学好算法&#xff0c;但是我常常看到各种抱怨…

EXCHANGE证书

证书&#xff1a; CA&#xff08;证书颁发机构&#xff09;和证书有什么区别&#xff1f; CA&#xff1a;是服务器中的一个服务&#xff0c;主要是用来为计算机&#xff08;用户&#xff09;来颁发证书&#xff0c;安装CA的服务器称为证书服务器&#xff0c; 证书&#xff1a;从…

C#2.0模拟List和内置算法

C#中的范型对于很多从C转过来的程序员来说&#xff0c;可以说是一个天大的喜讯。hehe&#xff0c;至少笔者对于这个新特性是充满了敬仰之情。 在C#2.0中&#xff0c;匿名方法、IEnumerable接口和匿名方法的合作&#xff0c;使很多的编程任务变得非常的简单&#xff0c;而且写出…

​横扫六大权威榜单后,达摩院开源深度语言模型体系 AliceMind

整理 | AI 科技大本营&#xff08;ID:rgznai100&#xff09;自然语言处理&#xff08;NLP&#xff09;被誉为 AI 皇冠上的明珠&#xff0c;传统 NLP 模型制作复杂&#xff0c;耗时耗力&#xff0c;且用途单一&#xff0c;难以复用。预训练语言模型是 NLP 领域的研究热点之一&am…

WP8:Unity3D之间的值传递

原地址&#xff1a;http://www.cnblogs.com/zhxilin/p/3799210.html 在前面的讨论中&#xff0c;我们介绍了如何在Unity3D for WP8中使用高于.Net 3.5的第三方库&#xff0c;传送门:http://www.cnblogs.com/zhxilin/p/3311240.html 在Unity3D和WP8的交互当中&#xff0c;如果要…

未来的程序员面临着怎样的职业变化

作为程序员&#xff0c;我们总是身处于如万花筒般变化无常的技术世界里。我们可能也是那群能够最早感知到科技变化所带来巨大影响的人。然而&#xff0c;面对这一波又一波向我们袭来的技术变革&#xff0c;我们是否也能从中窥见一丝规律&#xff0c;从而使自己更好地应对未来呢…

C#中使用Win32和其他库

C# 用户经常提出两个问题&#xff1a;“我为什么要另外编写代码来使用内置于 Windows 中的功能&#xff1f;在框架中为什么没有相应的内容可以为我完成这一任务&#xff1f;”当框架小组构建他们的 .NET 部分时&#xff0c;他们评估了为使 .NET 程序员可以使用 Win32 而需要完成…

神经网络的学习方式网络传播和图卷积,两者到底什么关系?

作者 | Remy Lau本文转载自CSDN博主「deephub」你可能听说过图卷积&#xff0c;因为它在当时是一个非常热门的话题。虽然不太为人所知&#xff0c;但网络传播是计算生物学中用于网络学习的主要方法。在这篇文章中&#xff0c;我们将深入研究网络传播背后的理论和直觉&#xff0…

string与数值之间的转换

9.50 编写程序处理一个vector<string>,其元素都表示整数型。计算vector中所有元素之和。修改程序&#xff0c;使之计算表示浮点值的string之和。 程序如下&#xff1a; #include<string> #include<vector> #include<iostream> using namespace std;int…

一个完整的大作业

1.选一个自己感兴趣的主题。网址为http://news.gzcc.cn/html/xiaoyuanxinwen/ 2.网络上爬取相关的数据 import requests import re from bs4 import BeautifulSoup urlhttp://news.gzcc.cn/html/xiaoyuanxinwen/ resrequests.get(url) res.encodingutf-8 soupBeautifulSoup(res…

剖析C#的多态

一、什么是多态 面向对象程序设计中的另外一个重要概念是多态性。在运行时&#xff0c;可以通过指向基类的指针&#xff0c;来调用实现派生类中的方法。可以把一组对象放到一个数组中&#xff0c;然后调用它们的方法&#xff0c;在这种场合下&#xff0c;多态性作用就体现出来了…

OSPF单区域配置

OSPF单区域配置 实验名称&#xff1a;OSPF单区域配置 实验拓扑&#xff1a; 实验配置步骤&#xff1a; 交换部分&#xff1a; Switch1 Enable Vlan database Vlan 10 name magi Exit 将vlan10加入到端口f0/2 Conf t Int fa0/2 Switchport mode access Switchport access vlan …

一文搞定7大流行后端框架:Spring、Netty、MyBatis、Hibernate、Dubbo...

框架&#xff08;Framework&#xff09;是整个或部分系统的可重用设计&#xff0c;表现为一组抽象构件及构件实例间交互的方法&#xff1b;另一种定义认为&#xff0c;框架是可被应用开发者定制的应用骨架。前者是从应用方面而后者是从目的方面给出的定义。 可以说&#xff0c;…

全“芯”关注用户需求 AMD“超轻薄笔记本”杀出重围

现在10.6寸跟11.6寸的笔记本已经占据整个笔记本市场的15%左右&#xff0c;跟过去只有几个点相比&#xff0c;已经有很大的提升了&#xff0c;几乎是百分之百的提升&#xff0c;超轻薄笔记本是大势所趋。这种趋势也带动了两大芯片巨头英特尔和AMD的角逐&#xff0c;英特尔为新一…

“去了太空就别回来了!”贝索斯还没“上天”,就遭美国 5 万多人请愿:不准重返地球...

整理 | 郑丽媛出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;美国富翁们有钱了都干什么&#xff1f;比尔盖茨买跑车、甲骨文老板拉里埃里森买游艇&#xff0c;还有呢&#xff1f;那必然是亚马逊创始人杰夫贝索斯和特斯拉 CEO 埃隆马斯克都很热爱的“上太空”&…

C语言中的字符串处理

•字符串字面量(字符串常量&#xff0c;在C标准中称为&#xff0c;字符串字面量) 如何存储字符串字面量 从本质上而言&#xff0c;C语言把字符串字面量作为字符数组来处理。当C语言编译器在程序中遇到长度为n的字符串字面量时&#xff0c;它会为字符串字面量分配长度为n1的…

php 派生类 数据库连接 单例模式 xhprof实测 高效连接

2019独角兽企业重金招聘Python工程师标准>>> 、 <?php //要解决的问题 在一个方法中多次调用类 //多次调用父类相同的类 class Pdoo {public function __construct(){}//这是个数据库的类function select($name) {echo "正宗" . $name;} } class Con…

安装MariaDB

结果我还是成功安装了MariaDB&#xff0c;其实大部分时候系统的操作不会有什么问题的&#xff0c;只是有时候会遇到一些问题较折腾。 最简单的指南&#xff1a;https://www.linode.com/docs/databases/mariadb/how-to-install-mariadb-on-centos-7 根据stackoverflow网友的说法…

CentOS5.6下安装Oracle10G软件 【保留报错经验】

CentOS5.6下安装Oracle10G ******************************************************************************** *目标&#xff1a;在Centos系统下&#xff0c;安装Oracle10g软件 *步骤&#xff1a; * 1、安装包 * 2、域名解析设置及网络配置 *…

人大团队研究:面向文本生成,预训练模型进展梳理

作者 | 刘媛媛来源 | 数据实战派文本生成是 NLP 中最重要且颇具挑战性的任务之一。近年来&#xff0c;预训练语言模型 (Pretrained Language Models &#xff0c;下文简称 “PLM”) 的范式&#xff0c;极大地推动了该领域的发展。例如&#xff0c;我们曾介绍过 AI 在古诗生成上…

用C#编写获取远程IP,MAC的方法

如果要想获得远程的地址&#xff0c;需要用sendarp这个函数来实现。具体的代码如下&#xff1a; [DllImport("Iphlpapi.dll")] private static unsafe extern int SendARP(Int32 dest,Int32 host,ref IntPtr mac,ref IntPtr length); [DllImport("Ws2_32.dll…

纯JS导出excel(支持中文)

JsonExportExcel 简介 github EXAMPLE 使用方法 1.引入js (dist目录下JsonExportExcel.min.js) <script src"https://cuikangjie.github.io/JsonExportExcel/dist/JsonExportExcel.min.js"></script>npm install -save js-export-excelconst ExportJson…