Damein

Create encrypted Login Credentials / store in encrypted file

4 posts in this topic

#1 ·  Posted (edited)

I recently needed a way to easily create and store login credentials while also being able to edit them on the fly. After searching a little I decided to write up my own little version of it and this is what I've come up with. I think its really clean and easy to use while being nice and secure. As I'm aware nothing is 100% secure but to my knowledge this is a sufficient way to go about it. If i'm mistaken there please let me know as I'd like to learn from it.

 

*** Note ***

Please be aware that to use this program as is you need to create the proper file/folder structure. So wherever you save the Script / Exe to you need to create a folder labeled "Data" and then create an INI file in the "Data" folder labeled "Login Credentials.ini". Paste the following into the INI

[Key]

[Credentials]

 

*** Note ***

Creating Credentials

  • Input a Username / Password into the GUI
  • Input an encryption key
  • Program verifies your entries and then encrypts the data into the INI file

 

Editing Credentials

  • Once you click the edit button it requires  you to put in your encryption key
  • Program explains to remove a credential you must rename the Username to 'Blank'
  • If valid it reads all the ini data and puts it into a List with a Username / Password field
  • You can then edit a Username / Password (Also remove if you assign 'Blank' to the Username)
  • If you choose to edit it it will then change it into the INI, keeping it encrypted.

 

Imgs

Credential Creator GUI

N7MrDfM.png

 

Credential Editor GUI

XJT3s8j.png

 

Encrypted INI FIle

wA5Hyup.png

 

 

And that's pretty much it. Allows you to then utilize the INI in further scripts that you create so long as you request their encryption key that they used.

 

Thoughts? Comments? Compaints?

 

Thanks!

 

Source

; Created by Robert Salatka (known as Damein on AutoIt Forums)
; Created 4-26-2016

#include <String.au3>
#include <Crypt.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
#include <EditConstants.au3>


Opt("GUIOnEventMode", 1)

Global $List, $Status = 0, $MainGui, $EditGui
Global $DecryptKey, $UsernameInput, $PasswordInput, $KeyInput, $FirstEntry = 0, $Permission = 0

$CheckForKey = IniRead(@ScriptDir & "/Data/Login Credentials.ini", "Key", "Key", "NA")
If $CheckForKey = "NA" Then
    $GetNewKey = InputBox("Encryption Key", "You appear to not have created an encryption key to use with this software. Please do so before using the software!")
    If $GetNewKey = "" Then
        MsgBox(48, "Error", "No key input, please run the program again. You must use an encryption key for this program to work!")
        Exit
    EndIf
    $CheckInformation = MsgBox(4, "Encryption Key", "Do you wish to use this as your encryption key: " & $GetNewKey)
    If $CheckInformation = 6 Then
        $EncryptNewKey = _Crypt_EncryptData($GetNewKey, $GetNewKey, $CALG_RC4)
        IniWrite(@ScriptDir & "/Data/Login Credentials.ini", "Key", "Key", $EncryptNewKey)
        MsgBox(0, "Encryption Key", "Encryption key has been saved!")
    Else
        MsgBox(48, "Encryption Key", "Encryption key creation has been cancelled! Please run the program again if you wish to!")
        Exit
    EndIf
EndIf

_CreateMainGui()

Func _CreateMainGui()
    $MainGui = GUICreate("Credential Creator", 400, 480)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

    GUICtrlCreateLabel("Username", 153, 110, 200, 40)
    GUICtrlSetFont(-1, 16)

    $UsernameInput = GUICtrlCreateInput("", 10, 140, 380, 30, $ES_CENTER)
    GUICtrlSetColor(-1, 0xe50000)
    GUICtrlSetFont(-1, 16)

    GUICtrlCreateLabel("Password", 153, 210, 200, 40)
    GUICtrlSetFont(-1, 16)

    $PasswordInput = GUICtrlCreateInput("", 10, 240, 380, 30, $ES_CENTER)
    GUICtrlSetColor(-1, 0xe50000)
    GUICtrlSetFont(-1, 16)

    GUICtrlCreateLabel("Encryption Key", 130, 310, 200, 40)
    GUICtrlSetFont(-1, 16)

    $KeyInput = GUICtrlCreateInput("", 10, 340, 380, 30, $ES_CENTER)
    GUICtrlSetColor(-1, 0xe50000)
    GUICtrlSetFont(-1, 16)

    $AddCredential = GUICtrlCreateButton("Add Credentials", 10, 390, 185, 70)
    GUICtrlSetOnEvent(-1, "_AddCredentials")
    GUICtrlSetFont(-1, 12)

    $EditCredentials = GUICtrlCreateButton("Edit/Remove Credentials", 205, 390, 185, 70)
    GUICtrlSetOnEvent(-1, "_CreateEditGui")
    GUICtrlSetFont(-1, 12)

    GUISetState()
