Jump to content
Sign in to follow this  
derkaderka

Encryption program

Recommended Posts

I am trying to make an encryption program, I got the RC4 encryption part to work, but not the convert string to binary part. Here is my code, please help.

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include <ButtonConstants.au3>
#include <String.au3>
#include <File.au3>
#include <WinAPI.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $selection = ""
Global $error = ""
Global $save = ""
Global $sFile, $hFile, $sText, $nBytes, $tBuffer
Global $readbin = ""


$Form1 = GUICreate("Encrypter", 480, 302, 192, 124)
$Encrypt = GUICtrlCreateButton("Encrypt", 144, 168, 75, 25)
$Decrypt = GUICtrlCreateButton("Decrypt", 392, 40, 75, 25)
$Input1 = GUICtrlCreateInput("", 16, 154, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
GUICtrlSetTip(-1, "Enter Password")
GUICtrlSetCursor (-1, 5)
$Edit1 = GUICtrlCreateEdit("", 16, 8, 201, 137)
GUICtrlSetTip(-1, "Enter string to encrypt")
GUICtrlSetCursor (-1, 5)
$Input2 = GUICtrlCreateInput("", 264, 42, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
GUICtrlSetTip(-1, "Enter Password")
GUICtrlSetCursor (-1, 5)
$Input3 = GUICtrlCreateInput("", 264, 8, 161, 21)
$Button1 = GUICtrlCreateButton("...", 432, 8, 35, 25)
$Input4 = GUICtrlCreateInput("", 16, 184, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
GUICtrlSetTip(-1, "Re-Enter Password")
GUICtrlSetCursor (-1, 5)
$Radio1 = GUICtrlCreateRadio("RC4", 264, 104, 113, 17)
$Radio2 = GUICtrlCreateRadio("Binary", 264, 128, 113, 17)
$Group1 = GUICtrlCreateGroup("Encryption Type", 256, 80, 121, 81)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Progress1 = GUICtrlCreateProgress(16, 264, 449, 25)
$Input5 = GUICtrlCreateInput("", 16, 210, 161, 21)
$Button2 = GUICtrlCreateButton("...", 184, 208, 35, 25)
GUICtrlSetTip(-1, "Choose a save destination")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Radio1
            GUICtrlSetState($Input1, $GUI_ENABLE)
            GUICtrlSetState($Input4, $GUI_ENABLE)
            $selection = "1"
        Case $Radio2
            GUICtrlSetState($Input1, $GUI_DISABLE)
            GUICtrlSetState($Input4, $GUI_DISABLE)
            $selection = "2"
        Case $Encrypt
            GUICtrlSetData($Progress1, 0)
            $error = ""
            If $selection = "2" Then
                If GUICtrlRead($Input5) = "" Then
                    MsgBox(0, "Error", "Error: Must select a destination.")
                EndIf
                If GUICtrlRead($Input5) <> "" Then
                    $readbin = GUICtrlRead($Edit1)
                    continue2()
                EndIf
            Else
                $Read1 = GUICtrlRead($Input1)
                $Read2 = GUICtrlRead($Input4)
                $Read3 = GUICtrlRead($Input5)
                If $Read1 = "" Then
                    MsgBox(0, "Error", "Error: Must enter password.")
                    $error = "1"
                EndIf
                If $Read2 = "" Then
                    MsgBox(0, "Error", "Error: Must re-enter password.")
                    $error = "1"
                EndIf
                If $Read3 = "" Then
                    MsgBox(0, "Error", "Error: Must select destination.")
                    $error = "1"
                EndIf
                If $Read1 <> $Read2 Then
                    MsgBox(0, "Error", "Error: Passwords do not match.")
                    $error = "1"
                EndIf
                If $error = "" Then
                    If $selection = "1" Then
                        continue()
                    EndIf
                    If $selection = "" Then
                        MsgBox(0, "Error", "Error: Must select an encryption type.")
                    EndIf
                EndIf
            EndIf
        Case $Button2
            $save1 = FileSaveDialog("Save", "", "(*.txt)")
            $save = $save1 & ".txt"
            GUICtrlSetData($Input5, $save)
    EndSwitch
WEnd

Func continue()
    $Read4 = GUICtrlRead($Edit1)
    $stringencrypt = _StringEncrypt(1, $Read4, $Read1)
    $r = 100/6
    GUICtrlSetData($Progress1, $r)
    $r = $r + $r
    $tBuffer = DllStructCreate("byte[" & StringLen($stringencrypt) & "]")
    GUICtrlSetData($Progress1, $r)
    $r = $r + $r
    DllStructSetData($tBuffer, 1, $stringencrypt)
    GUICtrlSetData($Progress1, $r)
    $r = $r + $r
    $hFile = _WinAPI_CreateFile($save, 1)
    GUICtrlSetData($Progress1, $r)
    $r = $r + $r
    _WinAPI_WriteFile($hFile, DllStructGetPtr($tBuffer), StringLen($stringencrypt), $nBytes)
    GUICtrlSetData($Progress1, $r)
    $r = $r + $r
    _WinAPI_CloseHandle($hFile)
    GUICtrlSetData($Progress1, $r)
EndFunc

Func continue2()
    $Binary = StringToBinary($readbin)
    $c = 0
    Do
        $bcheck = IsBinary($Binary) = 0
        If $bcheck = 0 Then
            $bfinal = Binary($Binary)
        EndIf
        $c = $c + 1
    Until $bcheck = 1 Or $c = 100
    If $c = 100 Then
        MsgBox(0, "Failure", "Binary Creation failed.")
    Else
        $s = 100/6
        GUICtrlSetData($Progress1, $s)
        $s = $s + $s
        $tBuffer = DllStructCreate("byte[" & StringLen($bfinal) & "]")
        GUICtrlSetData($Progress1, $s)
        $s = $s + $s
        DllStructSetData($tBuffer, 1, $bfinal)
        GUICtrlSetData($Progress1, $s)
        $s = $s + $s
        $hFile = _WinAPI_CreateFile($save, 1)
        GUICtrlSetData($Progress1, $s)
        $s = $s + $s
        _WinAPI_WriteFile($hFile, DllStructGetPtr($tBuffer), StringLen($bfinal), $nBytes)
        GUICtrlSetData($Progress1, $s)
        $s = $s + $s
        _WinAPI_CloseHandle($hFile)
        GUICtrlSetData($Progress1, $s)
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

Does this work as expected?

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#Tidy_Parameters=/tc 0 /sci 1 /kv 0 /reel

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <String.au3>
#include <WinAPI.au3>

Global Const $Form1 = GUICreate("Encrypter", 480, 302, 192, 124)

Global Const $Edit1 = GUICtrlCreateEdit("", 16, 8, 201, 137)
GUICtrlSetTip(-1, "Enter string to encrypt")
GUICtrlSetCursor(-1, 5)

Global Const $Input1 = GUICtrlCreateInput('', 16, 154, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_PASSWORD))
GUICtrlSetTip(-1, "Enter Password")
GUICtrlSetCursor(-1, 5)

Global Const $Input4 = GUICtrlCreateInput("", 16, 184, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_PASSWORD))
GUICtrlSetTip(-1, "Re-Enter Password")
GUICtrlSetCursor(-1, 5)

Global Const $Input5 = GUICtrlCreateInput("", 16, 210, 161, 21)
Global Const $Button2 = GUICtrlCreateButton("...", 184, 208, 35, 25)
GUICtrlSetTip(-1, "Choose a save destination")

Global Const $Encrypt = GUICtrlCreateButton("Encrypt", 144, 168, 75, 25)

; ----------------------------------------------------

Global Const $Input3 = GUICtrlCreateInput('', 264, 8, 161, 21)
Global Const $Button1 = GUICtrlCreateButton("...", 432, 8, 35, 25)

Global Const $Input2 = GUICtrlCreateInput("", 264, 42, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_PASSWORD))
GUICtrlSetTip(-1, "Enter Password")
GUICtrlSetCursor(-1, 5)

