[C++] Stéganographie LSB

Download | Vote Up (0) | Vote Down (0)
/* Hide a file into a BMP using the LSB method */
/* If you have any question, ask : necromoine@gmail.com */

#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <string>


using namespace std;


string decBin(int num) // Return size of 16
{
    string binaire("");
    for(int i(15);i>=0;i--){
        if(num >= pow(2,i)){
            binaire.append("1");
            num-= pow(2,i);
        }else{
            binaire.append("0");
        }
    }
    return binaire;
}
int binDec(string bin){ // Binary to integer
    int decimal(0);
    for(int i(bin.size()-1);i>=0;i--){
        if(bin[i]=='1'){
            decimal+=pow(2,bin.size()-1-i);
        }
    }
    return decimal;
}
string fileSize(fstream& encrypt){ // Return file to hide size
    int size;
    encrypt.seekg(0, ios::end);
    size = encrypt.tellg();
    encrypt.seekg(ios::beg);
    return decBin(size);
}
int getSize(fstream& decrypt){ // Return hidden file size
    decrypt.seekg(54,ios::beg);
    int iSize;
    string sSize(""),bSize;
    for(int i(0);i<8;i++){
         char size;
                 decrypt.read((char *)&size, sizeof(char));
                 iSize = (int)(size%4);
                 bSize = decBin(iSize);
                 sSize.append(bSize.substr(14,2));
         }
    return binDec(sSize);

}
void write(fstream& input, string binary){ // Write binary into input
    int iPixel, iBinary;
    for(int j(0);j<binary.size()/8;j++){
        for(int i(0);i<8;i+=2){
            char fPixel;
            input.read((char *)&fPixel, 1); 
                iPixel = (int)(fPixel-(fPixel%4)); 
                iBinary = binDec(binary.substr(i+8*j,2));
                iPixel+=iBinary;
                input.seekg(-1, ios::cur); 
                input.write((char *)&iPixel,sizeof(char)); 
        }
    }
}

void menu(){
    cout << "   Stegano Tool by Necromoine  " << endl << endl;
    cout << "     Hide any file in a BMP     " << endl;
    cout << "   ##########################   " << endl << endl;
    cout << "Usage : "<< endl;
    cout << "Encrypt A into B : ./steg e A B" << endl;
    cout << "Decrypt A into B : ./steg d A B" << endl << endl;
    cout << "Warning, hidden file must be under 65535 octets" << endl << endl;
}
int main(int argc,char *argv[]){
    
    if(argc != 4){
        menu();
        return 0;
    }
    string binary;
    if(argv[1][0] == 'e'){
        cout << argv[3] << endl << argv[2] << endl;
        int letter;
        fstream input(argv[3], fstream::in | fstream::out | fstream::binary);
        fstream encrypt(argv[2], fstream::in | fstream::out );
        input.seekg(54, fstream::beg);
        write(input,fileSize(encrypt)); 
        while(!encrypt.eof()){
            cout << encrypt.tellg() << endl;
            unsigned char caract;
            encrypt.read((char *)&caract, 1);
            letter = (int)caract;
            binary = decBin(letter).substr(8,8);
            write(input, binary);
        }
        cout << "Encryption done, check " << argv[3] << endl;
    }
    else if(argv[1][0] == 'd'){
        string data("");
        int iPixel, caract, iSize, i(0);
        fstream encrypt(argv[2], fstream::in | fstream::out | fstream::binary);
        fstream output(argv[3], fstream::trunc | fstream::out | fstream::in );
        encrypt.seekg(54,fstream::beg);
        iSize = getSize(encrypt);
        while(i<iSize){
            unsigned char rPixel;
            encrypt.read((char *)&rPixel, 1);
            iPixel = (int)(rPixel%4);
            binary = decBin(iPixel);
            data.append(binary.substr(14,2));
            if(data.size() == 8){
                caract = binDec(data);
                output.write((char *)&caract, 1);
                data = "";
                i++;
            }
        }
        cout << "Decryption done, check " << argv[3] << endl;
    }else{
        menu();
        return 0;
    }
    return 0;
} 

ex0ns


Be the first to give feedback !

Please login to comment !