Jump to content

Recommended Posts

Posted (edited)

I just starting writing my first AutoIt3 application with some various pieces of scripts I've found surfing the forums. Essentially, I am just toying with Steganography, but I want to incorporate Blowfish Encryption so that the data being encoded into the image is Encrypted with Blowfish first. The encryption process seems to be working fine, however when I decrypt the message and try to fetch the encoded message I get an empty var.

Here is the script:

#Region 
#AutoIt3Wrapper_Icon=icon.ico
#EndRegion 

#include <GDIPlus.au3>
#include <String.au3>
#include <Color.au3>
#include <Math.au3>
#include <Blowfish.au3>


;$cipher=Blowfish($key,$text,0)
;MsgBox(0,"This encrypted text is",$cipher)
;$decrypted=Blowfish($key,$cipher,1)
;MsgBox(0,"There and back agian:",$decrypted)

;Main code begins
;$key = "12345678"
$passwd = InputBox("Getting started.", "Enter your name.", "", " M", 100, 100, 220, 220)
$passwd &= @MON
$passwd &= @MDAY
$passwd &= @YEAR
;MsgBox(0, "Password", $passwd)
$password = InputBox("Password Required!", "Please enter your password.", "", "*")
If $password = $passwd Then
    ;$firstPosition = MouseGetPos()
    ;MsgBox(0, "position 1", $firstPosition)
    Sleep(1000)
    ;$secondPosition = MouseGetPos()
    ;If $secondPosition = $firstPosition Then
        $choice = MsgBox(2, "ERROR!!!", "There has been an error!")
        
        Select
            Case $choice = 3
                $key = InputBox("Desired Key", "The key must be multiples of eight (8) characters", "", "X")
                $InputImage = FileOpenDialog("Input Image", @ScriptDir, "PNGs (*.png)")
                $OutputImage = FileSaveDialog("Output Image", @ScriptDir, "PNGs (*.png)")
                $text = InputBox("Message to encode", "This is the message you want to encrypt into our steg.", "", "")
                MsgBox(0, "key", $key)
                $cipher=Blowfish($key,$text,0)
                $DataToEncode = $cipher
                EncodeImage($DataToEncode, $InputImage, $OutputImage)
            Case $choice = 4
                $key = InputBox("Desired Key", "The key used to encoded this message", "", "X")
                $ImageToDecode = FileOpenDialog("What should I decode?", @ScriptDir, "PNGs (*.png)")
                $DecodedImageData = BinDecodeString(DecodeImage($ImageToDecode))
                $cipher = $DecodedImageData
                MsgBox(0, "data", $cipher)
                MsgBox(0, "key", $key)
                $text = Blowfish($key,$cipher,1)
                MsgBox(0, "text", $text)
            Case $choice = 2
                Exit
            EndSelect
        ;Else
        ;   Exit
        ;Endif
    Else 
        MsgBox(4096, "", "Exiting..") 
        Exit
    EndIf
    
    
;Main code ends

;Extracts our bits
Func DecodeImage($InputImage)
    $iPosX = 0
    $iPosY = 0
    $results = ""

    _GDIPlus_Startup()

    $hImage = _GDIPlus_ImageLoadFromFile($InputImage)
    $iXmax = _GDIPlus_ImageGetWidth($hImage)
    $iYmax = _GDIPlus_ImageGetHeight($hImage)

    For $iPosY = 0 To $iYmax - 1
        For $iPosX = 0 To $iXmax - 1
            $colval = _GDIPlus_BitmapGetPixel($hImage, $iPosX, $iPosY)
            $aColor = _ColorGetRGB(Dec($colval))
            $results = $results & GetLSB($aColor[0])
            $results = $results & GetLSB($aColor[1])
            $results = $results & GetLSB($aColor[2])
            _GDIPlus_BitmapSetPixel($hImage, $iPosX, $iPosY, RGBIt(_ColorSetRGB($aColor)))
            TrayTip("LSBSteg", "On Pixle " & $iPosX & "x" & $iPosY & " max " & $iXmax & "x" & $iYmax, 0)
            If StringRight($results, 16) = "0000000000000000" Then ExitLoop 2 ;exit both for loops
        Next

    Next
    Return $results
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc   ;==>DecodeImage

;Changes the image to encode our bits
Func EncodeImage($StringToHide, $InputImage, $OutputImage)
    $iPosX = 0
    $iPosY = 0
    $BinStringToHide = BinEncodeString($StringToHide) & "0000000000000000" ;double null terminate
    _GDIPlus_Startup()

    $hImage = _GDIPlus_ImageLoadFromFile($InputImage)
    $iXmax = _GDIPlus_ImageGetWidth($hImage)
    $iYmax = _GDIPlus_ImageGetHeight($hImage)

    For $iPosY = 0 To $iYmax - 1
        For $iPosX = 0 To $iXmax - 1
            $colval = _GDIPlus_BitmapGetPixel($hImage, $iPosX, $iPosY)
            $aColor = _ColorGetRGB(Dec($colval))
            $r = PopBinValue($BinStringToHide)
            $b = PopBinValue($BinStringToHide)
            $g = PopBinValue($BinStringToHide)
            $aColor[0] = SetLSB($aColor[0], $r)
            $aColor[1] = SetLSB($aColor[1], $b)
            $aColor[2] = SetLSB($aColor[2], $g)
            _GDIPlus_BitmapSetPixel($hImage, $iPosX, $iPosY, RGBIt(_ColorSetRGB($aColor)))
            TrayTip("LSBSteg", "On Pixle " & $iPosX & "x" & $iPosY & " BinLen " & StringLen($BinStringToHide), 0)
            If StringLen($BinStringToHide) < 1 Then ExitLoop 2 ;exit both for loops
        Next
    Next

    _GDIPlus_ImageSaveToFile($hImage, $OutputImage)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc   ;==>EncodeImage