Global Const $Decrypt = GUICtrlCreateButton("Decrypt", 392, 40, 75, 25)

Global Const $Radio1 = GUICtrlCreateRadio("RC4", 264, 104, 113, 17)
Global Const $Radio2 = GUICtrlCreateRadio("Binary", 264, 128, 113, 17)
Global Const $Group1 = GUICtrlCreateGroup("Encryption Type", 256, 80, 121, 81)
GUICtrlCreateGroup('', -99, -99, 1, 1)

Global Const $Progress1 = GUICtrlCreateProgress(16, 264, 449, 25)

GUISetState(@SW_SHOW)

Global $selection = ""
Global $error = ""
Global $save = ""
Global $sFile, $hFile, $sText, $nBytes
Global $readbin = ""
Global $Read1, $Read2, $Read3, $save1

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Radio1
            GUICtrlSetState($Input1, $GUI_ENABLE)
            GUICtrlSetState($Input4, $GUI_ENABLE)
            $selection = "1"
        Case $Radio2
            GUICtrlSetState($Input1, $GUI_DISABLE)
            GUICtrlSetState($Input4, $GUI_DISABLE)
            $selection = "2"
        Case $Encrypt
            GUICtrlSetData($Progress1, 0)
            $error = ""
            If $selection = "2" Then
                If GUICtrlRead($Input5) = "" Then
                    MsgBox(0, "Error", "Error: Must select a destination.")
                EndIf
                If GUICtrlRead($Input5) <> "" Then
                    $readbin = GUICtrlRead($Edit1)
                    continue2()
                EndIf
            Else
                $Read1 = GUICtrlRead($Input1)
                $Read2 = GUICtrlRead($Input4)
                $Read3 = GUICtrlRead($Input5)
                If $Read1 = "" Then
                    MsgBox(0, "Error", "Error: Must enter password.")
                    $error = "1"
                EndIf
                If $Read2 = "" Then
                    MsgBox(0, "Error", "Error: Must re-enter password.")
                    $error = "1"
                EndIf
                If $Read3 = "" Then
                    MsgBox(0, "Error", "Error: Must select destination.")
                    $error = "1"
                EndIf
                If $Read1 <> $Read2 Then
                    MsgBox(0, "Error", "Error: Passwords do not match.")
                    $error = "1"
                EndIf
                If $error = "" Then
                    If $selection = "1" Then
                        continue()
                    EndIf
                    If $selection = "" Then
                        MsgBox(0, "Error", "Error: Must select an encryption type.")
                    EndIf
                EndIf
            EndIf
        Case $Button2
            $save1 = FileSaveDialog("Save", "", "(*.txt)")
            $save = $save1 & ".txt"
            GUICtrlSetData($Input5, $save)
    EndSwitch
