JPEG图片的隐写

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ffgcc/article/details/83543854

两个星期前就想要完成基于JPEG-Jsteg算法的图片隐写,然而两个星期过去,平时事情很多,关键这个东西网上相关资料很少,成型的代码更是几乎没有,所以这里先记录一个基于图片结束标志符的隐写(。。。),希望有操作过DCT系数(c/c++语言)的大佬可以留言或者发我邮箱[email protected]

#include<iostream>
#include<fstream>
using namespace std;
class picture
{
    private:
        ifstream ifile_picture;
        ifstream ifile_txt;
        ofstream ofile_picture;
        ofstream ofile_txt;
        int length_ifile_picture,length_ifile_txt;
        char name_ifile_picture[50], name_ifile_txt[50], name_ofile_picture[50],name_ofile_txt[50];
    public:
        void Open(ifstream &ifile,char *name,int &length);
        void M();
        void m();
};
void picture::Open(ifstream &ifile,char *name,int &length)
{
    ifile.open(name,ios::binary|ios::in);
    if (!ifile)
    {
        cout<<"Open "<<name<<" failed!"<<endl;
        return ;
    }
    ifile.seekg(0, ios::end);
    length = ifile.tellg();
    ifile.seekg(0, ios::beg);
}
void picture::M()
{
    char *str;
    cout<<"请输入图片名(需要加后缀名):"<<endl;
    cin>>this->name_ifile_picture;
    Open(this->ifile_picture, this->name_ifile_picture, this->length_ifile_picture);
    str = new char[length_ifile_picture + 5];
    this->ifile_picture.read(str, this->length_ifile_picture);
    while(1)
    {
        if(str[this->length_ifile_picture-2]==-1  && str[this->length_ifile_picture-1]==-39)
            break;   //图像结束标志  FF D9
        else
            this->length_ifile_picture--;
    }
    cout<<"请输入修改后的图片名(需要加后缀名):"<<endl;
    cin>>this->name_ofile_picture;
    this->ofile_picture.open(this->name_ofile_picture,ios::binary|ios::out);
    if (!this->ofile_picture)
    {
        cout<<"Open "<<this->name_ofile_picture<<" failed!"<<endl;
        return ;
    }
    this->ofile_picture.write(str, this->length_ifile_picture);
    this->ifile_picture.close();
    delete []str;
    cout<<"请输入文本名(需要加后缀名):"<<endl;
    cin>>this->name_ifile_txt;
    Open(this->ifile_txt, this->name_ifile_txt, this->length_ifile_txt);
    str = new char[length_ifile_txt + 5];
    this->ifile_txt.read(str, this->length_ifile_txt);
    this->ofile_picture.write(str, this->length_ifile_txt);
    this->ifile_txt.close();
    this->ofile_picture.close();
    delete []str;
    cout<<"成功!"<<endl;
}
void picture::m()		//信息提取
{
    char *str;
    cout<<"请输入图片名(需要加后缀名):"<<endl;
    cin>>this->name_ifile_picture;
    Open(this->ifile_picture, this->name_ifile_picture, this->length_ifile_picture);
    str = new char[length_ifile_picture + 5];
    this->ifile_picture.read(str, this->length_ifile_picture);
    int l=this->length_ifile_picture;
    while(1)
    {
        if(str[l-2]==-1   && str[l-1] == -39)
            break;   //图像结束标志  FF D9
        else
            l--;
    }
    cout<<"请输入信息提取存入文本名(需要加后缀名):"<<endl;
    cin>>this->name_ofile_txt;
    this->ofile_txt.open(this->name_ofile_txt,ios::binary|ios::out);
    if (!this->ofile_picture)
    {
        cout<<"Open "<<this->name_ofile_txt<<" failed!"<<endl;
        return ;
    }
    this->ofile_txt.write(str+l, this->length_ifile_picture-l);
    this->ifile_picture.close();
    this->ofile_txt.close();
    delete []str;
    cout<<"成功!"<<endl;
}
int main()
{
    picture a;
    a.M() ;
    a.m() ;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ffgcc/article/details/83543854