Jump to content

A simple cipher example


Andreik
 Share

Recommended Posts

I write a simple cipher example. I hope you like it.

Func XOR($STRING,$NUM=5)
    Local $INDEX,$RESULT=""
    For $INDEX = 1 To StringLen($STRING)
        $RESULT &= Chr(BitXOR(Asc(StringMid($STRING,$INDEX,1)),$NUM))
    Next
    Return $RESULT
EndFunc

Func Cipher($DATA,$PASSWORD,$OPT=0)
    Local $INDEX,$RESULT="",$L1,$L2,$CARRY,$PWD
    $L1 = StringLen($DATA)
    $L2 = StringLen($PASSWORD)
    If $L1 < $L2 Then SetError(1,0,0)
    $CARRY = Mod($L1,$L2)
    For $INDEX = 1 To Int($L1/$L2)
        $PWD &= $PASSWORD
    Next
    $PWD &= StringLeft($PASSWORD,$CARRY)
    For $INDEX = 1 To $L1
        If $OPT = 0 Then
            $RESULT &= Chr(Asc(StringMid($DATA,$INDEX,1))+Asc(StringMid($PWD,$INDEX,1)))
        ElseIf $OPT = 1 Then
            $RESULT &= Chr(Asc(StringMid($DATA,$INDEX,1))-Asc(StringMid($PWD,$INDEX,1)))
        EndIf
    Next
    Return $RESULT
EndFunc

$GUI = GUICreate("Encrypt/Decrypt",400,235,-1,-1,0x16C80000)
$EDIT1 = GUICtrlCreateEdit("This is a test",5,5,190,195,BitOR(0x00200000,0x00100000))
$EDIT2 = GUICtrlCreateEdit("",205,5,190,195,BitOR(0x00200000,0x00100000))
$BUTTON1 = GUICtrlCreateButton("->",170,210,20,20)
$BUTTON2 = GUICtrlCreateButton("<-",210,210,20,20)
$ENC = GUICtrlCreateButton("ENCRYPT",10,210,90,20)
$DEC = GUICtrlCreateButton("DECRYPT",300,210,90,20)
GUISetState(@SW_SHOW,$GUI)

While 1
    $MSG = GUIGetMsg()
    Switch $MSG
        Case $BUTTON1
            GUICtrlSetData($EDIT2,GUICtrlRead($EDIT1))
            GUICtrlSetData($EDIT1,"")
        Case $BUTTON2
            GUICtrlSetData($EDIT1,GUICtrlRead($EDIT2))
            GUICtrlSetData($EDIT2,"")
        Case $ENC
            GUICtrlSetData($EDIT2,XOR(Cipher(GUICtrlRead($EDIT1),"TEST",0),5))
        Case $DEC
            GUICtrlSetData($EDIT2,Cipher(XOR(GUICtrlRead($EDIT1),5),"TEST",1))
        Case -3
            Exit
    EndSwitch
    Sleep(20)
WEnd

When the words fail... music speaks.

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...