WEnd

Func continue()
    Local Const $Read4 = GUICtrlRead($Edit1)
    Local Const $stringencrypt = _StringEncrypt(1, $Read4, $Read1)
    WriteFile($stringencrypt)
EndFunc ;==>continue

Func continue2()
    Local Const $Binary = StringToBinary($readbin)

    If IsBinary($Binary) Then
        WriteFile($Binary)
    Else
        MsgBox(0, "Failure", "Binary Creation failed.")
    EndIf
EndFunc ;==>continue2

Func WriteFile(Const $EncryptString)
    Local Const $StringLen = StringLen($EncryptString)
    Local Const $Amount = 100 / 6
    BumpProgressBar($Amount)
    Local Const $tBuffer = DllStructCreate("char[" & $StringLen & "]")
    BumpProgressBar($Amount)
    DllStructSetData($tBuffer, 1, $EncryptString)
    BumpProgressBar($Amount)
    Local Const $hFile = _WinAPI_CreateFile($save, 1)
    BumpProgressBar($Amount)
    _WinAPI_WriteFile($hFile, DllStructGetPtr($tBuffer), $StringLen, $nBytes)
    BumpProgressBar($Amount)
    _WinAPI_CloseHandle($hFile)
    BumpProgressBar($Amount)
EndFunc

Func BumpProgressBar(Const $Amount)
    Local Static $Bump = $Amount
    $Bump += $Amount
    GUICtrlSetData($Progress1, $Bump)
EndFunc ;==>BumpProgressBar
Edited by jaberwocky6669

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...