[PHP] Steganography

Results 1 to 6 of 6
  1. #1
    Account Upgraded | Title Enabled! Exiled Hero is offline
    MemberRank
    Nov 2008 Join Date
    Multiple PlacesLocation
    202Posts

    [PHP] Steganography

    PLEASE NOTE: Due to laziness, the class isn't that clean and it could of been coded a lot better.

    This is a class that uses the steganography technique to hide encrypted data within a bitmap image (.BMP).

    Th3 class can hide the data very well in 24 or 32 bit RGB images.

    The class could be used to hide the data in indexed color images, for instance of 256 colors, but that would require adding a few functions to deal with the modifications to the color palette.

    The class implements 4 levels of bit stream encoding. Each level uses an increasing number of bits per each byte in the RGB pixel table. Therefore, the higher level of encoding will hold more bytes of data in the bitmap image.

    Class:

    PHP Code:
    <?php

    class Steganography 
    {
      var 
    $m_errMsg;
      var 
    $m_dataCarrier;
      var 
    $m_data;
      var 
    $m_key;
      var 
    $m_outFile;

      function 
    __construct() 
      {
        
    $this->m_errMsg "";
        
    $this->m_dataCarrier "";
        
    $this->m_data "";
        
    $this->m_key "ExiledHeroBmpCrypt";
        
    $this->m_outFile "";
      }

      function 
    Encrypt$bmpFile$indata$key$outFile$level 1)
      {
        if ( !
    file_exists$bmpFile ) ) 
        {
          
    $this->m_errMsg "The bitmap file '" $bmpFile "' was not found!";
          return 
    false;
        }

        if ( 
    $indata == "" 
        {
          
    $this->m_errMsg "No data to encrypt!";
          return 
    false;
        }

        if ( 
    $outFile == "" 
        {
          
    $this->m_errMsg "The output file was not specified!";
          return 
    false;
        }

        
    $this->m_dataCarrier $bmpFile;
        
    $this->m_data $indata;

        if ( 
    $key != "" $this->m_key $key;
        
    $this->m_outFile $outFile;

        if ( 
    $level == )
          return 
    $this->_encrypt_1();
        else if ( 
    $level == )
          return 
    $this->_encrypt_2();
        else if ( 
    $level == )
          return 
    $this->_encrypt_3();
        else if ( 
    $level == )
          return 
    $this->_encrypt_4();
        else 
        {
          
    $this->m_errMsg "The encryption level is out of range!";
          return 
    false;
        }

      }

      function 
    Decrypt$bmpFile$key$outFile "" 
      {

        if ( !
    file_exists$bmpFile ) ) 
        {
          
    $this->m_errMsg "The bitmap file '" $bmpFile "' was not found!";
          return 
    false;
        }

        if ( 
    $key != "" $this->m_key $key;
        
    $this->m_outFile $outFile;

        
    $this->m_dataCarrier $bmpFile;

        
    $this->m_data file_get_contents$this->m_dataCarrier );

        if ( !
    $this->_get_encryption_info$level$cryptKey$offset$datSize ) ) 
          return 
    false;

        
    $nbytes = ( $level == ) ? : ( ( $level == ) ? : ( ( $level == ) ? ) );

        if ( 
    $offset $datSize $nbytes strlen$this->m_data ) ) 
        {
          
    $this->m_errMsg "The bitmap file '" $this->m_dataCarrier "' contains no encrypted data!" $level ", $nbytes";
          return 
    false;
        }

        
    $data "";    

        for ( 
    $i 0$i $datSize$i++ )
          
    $data chr);

        for ( 
    $i $offset$j 0$i $offset $datSize $nbytes$i += $nbytes$j++ ) 
        {
          if ( 
    $level == 
          {
            for ( 
    $k 0$n 7$k 8$k++, $n-- )
              
    $data$j ] = chrord$data$j ] ) | ( ( ord$this->m_data$i $k ] ) & ) << $n ) );
          }
          else if ( 
    $level == )
          {
            for ( 
    $k 0$n 6$k 4$k++, $n -= )
              
    $data$j ] = chrord$data$j ] ) | ( ( ord$this->m_data$i $k ] ) & ) << $n ) );
          }
          else if ( 
    $level == 
          {
            for ( 
    $k 0$k 3$k++ ) 
            {
              if ( 
    $k == )
                
    $data$j ] = chrord$data$j ] ) | ( ( ord$this->m_data$i ] ) & ) << ) );
              else if ( 
    $k == )
                
    $data$j ] = chrord$data$j ] ) | ( ( ord$this->m_data$i ] ) & ) << ) );
              else
                
    $data$j ] = chrord$data$j ] ) | ( ord$this->m_data$i ] ) & ) );
            }
          }
          else if ( 
    $level == 
          {
            for ( 
    $k 0$k 2$k++ ) 
            { 
              if ( 
    $k == )
                
    $data$j ] = chrord$data$j ] ) | ( ( ord$this->m_data$i ] ) & 15 ) << ) );
              else
                
    $data$j ] = chrord$data$j ] ) | ( ( ord$this->m_data$i ] ) & 15 ) );
            }
          }
        }

        for ( 
    $i 0$j 0$i $datSize$i++, $j = ( $j ) % 32 )
          
    $data$i ] = chrord$data$i ] ) ^ ord$cryptKey$j ] ) );

        if ( 
    $this->m_outFile != "" 
        (
          
    $fp fopen$this->m_outFile"wb" );
          
    fwrite$fp$data$datSize );
          
    fclose$fp );
          return 
    true;
        }

        return 
    $data;
      }

      function 
    GetErrorMessage()
      {
        return 
    "ERROR: " $this->m_errMsg;
      }

      function 
    _encrypt_1() 
      {
        
    $bmpSize filesize$this->m_dataCarrier );
        
    $datSize strlen$this->m_data );
        
    $totalSize = ( 12 $datSize ) * 8;
        
        if ( ( 
    $bmpSize 55 ) < $totalSize 
        {      
          
    $this->m_errMsg "The bitmap file '" $bmpFile "' is too small to carry the input data!";
          return 
    false;
        }

        
    $cryptKey md5$this->m_key );

        
    $cryptData sprintf"SCC1%08x"$datSize );

        
    $bmpBuffer file_get_contents$this->m_dataCarrier );

        for ( 
    $i 0$j 0$i $datSize$i++, $j = ( $j ) % 32 )     
          
    $cryptData . = chrord$this->m_data$i ] ) ^ ord$cryptKey$j ] ) );

        for ( 
    $i 0$j 55$i strlen$cryptData ); $i++, $j += 
        { 
          
    $temp sprintf"%08s"decbinord$cryptData$i ] ) ) );
          
          for ( 
    $k 0$k 8$k++ ) 
          {
            
    $bmpBuffer$k $j ] = chrord$bmpBuffer$k $j ] ) & 254 );
            if ( 
    $temp$k ] == '1' $bmpBuffer$k $j ] = chrord$bmpBuffer$k $j ] ) + );
          }
        }

        
    $fp fopen($ this->m_outFile"wb" );
        
    fwrite$fp$bmpBuffer );
        
    fclose$fp );

        return 
    true;
      }

      function 
    _encrypt_2() 
      {
        
    $bmpSize filesize$this->m_dataCarrier );
        
    $datSize strlen$this->m_data );
        
    $totalSize = ( 12 $datSize ) * 4;
        if ( ( 
    $bmpSize 55 ) < $totalSize 
        {
          
    $this->m_errMsg "The bitmap file '" $this->m_dataCarrier "' is too small to carry the input data!";
          return 
    false;
        }

        
    $cryptKey md5md5$this->m_key ) );

        
    $cryptData sprintf"SCC2%08x"$datSize );

        
    $bmpBuffer file_get_contents$this->m_dataCarrier );

        for ( 
    $i 0$j 0$i $datSize$i++, $j = ( $j ) % 32 
          
    $cryptData .= chrord$this->m_data$i ] ) ^ ord$cryptKey$j ] ) );

        for ( 
    $i 0$j 55$i strlen$cryptData ); $i++, $j += 
        { 
          
    $temp sprintf"%08s"decbinord$cryptData$i ] ) ) );  
          for ( 
    $k 0$k 4$k++ ) 
          {
            
    $bmpBuffer$k $j ] = chrord$bmpBuffer$k $j ] ) & 252 );
            
    $n = ( $temp$k ] == '1' ) ? 0;
            
    $n += ( $temp$k ] == '1' ) ? 0;
            if ( 
    $n != $bmpBuffer$k $j ] = chrord$bmpBuffer$k $j ] ) + $n ); 
          }
        }

        
    $fp fopen$this->m_outFile"wb" );
        
    fwrite$fp$bmpBuffer );
        
    fclose$fp );

        return 
    true;
      }

      function 
    _encrypt_3() 
      {
        
    $bmpSize filesize$this->m_dataCarrier );
        
    $datSize strlen$this->m_data );
        
    $totalSize = ( 12 $datSize ) * 3;
      
        if ( ( 
    $bmpSize 55 ) < $totalSize 
        {
          
    $this->m_errMsg "The bitmap file '" $this->m_dataCarrier "' is too small to carry the input data!";
          return 
    false;
        }

        
    $cryptKey md5md5md5$this->m_key ) ) );

        
    $cryptData sprintf"SCC3%08x"$datSize );  

        
    $bmpBuffer file_get_contents$this->m_dataCarrier );

        for (
    $i 0$j 0$i $datSize$i++, $j = ( $j ) % 32 )
          
    $cryptData .= chrord$this->m_data$i ] ) ^ ord$cryptKey$j ] ) );

        for ( 
    $i 0$j 55$i strlen$cryptData ); $i++, $j += 
        {
          
    $temp sprintf"%08s"decbinord$cryptData$i ] ) ) );
          
          for ( 
    $k 0$k 3$k++ ) 
          {
            if ( 
    $k != 
            {
              
    $bmpBuffer$k $j ] = chrord$bmpBuffer$k $j ] ) & 248 );
              
    $n = ( $temp$k ] == '1' ) ? 0;
              
    $n += ( $temp$k ] ==  '1' ) ? 0;
              
    $n += ( $temp$k ] == '1') ? 0;

              if ( 
    $n != $bmpBuffer$k $j ] = chrord$bmpBuffer$k $j ] ) + $n ); 
            }
            else 
            {
              
    $bmpBuffer$k $j ] = chrord$bmpBuffer$k $j ] ) & 252 );  
              
    $n = ( $temp$k ] == '1' ) ? 0;
              
    $n += ( $temp$k ] == '1' ) ? 0;
              if ( 
    $n != $bmpBuffer$k $j ] = chrord$bmpBuffer$k $j ] ) + $n );
            }
          }
        }

        
    $fp fopen$this->m_outFile"wb" );
        
    fwrite$fp$bmpBuffer );
        
    fclose$fp ); 

        return 
    true;
      }

      function 
    _encrypt_4() 
      {

        
    $bmpSize filesize$this->m_dataCarrier );
        
    $datSize strlen$this->m_data );
        
    $totalSize = ( 12 $datSize ) * 2;
        
        if ( ( 
    $bmpSize 55 ) < $totalSize 
        { 
          
    $this->m_errMsg "The bitmap file '" $this->m_dataCarrier "' is too small to carry the input data!";
          return 
    false;
        }

        
    $cryptKey md5md5md5md5$this->m_key ) ) ) );

        
    $cryptData sprintf"SCC4%08x"$datSize );

        
    $bmpBuffer file_get_contents$this->m_dataCarrier );

        for ( 
    $i 0$j 0$i $datSize$i++, $j = ( $j ) % 32 
          
    $cryptData .= chrord$this->m_data$i ] ) ^ ord$cryptKey$j ] ) );

        for ( 
    $i 0$j 55$i strlen$cryptData ); $i++, $j += 
        {
          
    $temp sprintf"%08s"decbinord$cryptData$i ] ) ) );
          
          for ( 
    $k 0$k 2$k++ ) 
          {
            
    $bmpBuffer$k $j ] = chrord$bmpBuffer$k $j ] ) & 240 );
            
    $n = ( $temp$k ] == '1' ) ? 0;
            
    $n += ( $temp$k 1] == '1' ) ? 0;
            
    $n += ( $temp$k ] == '1' ) ? 0;
            
    $n += ( $temp$k 3] == '1' ) ? 0;
            if ( 
    $n != $bmpBuffer$k $j ] = chrord$bmpBuffer$k $j ] ) + $n );
          }
        }

        
    $fp fopen$this->m_outFile"wb" );
        
    fwrite$fp$bmpBuffer );
        
    fclose$fp );

        return 
    true;
      }

      function 
    _get_encryption_info( &$level, &$cryptKey, &$offset, &$datSize 
      {
        
    $cryptKey md5$this->m_key );

        
    $cryptHeader "";
        for ( 
    $i 0$i 12$i++ )
          
    $cryptHeader chr);

        for ( 
    $i 55$j 7$k 0$i 151$i++ ) 
        {
          
    $cryptHeader$k ] = chrord$cryptHeader$k ] ) | ( ( ord$this->m_data$i ] ) & ) << $j ) );
          
    $j--;
          
          if ( 
    $j 
          {
            
    $j 7;
            
    $k++;
          }
        }

        if ( !
    strcmpsubstr$cryptHeader0), "SCC1" ) )
        {
          
    sscanfsubstr$cryptHeader4), "%x"$datSize );
          
    $level 1;
          
    $offset 151;
          return 
    true;
        }

        
    $cryptKey md5$cryptKey );

        
    $cryptHeader "";

        for ( 
    $i 0$i 12$i++ )
          
    $cryptHeader chr);

        for ( 
    $i 55$j 6$k 0$i 103$i++ ) 
        {
          
    $cryptHeader$k ] = chrord$cryptHeader$k ] ) | ( ( ord$this->m_data$i ] ) & ) << $j ) );
          
    $j -= 2;
          
          if ( 
    $j 
          {
            
    $j 6;
            
    $k++;
          }
        }

        if ( !
    strcmpsubstr$cryptHeader0), "SCC2" ) ) 
        {
          
    sscanfsubstr$cryptHeader4), "%x"$datSize );
          
    $level 2;
          
    $offset 103;
          return 
    true;
        }

        
    $cryptKey md5$cryptKey );

        
    $cryptHeader "";

        for ( 
    $i 0$i 12$i++ )
          
    $cryptHeader chr);

        for ( 
    $i 55$j 0$i 91$i += 3$j++ ) 
        {
          for ( 
    $k 0$k 3$k++ ) 
          {
            if ( 
    $k == )
              
    $cryptHeader$j ] = chrord$cryptHeader$j ] ) | ( ( ord$this->m_data$i ] ) & ) << ) );
            else if ( 
    $k == )
              
    $cryptHeader$j ] = chrord$cryptHeader$j ] ) | ( ( ord$this->m_data$i ] ) & ) << ) );
            else
              
    $cryptHeader$j ] = chrord$cryptHeader$j ] ) | ( ord$this->m_data$i ] ) & ) );
          }
        }

        if ( !
    strcmpsubstr$cryptHeader0), "SCC3" ) ) 
        {
          
    sscanfsubstr$cryptHeader4), "%x"$datSize );
          
    $level 3;
          
    $offset 91;
          return 
    true;
        }

        
    $cryptKey md5$cryptKey );

        
    $cryptHeader "";

        for ( 
    $i 0$i 12$i++ )
          
    $cryptHeader chr);

        for ( 
    $i 55$j 0$i 79$i += 2$j++ ) 
        {
          for ( 
    $k 0$k 2$k++ ) 
          {
            if ( 
    $k == )
              
    $cryptHeader$j ] = chrord($cryptHeader$j ] ) | ( ( ord$this->m_data$i ] ) & 15 ) << ) );
            else
              
    $cryptHeader$j ] = chrord$cryptHeader$j ] ) | ( ord$this->m_data$i ] ) & 15 ) );
            }
          }

        if ( !
    strcmpsubstr$cryptHeader0), "SCC4" ) ) 
        { 
          
    sscanfsubstr$cryptHeader4), "%x"$datSize );
          
    $level 4;
          
    $offset 79;
          return 
    true;
        }

        
    $this->m_errMsg "The bitmap file '" $this->m_dataCarrier "' contains no encrypted data!" $cryptHeader;
        return 
    false;
      }

    }

    /* EOF */
    Command Line example:

    PHP Code:
    <?php

    include("steganography.php");

    $method "";

    if ( 
    $argc )
    {
      if ( 
    strcmp$argv], "-e" ) == $method " Encrypt ";
      else if ( 
    strcmp$argv], "-d" ) == $method " Decrypt ";
    }

    if ( 
    $method == "Encrypt" && $argc >= 
    {
      if ( !
    file_exists$argv] ) ) 
      {
        echo 
    "File '" $argv] . "' was not found!\n";
        exit();
      }
        
      
    $bmpEnc = new BmpCrypt();
      
    $return $bmpEnc->Encrypt$argv], file_get_contents$argv] ), ( $argc ) ? $argv] : ""$argv], ( $argc ) ? $argv] : );
      
      if ( 
    $return == false ) echo $bmpEnc->GetErrorMessage() . "\n";
    }
      
    else if ( 
    $method == "Decrypt" && $argc >= 
    {
      
    $bmpEnc = new Steganography();
      
    $return $bmpEnc->Decrypt($argv[2], ($argc 3) ? $argv[4] : ""$argv[3]);
        
      if ( 
    $return == false ) echo $bmpEnc->GetErrorMessage() . "\n";
    }
    else 
    {
      echo 
    "USAGE:\n";
      echo 
    "   To encrypt data and hide it in a bitmap file:\n";
      echo 
    "      php -f Steg_CmdLine_Example.php -e bitmap_file data_file output_file [key] [level]\n";
      echo 
    "   To decrypt data and recover it from a bitmap file:\n";
      echo 
    "      php -f Steg_CmdLine_Example.php -d bitmap_file output_file [key]\n";
    }

    /* EOF */
    I'll edit this post once I done the webpage example.
    Last edited by Exiled Hero; 22-04-12 at 11:58 PM.


  2. #2
    Vous Pas Reel HotelUnderSeige is offline
    MemberRank
    Nov 2007 Join Date
    ColoradoSpringsLocation
    1,290Posts

    Re: [PHP] Steganography

    I think this is simply genius!
    I do like your work here sire.

  3. #3
    Account Upgraded | Title Enabled! Exiled Hero is offline
    MemberRank
    Nov 2008 Join Date
    Multiple PlacesLocation
    202Posts

    Re: [PHP] Steganography

    Why thank you.

    Still haven't had time to do the web based example but I'll get around to it. I give credits to Burn Notice for the idea.

    I also have written a script that simulates router decision using neural networks.

  4. #4
    Hm. foxx is offline
    MemberRank
    Sep 2006 Join Date
    Czech RepublicLocation
    5,257Posts

    Re: [PHP] Steganography

    Yo nigga, is your name Chao-Chyuan Shih and did you make this 7 years ago?

    BmpCrypt_Class.php at BmpCrypt - Free PHP Code

  5. #5
    Apprentice don456 is offline
    MemberRank
    Jun 2005 Join Date
    On EarthLocation
    17Posts

    Re: [PHP] Steganography

    lol nice find...its the same coding with different class name only...

  6. #6
    Novice Water Dragon is offline
    MemberRank
    Jul 2014 Join Date
    Tornado AlleyLocation
    1Posts

    Re: [PHP] Steganography

    Here is Chao-Chyuan Shih, the original author of BmpCrypt and TxtCrypt...



Advertisement