EndFunc   ;==>_CreateMainGui

Func _AddCredentials()
    $ReadUsername = GUICtrlRead($UsernameInput)
    $ReadPassword = GUICtrlRead($PasswordInput)
    $ReadEncryptionKey = GUICtrlRead($KeyInput)
    $ReadKey = IniRead(@ScriptDir & "/Data/Login Credentials.ini", "Key", "Key", "NA")
    $DecryptKey = _Crypt_DecryptData($ReadKey, $ReadEncryptionKey, $CALG_RC4)
    $TranslateKey = BinaryToString($DecryptKey)
    If $ReadEncryptionKey = $TranslateKey Then
        $CheckInformation = MsgBox(4, "Add Credentials", "Are you sure you wish to add the credentials for: " & $ReadUsername)
        If $CheckInformation = 6 Then
            $ReadIni = IniReadSection(@ScriptDir & "/Data/Login Credentials.ini", "Credentials")
            ; Gives current count add 1 to add to INI
            $EncryptNewUsername = _Crypt_EncryptData($ReadUsername, $ReadEncryptionKey, $CALG_RC4)
            $EncryptNewPassword = _Crypt_EncryptData($ReadPassword, $ReadEncryptionKey, $CALG_RC4)
            IniWrite(@ScriptDir & "/Data/Login Credentials.ini", "Credentials", $ReadIni[0][0] + 1, $EncryptNewUsername & "|" & $EncryptNewPassword)
            GUICtrlSetData($UsernameInput, "")
            GUICtrlSetData($PasswordInput, "")
            MsgBox(0, "Add Credentials", "Addition of credential " & $ReadUsername & " is complete!")
        Else
            MsgBox(0, "Add Credentials", "Cancelling the addition of the credentials!")
        EndIf
    Else
        MsgBox(48, "Error", "Encryption key is invalid. Please try again!")
    EndIf
EndFunc   ;==>_AddCredentials


Func _CreateEditGui()
    GUISetState(@SW_DISABLE, $MainGui)
    If $FirstEntry = 0 Then
        MsgBox(48, "Edit Credentials", "Please note, to remove a credential from the database select the credential you wish to remove and click 'Edit Credentials'" & _
                " then type in 'Blank' (without the quotation marks) as the new username and it will remove the credentials!")
        $GetEncryptionKey = InputBox("Security Check", "Please input the correct encryption key!")
        $ReadKey = IniRead(@ScriptDir & "/Data/Login Credentials.ini", "Key", "Key", "NA")
        $DecryptKey = _Crypt_DecryptData($ReadKey, $GetEncryptionKey, $CALG_RC4)
        If $DecryptKey = $GetEncryptionKey Then
            MsgBox(0, "Security Check", "Decryption successful, welcome, " & @UserName)
        Else
            MsgBox(48, "Error", "Encryption key is invalid please try again!")
            $Permission = 1
        EndIf
        $FirstEntry = 1
    EndIf
    If $Permission = 0 Then
        $EditGui = GUICreate("Credential Editor", 400, 480)
        GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseGui")
        $List = GUICtrlCreateListView("Username|Password", 10, 10, 380, 380)
        _GUICtrlListView_SetColumnWidth($List, 0, 190)
        _GUICtrlListView_SetColumnWidth($List, 1, 190)

        $EditButton = GUICtrlCreateButton("Edit Credentials", 10, 405, 180, 60)
        GUICtrlSetOnEvent(-1, "_Edit")
        GUICtrlSetFont(-1, 16)

        $CloseButton = GUICtrlCreateButton("Close", 210, 405, 180, 60)
        GUICtrlSetOnEvent(-1, "_CloseGUi")
        GUICtrlSetFont(-1, 16)

        $ReadCredentialCount = IniReadSection(@ScriptDir & "/Data/Login Credentials.ini", "Credentials")
        If @error Then
            MsgBox(48, "Error", "No credentials are present. Disabling editting permission. Please rerun the program once you have added credentials to the database!")
            GUICtrlSetState($EditButton, $GUI_DISABLE)
        Else
            For $i = 1 To $ReadCredentialCount[0][0]
                $SplitData = StringSplit($ReadCredentialCount[$i][1], "|")
                $DecryptUserName = _Crypt_DecryptData($SplitData[1], $DecryptKey, $CALG_RC4)
                $DecryptPassword = _Crypt_DecryptData($SplitData[2], $DecryptKey, $CALG_RC4)
                $DecryptedUserName = BinaryToString($DecryptUserName)
                $DecryptedPassword = BinaryToString($DecryptPassword)
                $ListViewData = $DecryptedUserName & "|" & $DecryptedPassword
                GUICtrlCreateListViewItem($ListViewData, $List)
            Next
        EndIf

        GUISetState()
    Else
        GUISetState(@SW_ENABLE, $MainGui)
        Sleep(100)
        WinActivate($MainGui)
        $Permission = 0
        $FirstEntry = 0
    EndIf
