Chinaunix首页 | 论坛 | 博客
  • 博客访问: 366617
  • 博文数量: 19
  • 博客积分: 588
  • 博客等级: 中士
  • 技术积分: 317
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-06 15:35
文章分类
文章存档

2012年(19)

分类: IT职场

2012-04-13 20:26:03

   参考别人的LSB的程序,我只是简单的修改了一下,使得可以从最低位到最高位都可以嵌入数据。

点击(此处)折叠或打开

  1. // LsbT.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"

  4. #include"ED.h"
  5. #include<iostream>
  6. using namespace std;
  7. //#include"ft_dib.cpp"

  8. int main(int argc,char *argv[])
  9. {
  10.     int id = -1;
  11.     int b;
  12.     char*filename = new char;
  13.     FTDib dib;
  14.     cout<<"=========基于LSB加密==========="<<endl;
  15.     while(id){
  16.         cout<<"+------------------------------+"<<endl;
  17.         cout<<"| 1.加密一个文件 |"<<endl;
  18.         cout<<"| 2.解密一个BMP文件 |"<<endl;
  19.         cout<<"| 3.退出 |"<<endl;
  20.         cout<<"+------------------------------+"<<endl<<endl;
  21.         cout<<">>>>>>>>>>文件名带后缀<<<<<<<<<<"<<endl<<endl;
  22.         cout<<"请选择:"<<endl;
  23.         cout<<" --->";
  24.         cin>>id;
  25.         switch(id)
  26.         {
  27.         case 1:
  28.             cout<<"原始BMP文件名称:";
  29.             cin>>filename;
  30.             dib.LoadFile(filename);
  31.             /////////////////////////////////
  32.             cout<<"请输入要加密的比特位数: ";
  33.             cin>>b;
  34.             ////////////////////////////////
  35.             cout<<"请输入要加密的文件名称:";
  36.             cin>>filename;
  37.             if(dib.LSBCoder(filename,b)==-1)
  38.             {
  39.                 cout<<" >>>文件长度过长<<<"<<endl;
  40.                 cout<<"+------------------------------+"<<endl;
  41.                 cout<<"| >>>请注意<<< |"<<endl;
  42.                 cout<<"| 请隐藏较小文件 |"<<endl;
  43.                 cout<<"| 或重新载入较大的bmp文件 |"<<endl;
  44.                 cout<<"+------------------------------+"<<endl<<endl;
  45.                 cout<<">>>按回车继续操作<<<"<<endl;
  46.                 system("pause");
  47.                 id = -1;
  48.                 break;
  49.             }
  50.             cout<<"加密后BMP文件名称:";
  51.             cin>>filename;
  52.             dib.SaveFile(filename);
  53.             break;
  54.         case 2:
  55.             cout<<"请输入要解密BMP图像名称:";
  56.             cin>>filename;
  57.             dib.LoadFile(filename);
  58.             /////////////////////////////////
  59.             cout<<"请输入要解密的比特位数: ";
  60.             cin>>b;
  61.             ////////////////////////////////
  62.             cout<<"解密后结果文件名称:";
  63.             cin>>filena

    点击(此处)折叠或打开

    1. /*********************************************
    2. * 功能:处理bmp图象
    3. * 作者:foxtail
    4. * 构建日期: 2008/3/4
    5. * 修改作者:erlingmusan
    6. * 构建日期: 2012/4/13
    7. *********************************************/
    8. #include "stdafx.h"
    9. #include<string>
    10. #include<fstream>
    11. #include<iostream>
    12. #include"ED.h"

    13. #define BI_RLE8 1L
    14. #define BI_RLE4 2L
    15. #define WIDTHBYTES(bits) (((bits)+31)/32*4)

    16. FTDib::FTDib()
    17. {
    18.     size = 0;
    19.     pDib = NULL;
    20.     m_pBitmapFileHeader = new BitmapFileHeader;//文件头
    21.     m_pBitmapInfoHeader = new BitmapInfoHeader;//信息头
    22.     m_pBitmapInfo = (BitmapInfo*)m_pBitmapInfoHeader;
    23. }

    24. FTDib::~FTDib()
    25. {
    26.     delete m_pBitmapFileHeader;
    27.     delete m_pBitmapInfoHeader;
    28.     if(pDib)
    29.     {
    30.         delete pDib;
    31.     }
    32. }
    33. //---------------------------------------------
    34. // 载入图像到一片内存区
    35. //---------------------------------------------
    36. void FTDib::LoadFile(const char* dibFileName)
    37. {
    38.     strcpy(m_fileName,dibFileName);
    39.     ifstream dibFile;
    40.     dibFile.open(m_fileName, ios::binary | ios::in);

    41.     dibFile.read((char*)m_pBitmapFileHeader,sizeof(BitmapFileHeader));//读入文件头
    42.     cout<<"读入文件头完毕"<<endl;
    43.     dibFile.read((char*)m_pBitmapInfoHeader,sizeof(BitmapInfoHeader));
    44.     cout<<"读入信息头完毕"<<endl;

    45.     if(m_pBitmapFileHeader->bfType == 0x4d42)
    46.     {
    47.         //读入图像信息
    48.         size = m_pBitmapFileHeader->bfSize - sizeof(BitmapFileHeader) - sizeof(BitmapInfoHeader);
    49.         pDib = new BYTE[size];
    50.         dibFile.read((char*)pDib,size);
    51.         dibFile.close();



    52.         m_pRGB = (RGB_Element*)(pDib);
    53.         m_numberOfColors = GetNumberOfColors();
    54.         if(m_pBitmapInfoHeader->biColors != 0){m_pBitmapInfoHeader->biColors = m_numberOfColors;}
    55.         DWORD colorTableSize = m_numberOfColors * sizeof(RGB_Element);
    56.         m_pData = pDib + colorTableSize;

    57.         if(m_pRGB == (RGB_Element*)m_pData)//没有颜色表
    58.         {
    59.             m_pRGB = NULL;
    60.         }
    61.         m_pBitmapInfoHeader->biDataSize = GetSize();
    62.         m_valid = true;
    63.         cout<<"载入成功"<<endl;
    64.     }
    65.     else
    66.     {
    67.         m_valid = false;
    68.         cerr<<"该图不是bmp格式";
    69.     }

    70. }
    71. //---------------------------------------------
    72. // 检查图片格式
    73. //---------------------------------------------
    74. bool FTDib::IsValid()
    75. {
    76.     return m_valid;
    77. }

    78. //---------------------------------------------
    79. // 取得图片名称或路径
    80. //---------------------------------------------
    81. char * FTDib::GetFileName()
    82. {
    83.     return m_fileName;
    84. }

    85. //---------------------------------------------
    86. // 取得图像宽度和高度
    87. //---------------------------------------------
    88. unsigned int FTDib::GetWidth()
    89. {
    90.     return (unsigned int)m_pBitmapInfoHeader->biWidth;
    91. }

    92. unsigned int FTDib::GetHeight()
    93. {
    94.     return (unsigned int)m_pBitmapInfoHeader->biHeight;
    95. }

    96. //---------------------------------------------
    97. // 取得图像数据区大小
    98. //---------------------------------------------
    99. DWORD FTDib::GetSize()
    100. {
    101.     if(m_pBitmapInfoHeader->biDataSize != 0)
    102.     {
    103.         return m_pBitmapInfoHeader->biDataSize;
    104.     }
    105.     else
    106.     {
    107.         DWORD height = (DWORD)GetHeight();
    108.         DWORD width = (DWORD)GetWidth();
    109.         return height * width;
    110.     }
    111. }

    112. //---------------------------------------------
    113. // 取得图像宽度
    114. //---------------------------------------------
    115. DWORD FTDib::GetDibWidthBytes()
    116. {
    117.     byBitCount = m_pBitmapInfoHeader->biBitPerPixel;
    118.     long nWidth = m_pBitmapInfoHeader->biWidth;

    119.     dwWidthBytes = (DWORD)m_pBitmapInfoHeader->biWidth;
    120.     switch(dwWidthBytes)
    121.     {
    122.     case 1:
    123.         dwWidthBytes = (nWidth + 7)/8;
    124.     case 2:
    125.         dwWidthBytes = (nWidth + 1)/2;
    126.     case 24:
    127.         dwWidthBytes = 3*nWidth;
    128.     }

    129.     while((dwWidthBytes&3) != 0){++dwWidthBytes;}

    130.     return dwWidthBytes;
    131. }
    132. //---------------------------------------------
    133. // 取得图像颜色数
    134. //---------------------------------------------
    135. unsigned int FTDib::GetNumberOfColors()
    136. {
    137.     int numberOfColors;

    138.     if((m_pBitmapInfoHeader->biColors == 0)&&(m_pBitmapInfoHeader->biBitPerPixel < 9))
    139.     {
    140.         switch(m_pBitmapInfoHeader->biBitPerPixel)
    141.         {
    142.         case 1:
    143.             numberOfColors = 2;break;
    144.         case 4:
    145.             numberOfColors = 16;break;
    146.         case 8:
    147.             numberOfColors = 256;
    148.         }
    149.     }
    150.     else
    151.     {
    152.         numberOfColors = (int)m_pBitmapInfoHeader->biColors;
    153.     }
    154.     return numberOfColors;
    155. }
    156. //---------------------------------------------
    157. // 取得图像数据区指针
    158. //---------------------------------------------
    159. BYTE *FTDib::GetData()
    160. {
    161.     return m_pData;
    162. }

    163. BYTE *FTDib::GetData2()
    164. {
    165.     if(GetRGB())
    166.     {
    167.         m_pData2 = m_pData;
    168.     }
    169.     return m_pData2;
    170. }

    171. //---------------------------------------------
    172. // 取得图像颜色表指针
    173. //---------------------------------------------
    174. RGB_Element * FTDib::GetRGB()
    175. {
    176.     return m_pRGB;
    177. }

    178. //---------------------------------------------
    179. // 取得图像信息指针
    180. //---------------------------------------------
    181. BitmapInfo * FTDib::GetInfo()
    182. {
    183.     return m_pBitmapInfo;
    184. }

    185. //---------------------------------------------
    186. // 调色板大小
    187. //---------------------------------------------
    188. WORD FTDib::PaletteSize(BYTE *lpDib)
    189. {
    190.     return (DIBNumColors(lpDib) * sizeof(RGB_Element));
    191. }

    192. //---------------------------------------------
    193. // 取得图像颜色数
    194. //---------------------------------------------
    195. WORD FTDib::DIBNumColors(BYTE *lpDib)
    196. {
    197.     WORD wBitCount;
    198.     wBitCount = ((BitmapInfoHeader*)lpDib)->biBitPerPixel;
    199.     switch(wBitCount)
    200.     {
    201.     case 1:
    202.         return 2;
    203.     case 4:
    204.         return 16;
    205.     case 8:
    206.         return 256;
    207.     default:
    208.         return 0;
    209.     }
    210. }

    211. //---------------------------------------------
    212. // 保存文件
    213. //---------------------------------------------
    214. void FTDib::SaveFile(const char* filename)
    215. {
    216.     ofstream dibFile;
    217.     dibFile.open(filename, ios::out | ios::binary);

    218.     dibFile.write((char*)m_pBitmapFileHeader,sizeof(BitmapFileHeader));
    219.     cout<<"写入文件头完毕"<<endl;
    220.     dibFile.write((char*)m_pBitmapInfoHeader,sizeof(BitmapInfoHeader));
    221.     cout<<"写入信息头完毕"<<endl;
    222.     dibFile.write((char*)pDib,size);
    223.     dibFile.close();
    224. }
    225. //---------------------------------------------
    226. // 基于LSB的信息隐藏
    227. //---------------------------------------------
    228. int FTDib::LSBCoder(const char* textFileName,int b)
    229. {
    230.     ifstream textFile;
    231.     textFile.open(textFileName,ios::in | ios::binary);
    232.     textFile.seekg(0,textFile.end);
    233.     DWORD textFileLength = textFile.tellg();
    234.     //判断位图是否够存储隐藏的信息
    235.     DWORD colorTableSize = m_numberOfColors * sizeof(RGB_Element);
    236.     if((size - colorTableSize)<textFileLength*8)
    237.     {
    238.         return -1; //不够隐藏
    239.     }


    240.     BYTE* pTextFile = new BYTE[textFileLength+1];
    241.     cout<<"隐藏时文件长度:"<<textFileLength<<endl;
    242.     textFile.seekg(0,textFile.beg);
    243.     textFile.read((char*)pTextFile,textFileLength);
    244.     textFile.close();

    245.     BYTE textData;
    246.     for(int i=0,k=0; i< textFileLength; ++i)
    247.     {
    248.         for(int j=0; j<8; ++j)
    249.         {
    250.             textData = pTextFile[i]>>j;
    251.             textData = textData&0x01;
    252.             if(textData==0)
    253.             {
    254.                 //pDib[k+32] = pDib[k+32]&0xfe;
    255.                 E0(&(pDib[k+32]),b);
    256.             }
    257.             else
    258.             {
    259.                 //pDib[k+32] = pDib[k+32]|0x01;
    260.                 E1(&(pDib[k+32]),b);
    261.             }
    262.             ++k;
    263.         }
    264.     }
    265.     cout<<"信息隐藏完毕"<<endl;
    266.     //在前四个字节中写入text文件数据长度
    267.     DWORD length;
    268.     for(int i=0; i<32; ++i)
    269.     {
    270.         length = textFileLength>>i;
    271.         length = length&0x00000001;
    272.         if(length==0)
    273.         {
    274.             pDib[i] = pDib[i]&0xfe;
    275.         }
    276.         else
    277.         {
    278.             pDib[i] = pDib[i]|0x01;
    279.         }
    280.     }

    281.     return 0;


    282. }

    283. //---------------------------------------------
    284. // 解码基于LSB的信息隐藏
    285. //---------------------------------------------
    286. void FTDib::LSBDecoder(const char* textFileName,int b)
    287. {
    288.     DWORD length = 0x00000000;
    289.     BYTE bit;
    290.     //获取txt文件长度
    291.     for(int i=0; i<32; ++i)
    292.     {
    293.         bit = pDib[i]&0x01;
    294.         if(bit==0)
    295.         {
    296.             length = length&0x7fffffff;
    297.         }
    298.         else
    299.         {
    300.             length = length|0x80000000;
    301.         }
    302.         if (i<31)    length = length>>1;
    303.     }

    304.     cout<<"解码时文件长度:"<<length<<endl;
    305.     //开始解码
    306.     BYTE* pTextFile = new BYTE[length];
    307.     BYTE textData;
    308.     for(int i=0,k=0; i<length*8; ++i)
    309.     {
    310.         if(i && i%8==0){++k;}
    311.         //textData = pDib[i+32]&0x01;
    312.         textData = D(&(pDib[i+32]),b);
    313.         if(textData==0)
    314.         {
    315.             pTextFile[k] = pTextFile[k]&0x7f;
    316.             //D0(&(pTextFile[k]),b);
    317.         }
    318.         else
    319.         {
    320.             pTextFile[k] = pTextFile[k]|0x80;
    321.             //D1(&(pTextFile[k]),b);
    322.         }
    323.         if (i%8 != 7) pTextFile[k] = pTextFile[k]>>1;
    324.     }

    325.     cout<<"解码完毕"<<endl;

    326.     ofstream textFile;
    327.     textFile.open(textFileName,ios::out | ios::binary);
    328.     textFile.write((char*)pTextFile,length);
    329.     textFile.close();
    330.     delete pTextFile;
    331. }

    332. void FTDib::E0(BYTE* data,int b)
    333. {
    334.     switch(b)
    335.     {
    336.         case 0: *data&=0xfe;break;
    337.         case 1: *data&=0xfd;break;
    338.         case 2: *data&=0xfb;break;
    339.         case 3: *data&=0xf7;break;
    340.         case 4: *data&=0xef;break;
    341.         case 5: *data&=0xdf;break;
    342.         case 6: *data&=0xbf;break;
    343.         case 7: *data&=0x7f;break;
    344.     }
    345.     //return *data;
    346. }
    347. void FTDib::E1(BYTE* data,int b)
    348. {
    349.     switch(b)
    350.     {
    351.     case 0: *data|=0x01;break;
    352.     case 1: *data|=0x02;break;
    353.     case 2: *data|=0x04;break;
    354.     case 3: *data|=0x08;break;
    355.     case 4: *data|=0x10;break;
    356.     case 5: *data|=0x20;break;
    357.     case 6: *data|=0x40;break;
    358.     case 7: *data|=0x80;break;
    359.     }
    360.     //return *data;
    361. }
    362. BYTE FTDib::D(BYTE* data,int b)
    363. {
    364.     switch(b)
    365.     {
    366.     case 0: *data&=0x01;break;
    367.     case 1: *data&=0x02;break;
    368.     case 2: *data&=0x04;break;
    369.     case 3: *data&=0x08;break;
    370.     case 4: *data&=0x10;break;
    371.     case 5: *data&=0x20;break;
    372.     case 6: *data&=0x40;break;
    373.     case 7: *data&=0x80;break;
    374.     }
    375.     return *data;
    376. }
    377. /*
    378. void FTDib::D1(BYTE* data,int b)
    379. {
    380.     switch(b)
    381.     {
    382.     case 0: *data|=0x80;break;
    383.     case 1: *data|=0x40;break;
    384.     case 2: *data|=0x20;break;
    385.     case 3: *data|=0x10;break;
    386.     case 4: *data|=0x08;break;
    387.     case 5: *data|=0x04;break;
    388.     case 6: *data|=0x02;break;
    389.     case 7: *data|=0x01;break;
    390.     }
    391.     //return *data;
    392. }*/

    me;
  64.             dib.LSBDecoder(filename,b);
  65.             break;
  66.         case 3:
  67.             id = 0;
  68.             break;
  69.         default:
  70.             cout<<">>>>输入不符合要求<<<<"<<endl;
  71.         }
  72.     }
  73.     system("pause");
  74.     delete filename;
  75.     return 0;
  76. }