;Setsu the valus of the least significant bit
Func SetLSB($arg, $bit)
    If $bit = "1" Then
        $results = BitOR($arg, 1) ; 1 as LSB
    Else
        $results = BitAND($arg, 254) ; 0 as LSB
    EndIf
    Return $results
EndFunc   ;==>SetLSB

;Gives you the value of the least significant bit
Func GetLSB($arg)
    ;msgbox(0,"",$arg)
    $results = _MathCheckDiv($arg, 2)
    If $results = 2 Or $results = -1 Then $results = 0
    ;msgbox(0,"",$results)
    Return $results
EndFunc   ;==>GetLSB

;Had to use this to implement a stack, it made the code logic easier
Func PopBinValue(ByRef $arg)
    $results = StringLeft($arg, 1)
    $arg = StringRight($arg, StringLen($arg) - 1)
    Return $results
EndFunc   ;==>PopBinValue

; Turns decimal color code into a hex encoded string
Func RGBIt($deccolor)
    Return Hex($deccolor, 6)
EndFunc   ;==>RGBIt

;Just get the color of a pixel
Func _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
    Local $tArgb, $pArgb, $aRet
    $tArgb = DllStructCreate("dword Argb")
    $pArgb = DllStructGetPtr($tArgb)
    $aRet = DllCall($ghGDIPDll, "int", "GdipBitmapGetPixel", "hwnd", $hBitmap, "int", $iX, "int", $iY, "ptr", $pArgb)
    Return Hex(DllStructGetData($tArgb, "Argb"), 6)
EndFunc   ;==>_GDIPlus_BitmapGetPixel
; Vossen

;Just set the color of a pixel
Func _GDIPlus_BitmapSetPixel($hBitmap, $iX, $iY, $iArgb)
    Local $aRet
    $iArgb = "0xFF" & $iArgb
    $aRet = DllCall($ghGDIPDll, "int", "GdipBitmapSetPixel", "hwnd", $hBitmap, "int", $iX, "int", $iY, "dword", $iArgb)
    Return
EndFunc   ;==>_GDIPlus_BitmapSetPixel
;based on a post from Malkey

;Loops though the sting, turing the Binary to ASCII
Func BinDecodeString($arg)
    $results = ""
    For $i = 1 To StringLen($arg) Step 8
        $results = $results & Byte2Asc(StringMid($arg, $i, 8))
    Next
    Return $results
EndFunc   ;==>BinDecodeString

Func Byte2Asc($arg)
    $results = 0
    If StringMid($arg, 1, 1) = 1 Then $results = $results + 128
    If StringMid($arg, 2, 1) = 1 Then $results = $results + 64
    If StringMid($arg, 3, 1) = 1 Then $results = $results + 32
    If StringMid($arg, 4, 1) = 1 Then $results = $results + 16
    If StringMid($arg, 5, 1) = 1 Then $results = $results + 8
    If StringMid($arg, 6, 1) = 1 Then $results = $results + 4
    If StringMid($arg, 7, 1) = 1 Then $results = $results + 2
    If StringMid($arg, 8, 1) = 1 Then $results = $results + 1
    Return Chr($results)
EndFunc   ;==>Byte2Asc

;Loops though the sting, turing the ASCII to a series of 1s and 0s
Func BinEncodeString($arg)
    $results = ""
    For $i = 1 To StringLen($arg)
        $results = $results & Asc2Byte(StringMid($arg, $i, 1))
    Next
    Return $results
EndFunc   ;==>BinEncodeString
Func Asc2Byte($arg)
    $tempasc = Asc($arg)
    Local $result = ""
    While ($tempasc >= 1)
        $tempasc /= 2
        If StringIsDigit($tempasc) Then
            $result &= 0
        Else
            $split = StringSplit($tempasc, ".")
            $tempasc = $split[1]
            $result &= 1
        EndIf
    WEnd
    $result = _StringReverse($result)
    While StringLen($result) < 8
        $result = "0" & $result
    WEnd
    Return $result
EndFunc   ;==>Asc2Byte
Edited by shat
Posted

Really hoping someone might be able to help me out with this. I've continued my attempts at trouble shooting it myself, but I can't figure it out. I've thought about switching to the AES/DES algorithm, but I am not familiar with them. Suggestions?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...