Jump to content

Baconian cipher example


 Share

Recommended Posts

This example is a slightly modified version of Francis Bacon's cipher, and is the result of me viewing this wikipedia page.

Like some of my previous scripts, this was hid away from daylight before it was finally posted.

Posted Image

Global $ComboOptions="Don't Modify Cover|Don't Modify Message|Don't cut any data|Don't pad any data"
Global $ComboOptionsa=StringSplit($ComboOptions,'|')
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("CoverText Encryptor", 426, 65, 193, 115)
GUISetOnEvent(-3,'_exi')
$Input1 = GUICtrlCreateInput("Bacon's cipher or the Baconian cipher is a method of steganography (a method of hiding a secret message as opposed to a true cipher) devised by Francis Bacon. ", 58, 0, 259, 20)
$Combo1 = GUICtrlCreateCombo('',58+259,0,426-(58+259),20,0x0003)
GUICtrlSetData(-1,$ComboOptions,$ComboOptionsa[3])
$Input2 = GUICtrlCreateInput("Text To Encrypt", 58, 20, 259, 20)
$Input3 = GUICtrlCreateInput("", 58, 40, 259, 20)
$Button1 = GUICtrlCreateButton("Encrypt", 317, 20, 106, 20, 0)
GUICtrlSetOnEvent(-1,'_enc')
$Button2 = GUICtrlCreateButton("Decrypt", 317, 40, 106, 20, 0)
GUICtrlSetOnEvent(-1,'_dec')
$Label1 = GUICtrlCreateLabel("Cover Text", 1, 2, 56, 17)
$Label2 = GUICtrlCreateLabel("Message", 2, 22, 55, 17)
$Label3 = GUICtrlCreateLabel("Encrypted", 1, 42, 52, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $ShowErrors=True
While 1
    Sleep(100)
WEnd
Func _enc()
    _dis(128)
    Local $Cover, $Text, $Enc
    Local $CoverMode=1, $MessageMode=1
    $Mode=GUICtrlRead($Combo1)
    Switch $Mode
        Case $ComboOptionsa[1]
            $CoverMode=0
        Case $ComboOptionsa[2]
            $MessageMode=0
        Case $ComboOptionsa[3]
            ;both are 1. default for this func
        Case $ComboOptionsa[4]
            $CoverMode=0
            $MessageMode=0
    EndSwitch
    $Cover=GUICtrlRead($Input1)
    $Text=GUICtrlRead($Input2)
    $Enc=_CoverText_Encrypt($Cover,$Text,$CoverMode,$MessageMode)
    GUICtrlSetData($Input3,$Enc)
    _dec(); so that the second box is changed to padded/cut message
    _dis(64)
EndFunc
Func _dec()
    _dis(128)
    Local $Cover, $Text, $Enc
    $Enc=GUICtrlRead($Input3)
    $Cover=StringLower($Enc)
    $Text=_CoverText_Decrypt($Enc)
    GUICtrlSetData($Input2,$Text)
    _dis(64)
EndFunc
Func _dis($s)
    GUICtrlSetState($Input1,$s)
    GUICtrlSetState($Input2,$s)
    GUICtrlSetState($Input3,$s)
    GUICtrlSetState($Button1,$s)
    GUICtrlSetState($Button2,$s)
    GUICtrlSetState($Combo1,$s)
EndFunc
Func _exi()
    Exit
EndFunc






Global $ShowErrors=False
#cs
; EXAMPLE 
#include <String.au3>
$Pledge="i pledge allegiance to the flag of the united states of america, and to the republic, for which it stands, one nation, under god, indivisable, and liberty and justice for all"
$Enc=_CoverText_Encrypt($Pledge,"this is a test")
MsgBox(0,'', '|'&$Enc&'|')
$Dec=_CoverText_Decrypt($Enc)
MsgBox(0,'', '|'&$Dec&'|')
#ce




#cs
_CoverText_Encrypt($Cover,$Text,$CoverMode=1,$MessageMode=1)
DESCRIPTION
    Changes the case of characters in $Cover to save $Text in.
    Up to 7 bits for each character in $Text is saved, supporting the range [0x20, 0x7E]
    Note: the 7 bits used are further mixed to prevent immediate decryption by hand.
PARAMETERS
    $Cover - The fake text to hide the encrypted message in.
    $Text - The Message to hide in $Cover
    $CoverMode   -  A switch to define how to handle what happens when the Cover is too short
        0 - Cut the Message to fit the length of the Cover
        1 - Extend the Cover to fit the length of the Message
    $MessageMode -  A switch to define how to handle what happens when the Cover is too long
        0 - Cut the Cover to fit the length of the Message
        1 - Pad the Message with spaces to use the entire Cover message
    Mode combinations
        CoverMode,MessageMode - Description
        0,0 - No Padding, Cover is cut to whichever input was shortest
        0,1 - No Cover modification, Message length is dependent on Cover length
        1,0 - No Message Modification, Cover length is dependent on Message length
        1,1 - No Cutting, Cover is padded to whicher input was longest
     
RETURN VALUES
    A string containing a version of $Cover that also contains the string $Text encrypted into it's capitalizations.
#ce






Func _CoverText_Encrypt($Cover,$Text,$CoverMode=1,$MessageMode=1)
    ConsoleWrite("+>---CoverText---<+"&@CRLF)
    $Cover=StringLower($Cover)
    Local $Chr, $Asc, $i, $ii, $Val, $Pos, $Asc
    Local $CurPos=1
    Local $Cover2=$Cover;keep an original copy of Cover
    Local $Extended=False
    Local $TextX=StringLen($Text)
    Local $CoverX=StringLen($Cover)
    Local $TextA=StringSplit($Text,'')
    Local $CoverA=StringSplit($Cover,'')
    $Cover=''
    ConsoleWrite(">> Processing Message, "&(7*$TextX)&" letters required for completeness."&@CRLF)
    For $i=1 To $TextX
        $Val=Asc($TextA[$i])
        $Val=_CoverText_7BitSwap($Val)
        For $ii=0 To 6
            $Pos=$CurPos+$ii
            If $Pos>$CoverX Then
                $Pos-=1
                If $CoverMode=1 Then
                    If $Extended=False Then
                        _CoverText_Error("Cover shorter than Message (Cover will be extended - The Cover must be 7x as long as the Message)")
                        $Cover&=' | '
                    EndIf
                    $Extended=True
                    $Chr=Chr(Random(0x61,0x7A,1))
                Else
                    _CoverText_Error("Cover shorter than Message (Message will be cut short) @"&$Pos)
                    ExitLoop 2
                EndIf
            Else
                $Chr=$CoverA[$Pos]
            EndIf
            $Asc=Asc($Chr)
            Switch $Asc
                Case 0x61 To 0x7A
                    If BitAnd($Val,2^$ii) Then
                        $Chr=Chr($Asc-0x20)
                    EndIf
                Case Else
                    $CurPos+=1
                    $ii-=1
            EndSwitch
            $Cover&=$Chr
            If $ii=6 Then
                $CurPos=$Pos+1;prepare for the next loop
            EndIf
        Next
    Next
    ConsoleWrite("End: "&$ii&@CRLF)
    If $Pos<$CoverX Then
        If $MessageMode=1 Then
            _CoverText_Error("Cover longer than Message (Message will be padded)")
            $Cover&=StringMid($Cover2,$Pos+1)
        Else
            _CoverText_Error("Cover longer than Message (Cover will be cut short)")
        EndIf
    EndIf
    Return $Cover
EndFunc



Func _CoverText_Error($t)
    If $ShowErrors Then MsgBox(0,'CoverText Warning',$t)
    ConsoleWrite("!>        "&$t&@CRLF)
EndFunc






Func _CoverText_Decrypt($Cover)
    ConsoleWrite("+>---CoverText---<+"&@CRLF)
    Local $CoverA=StringSplit($Cover,'')
    Local $CoverX=UBound($CoverA)-1
    Local $Bit=0
    Local $Val=0
    Local $Text=''
    Local $Chr, $Asc, $i
    ConsoleWrite(">> Processing Message"&@CRLF)
    For $i=1 To $CoverX
        $Chr=$CoverA[$i]
        $Asc=Asc($Chr)
        Switch $Asc
            Case 0x61 To 0x7A;0's
                ;$Val+=(2^$Bit)*0
            Case 0x41 To 0x5A;1's
                $Val+=(2^$Bit);*1
            Case Else
                ContinueLoop
        EndSwitch
        $Bit+=1
        If $Bit>6 Then
            $Val=_CoverText_7BitUnSwap($Val)
            $Text&=Chr($Val)
            $Bit=0
            $Val=0
        EndIf
    Next
    If $Bit>0 Then ConsoleWrite("!>     Bit pos non-zero (Cover Letter # not 7x Message Length ?) @"&$i&@CRLF)
    Return $Text
EndFunc




Func _CoverText_7BitSwap($Val)
    Local $Val2=0
    Local $NewPlace[7]
    $NewPlace[0]=4
    $NewPlace[1]=3
    $NewPlace[2]=0
    $NewPlace[3]=6
    $NewPlace[4]=1
    $NewPlace[5]=5
    $NewPlace[6]=2
    $Val-=0x20
    For $i=0 To 6
        If BitAnd($Val,2^$i) Then $Val2+=2^$NewPlace[$i]
    Next
    Return $Val2
EndFunc
Func _CoverText_7BitUnSwap($Val)
    Local $Val2=0
    Local $NewPlace[7]
    $NewPlace[4]=0
    $NewPlace[3]=1
    $NewPlace[0]=2
    $NewPlace[6]=3
    $NewPlace[1]=4
    $NewPlace[5]=5
    $NewPlace[2]=6
    For $i=0 To 6
        If BitAnd($Val,2^$i) Then $Val2+=2^$NewPlace[$i]
    Next
    $Val2+=0x20
    Return $Val2
EndFunc

While this is a modified Bacon's cipher - it works on the same principle, using letter case to store textual information.

(Note: the "Message" box is also the where the result of a Decryption appears)

Edit: Posted code to remove attachment.

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Link to comment
Share on other sites

Here is an example of encoded text with this example:

"lorem IPsuM dOloR SIT ameT, cOnSectetur adIPIsiciNg ELIt, SEd DO eiUsMOD Tempor inciDiDUNt UT lABore et dolorE mAGna aLIQuA. ut EnIM ad Minim Veniam, quis nostrad ... "

Hidden Message: "Have tons of fun! "

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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