点击(此处)折叠或打开

  1. /*********************************************
  2. * 功能:处理bmp图象
  3. * 作者:foxtail
  4. * 构建日期: 2008/3/4
  5. * 修改作者:erlingmusan
  6. * 构建日期: 2012/4/13
  7. *********************************************/
  8. #ifndef _ED_H
  9. #define _ED_H
  10. #include<string>
  11. using namespace std;

  12. #pragma pack(1)
  13. //-------------------------------------------
  14. // 常用类型
  15. //-------------------------------------------
  16. typedef unsigned short WORD;
  17. typedef unsigned long DWORD;
  18. typedef unsigned char BYTE;
  19. //-------------------------------------------
  20. // bmp文件头
  21. //-------------------------------------------
  22. struct BitmapFileHeader /* bf */
  23. {
  24.     WORD     bfType; //位图文件类型,必须为BMP
  25.     DWORD     bfSize; //位图文件大小,以字节为单位
  26.     WORD     bfReserved; //位图文件保留字,必须为0
  27.     WORD     bfReservedEx; //位图文件保留字,必须为0
  28.     DWORD     bfOffBits; //位图数据的起始位置,相对于位图文件头
  29. }; //的偏移量,以字节为单位
  30. //------------------------------------------
  31. // bmp信息头
  32. //------------------------------------------
  33. struct BitmapInfoHeader /* bi */
  34. {
  35.     DWORD     biSize; //本结构所占字节数
  36.     long     biWidth; //位图宽度,以像素为单位
  37.     long     biHeight; //位图高度,以像素为单位
  38.     WORD     biPlanes; //位图设备级别为1
  39.     WORD     biBitPerPixel; //每像素所需的位数,必须是1(双色),4(16色),8(256色)或24(真彩色)
  40.     DWORD     biCompression; //压缩类型,0(不压缩),1(BI_REL8),2(BI_REL4)
  41.     DWORD     biDataSize; //位图数据区的大小,以字节为单位
  42.     long     biHResolution; //水平分辨率
  43.     long      biVResolution; //垂直分辨率
  44.     DWORD     biColors; //实际使用颜色数
  45.     DWORD biImprotantColors; //显示过程中的重要颜色数
  46. };
  47. //------------------------------------------
  48. // 颜色表元素结构
  49. //------------------------------------------
  50. struct RGB_Element /* rgb */
  51. {
  52.     BYTE    rgbBlue; //蓝色亮度
  53.     BYTE    rgbGreen; //绿色亮度
  54.     BYTE    rgbRed; //红色亮度
  55.     BYTE    rgbReserved; //保留,必须为0
  56. };
  57. //------------------------------------------
  58. // bmp信息
  59. //------------------------------------------
  60. struct BitmapInfo /* bmi */
  61. {
  62.     BitmapInfoHeader    bmiHeader; //位图信息头
  63.     RGB_Element     bmiColors[1]; //颜色表
  64. };

  65. //-------------------------------------------
  66. class FTDib
  67. {
  68. private:
  69.     RGB_Element            *m_pRGB; //指向颜色表
  70.     BYTE                *m_pData,*m_pData2; //
  71.     unsigned int        m_numberOfColors; //颜色数
  72.     bool                m_valid; //
  73.     BitmapFileHeader    *m_pBitmapFileHeader; //指向文件头
  74.     BitmapInfoHeader    *m_pBitmapInfoHeader; //指向信息头
  75.     BitmapInfo            *m_pBitmapInfo; //
  76.     int                    byBitCount; //
  77.     DWORD                dwWidthBytes; //
  78.     BYTE                *pDib; //指向数据区
  79.     DWORD                size; //颜色表和数据区的总大小
  80. public:
  81.     FTDib();
  82.     ~FTDib();
  83.     //基本属性和操作
  84.     char            m_fileName[256]; //需要载入的文件的名字
  85.     char*            GetFileName();
  86.     bool            IsValid();
  87.     DWORD            GetSize();
  88.     unsigned int    GetWidth();
  89.     unsigned int    GetHeight();
  90.     unsigned int    GetNumberOfColors();
  91.     RGB_Element*    GetRGB();
  92.     BYTE*            GetData();
  93.     BYTE*            GetData2();
  94.     DWORD            GetDibWidthBytes();
  95.     BitmapInfo*        GetInfo();
  96.     WORD            PaletteSize(BYTE*lpDib);
  97.     WORD            DIBNumColors(BYTE*lpDib);
  98.     void             SaveFile(const char* filename);
  99.     void            LoadFile(const char* dibFileName);
  100.     //扩展属性和操作
  101.     //void            GradeToRGB();
  102.     //void            RGBToGrade();
  103.     int LSBCoder(const char* textFileName, int b); //LSB信息隐藏
  104.     void LSBDecoder(const char* dibFileName,int b); //读取信息

  105.     void E0(BYTE* data,int b);
  106.     void E1(BYTE* data,int b);
  107.     BYTE D(BYTE* data,int b);
  108.     //void D1(BYTE* data,int b);
  109. };
  110. #endif//_ED_H

阅读(1670) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~