EndFunc   ;==>_CreateEditGui

Func _Edit()
    $Status = 0
    $GetSelected = ControlListView($EditGui, "", $List, "GetSelected")
    $GetUserName = ControlListView($EditGui, "", $List, "GetText", $GetSelected)
    $GetPassword = ControlListView($EditGui, "", $List, "GetText", $GetSelected, 1)
    While $Status = 0
        $NewUsername = InputBox("Edit Credentials", "Please input the new username you wish to apply!", $GetUserName)
        $NewPassword = InputBox("Edit Credentials", "Please input the new password you wish to apply!", $GetPassword)
        If $NewUsername = "" Or $NewPassword = "" Then
            MsgBox(48, "Error", "Username or password was not input. If you wish to remove the credentials remember to type 'Blank' in the boxes without the quoataions.")
            $Status = 0
        EndIf
        If $NewUsername > "" And $NewPassword > "" Then
            $CheckInformation = MsgBox(4, "Edit Credentials", "Is this the information you wish to save?" & @CRLF & @CRLF & "Username: " & $NewUsername & @CRLF & _
                    "Password: " & $NewPassword)
            If $CheckInformation = 6 Then
                If $NewUsername = "Blank" Then

                    $ReadIni = IniReadSection(@ScriptDir & "/Data/Login Credentials.ini", "Credentials")
                    For $i = 1 To $ReadIni[0][0]
                        $SplitData = StringSplit($ReadIni[$i][1], "|")
                        $DecryptUserName = _Crypt_DecryptData($SplitData[1], $DecryptKey, $CALG_RC4)
                        $DecryptedUserName = BinaryToString($DecryptUserName)
                        If $DecryptedUserName = $GetUserName Then
                            IniDelete(@ScriptDir & "/Data/Login Credentials.ini", "Credentials", $i)
                        EndIf
                    Next

                    $ReadIni = IniReadSection(@ScriptDir & "/Data/Login Credentials.ini", "Credentials")
                    IniDelete(@ScriptDir & "/Data/Login Credentials.ini", "Credentials")
                    For $i = 1 To $ReadIni[0][0]
                        IniWrite(@ScriptDir & "/Data/Login Credentials.ini", "Credentials", $i, $ReadIni[$i][1])
                    Next
                Else

                    $EncryptNewUsername = _Crypt_EncryptData($NewUsername, $DecryptKey, $CALG_RC4)
                    $EncryptNewPassword = _Crypt_EncryptData($NewPassword, $DecryptKey, $CALG_RC4)
                    $ReadIni = IniReadSection(@ScriptDir & "/Data/Login Credentials.ini", "Credentials")
                    For $i = 1 To $ReadIni[0][0]
                        $SplitData = StringSplit($ReadIni[$i][1], "|")
                        $DecryptUserName = _Crypt_DecryptData($SplitData[1], $DecryptKey, $CALG_RC4)
                        $DecryptedUserName = BinaryToString($DecryptUserName)
                        If $DecryptedUserName = $GetUserName Then

                            IniWrite(@ScriptDir & "/Data/Login Credentials.ini", "Credentials", $i, $EncryptNewUsername & "|" & $EncryptNewPassword)
                            ExitLoop
                        EndIf

                    Next
                EndIf

                MsgBox(0, "Edit Credentials", "Credentials have been changed!")
                GUIDelete($EditGui)
                _CreateEditGui()
                $Status = 1
            Else
                $Status = 0
            EndIf
        EndIf
    WEnd
EndFunc   ;==>_Edit

Func _CloseGui()
    $FirstEntry = 0
    GUIDelete($EditGui)
    GUISetState(@SW_ENABLE, $MainGui)
    Sleep(100)
    WinActivate($MainGui)
EndFunc   ;==>_CloseGui


Func _Exit()
    Exit
EndFunc   ;==>_Exit


While 1
    Sleep(10)
WEnd

 

Edited by Damein

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Damein,

I found a couple of minor issues with your code, if the directory and the ini file we not created ahead of time the autoit failed.

I have added so code to accommodate new users, and to move the data file to the user's appdata  directory.

Updated autoit:  Credentials.au3

Diff of au3 files:  CredentialCreator.html

Edited by punchcard
comments

Regards,

Mike D

Share this post


Link to post
Share on other sites

You should add a 'SITE' field. So you can have unique username/passwords per.  And encryption key seems like a 'master password' sort of function if you are looking at commercial equivalents.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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