Advertisement
Najeebsk

PASSWORD-STORE.au3

Nov 8th, 2022
3,152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 10.89 KB | None | 0 0
  1. ; Created by Najeeb Shah Khan
  2. ; Created 01-09-2022
  3. ; Update 11-09-2022 Correct initial use issues and move data to appdata folder.
  4.  
  5. #include <String.au3>
  6. #include <Crypt.au3>
  7. #include <GUIConstantsEx.au3>
  8. #include <GuiListView.au3>
  9. #include <MsgBoxConstants.au3>
  10. #include <EditConstants.au3>
  11.  
  12.  
  13. Opt("GUIOnEventMode", 1)
  14.  
  15. Global $List, $Status = 0, $MainGui, $EditGui
  16. Global $DecryptKey, $UsernameInput, $PasswordInput, $KeyInput, $FirstEntry = 0, $Permission = 0
  17.  
  18. $CheckForKey = IniRead(@AppDataDir & "/Cred_Data/Credentials.ini", "Key", "Key", "NA")
  19. If $CheckForKey = "NA" Then
  20.     $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!")
  21.     If $GetNewKey = "" Then
  22.         MsgBox(48, "Error", "No key input, please run the program again. You must use an encryption key for this program to work!")
  23.         Exit
  24.     EndIf
  25.     $CheckInformation = MsgBox(4, "Encryption Key", "Do you wish to use this as your encryption key: " & $GetNewKey)
  26.     If $CheckInformation = 6 Then
  27.         $EncryptNewKey = _Crypt_EncryptData($GetNewKey, $GetNewKey, $CALG_RC4)
  28.         ; create directory if it does not exist
  29.         $DataDir = @AppDataDir & "/Cred_Data"
  30.         If DirGetSize($DataDir)  -1 Then
  31.             DirCreate($DataDir)
  32.         EndIf
  33.         IniWrite(@AppDataDir & "/Cred_Data/Credentials.ini", "Key", "Key", $EncryptNewKey)
  34.         MsgBox(0, "Encryption Key", "Encryption key has been saved!" )
  35.     Else
  36.         MsgBox(48, "Encryption Key", "Encryption key creation has been cancelled! Please run the program again if you wish to!")
  37.         Exit
  38.     EndIf
  39. EndIf
  40.  
  41. _CreateMainGui()
  42.  
  43. Func _CreateMainGui()
  44.     $MainGui = GUICreate("Najeeb Password Store", 400, 480)
  45.     GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
  46.  
  47.     GUICtrlCreateLabel("Username", 153, 110, 200, 40)
  48.     GUICtrlSetFont(-1, 16)
  49.  
  50.     $UsernameInput = GUICtrlCreateInput("", 10, 140, 380, 30, $ES_CENTER)
  51.     GUICtrlSetColor(-1, 0xe50000)
  52.     GUICtrlSetFont(-1, 16)
  53.  
  54.     GUICtrlCreateLabel("Password", 153, 210, 200, 40)
  55.     GUICtrlSetFont(-1, 16)
  56.  
  57.     $PasswordInput = GUICtrlCreateInput("", 10, 240, 380, 30, $ES_CENTER)
  58.     GUICtrlSetColor(-1, 0xe50000)
  59.     GUICtrlSetFont(-1, 16)
  60.  
  61.     GUICtrlCreateLabel("Encryption Key", 130, 310, 200, 40)
  62.     GUICtrlSetFont(-1, 16)
  63.  
  64.     $KeyInput = GUICtrlCreateInput("", 10, 340, 380, 30, $ES_CENTER)
  65.     GUICtrlSetColor(-1, 0xe50000)
  66.     GUICtrlSetFont(-1, 16)
  67.  
  68.     $AddCredential = GUICtrlCreateButton("Add Credentials", 10, 390, 185, 70)
  69.     GUICtrlSetOnEvent(-1, "_AddCredentials")
  70.     GUICtrlSetFont(-1, 12)
  71.  
  72.     $EditCredentials = GUICtrlCreateButton("Edit/Remove Credentials", 205, 390, 185, 70)
  73.     GUICtrlSetOnEvent(-1, "_CreateEditGui")
  74.     GUICtrlSetFont(-1, 12)
  75.  
  76.     GUISetState()
  77. EndFunc   ;==>_CreateMainGui
  78.  
  79. Func _AddCredentials()
  80.     $ReadUsername = GUICtrlRead($UsernameInput)
  81.     $ReadPassword = GUICtrlRead($PasswordInput)
  82.     $ReadEncryptionKey = GUICtrlRead($KeyInput)
  83.     $ReadKey = IniRead(@AppDataDir & "/Cred_Data/Credentials.ini", "Key", "Key", "NA")
  84.     $DecryptKey = _Crypt_DecryptData($ReadKey, $ReadEncryptionKey, $CALG_RC4)
  85.     $TranslateKey = BinaryToString($DecryptKey)
  86.     If $ReadEncryptionKey = $TranslateKey Then
  87.         $CheckInformation = MsgBox(4, "Add Credentials", "Are you sure you wish to add the credentials for: " & $ReadUsername)
  88.         If $CheckInformation = 6 Then
  89.             $ReadIni = IniReadSection(@AppDataDir & "/Cred_Data/Credentials.ini", "Credentials")
  90.             ; Check for section initialized, if not assume it is now and set counter to 1
  91.             If @error Then
  92.                 $NextKey = 1
  93.             Else
  94.                 $NextKey = $ReadIni[0][0] + 1
  95.             EndIf
  96.             ; Gives current count add 1 to add to INI
  97.             $EncryptNewUsername = _Crypt_EncryptData($ReadUsername, $ReadEncryptionKey, $CALG_RC4)
  98.             $EncryptNewPassword = _Crypt_EncryptData($ReadPassword, $ReadEncryptionKey, $CALG_RC4)
  99.             IniWrite(@AppDataDir & "/Cred_Data/Credentials.ini", "Credentials", $NextKey , $EncryptNewUsername & "|" & $EncryptNewPassword)
  100.             GUICtrlSetData($UsernameInput, "")
  101.             GUICtrlSetData($PasswordInput, "")
  102.             MsgBox(0, "Add Credentials", "Addition of credential " & $ReadUsername & " is complete!")
  103.             ;Exit
  104.         Else
  105.             MsgBox(0, "Add Credentials", "Cancelling the addition of the credentials!")
  106.         EndIf
  107.     Else
  108.         MsgBox(48, "Error", "Encryption key is invalid. Please try again!")
  109.     EndIf
  110. EndFunc   ;==>_AddCredentials
  111.  
  112.  
  113. Func _CreateEditGui()
  114.     GUISetState(@SW_DISABLE, $MainGui)
  115.     If $FirstEntry = 0 Then
  116.         MsgBox(48, "Edit Credentials", "Please note, to remove a credential from the database select the credential you wish to remove and click 'Edit Credentials'" & _
  117.                 " then type in 'Blank' (without the quotation marks) as the new username and it will remove the credentials!")
  118.         $GetEncryptionKey = InputBox("Security Check", "Please input the correct encryption key!")
  119.         $ReadKey = IniRead(@AppDataDir & "/Cred_Data/Credentials.ini", "Key", "Key", "NA")
  120.         $DecryptKey = _Crypt_DecryptData($ReadKey, $GetEncryptionKey, $CALG_RC4)
  121.         If $DecryptKey = $GetEncryptionKey Then
  122.             MsgBox(0, "Security Check", "Decryption successful, welcome, " & @UserName)
  123.         Else
  124.             MsgBox(48, "Error", "Encryption key is invalid please try again!")
  125.             $Permission = 1
  126.         EndIf
  127.         $FirstEntry = 1
  128.     EndIf
  129.     If $Permission = 0 Then
  130.         $EditGui = GUICreate("Credential Editor", 400, 480)
  131.         GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseGui")
  132.         $List = GUICtrlCreateListView("Username|Password", 10, 10, 380, 380)
  133.         _GUICtrlListView_SetColumnWidth($List, 0, 190)
  134.         _GUICtrlListView_SetColumnWidth($List, 1, 190)
  135.  
  136.         $EditButton = GUICtrlCreateButton("Edit Credentials", 10, 405, 180, 60)
  137.         GUICtrlSetOnEvent(-1, "_Edit")
  138.         GUICtrlSetFont(-1, 16)
  139.  
  140.         $CloseButton = GUICtrlCreateButton("Close", 210, 405, 180, 60)
  141.         GUICtrlSetOnEvent(-1, "_CloseGUi")
  142.         GUICtrlSetFont(-1, 16)
  143.  
  144.         $ReadCredentialCount = IniReadSection(@AppDataDir & "/Cred_Data/Credentials.ini", "Credentials")
  145.         If @error Then
  146.             MsgBox(48, "Error", "No credentials are present. Disabling editting permission. Please rerun the program once you have added credentials to the database!")
  147.             GUICtrlSetState($EditButton, $GUI_DISABLE)
  148.         Else
  149.             For $i = 1 To $ReadCredentialCount[0][0]
  150.                 $SplitData = StringSplit($ReadCredentialCount[$i][1], "|")
  151.                 $DecryptUserName = _Crypt_DecryptData($SplitData[1], $DecryptKey, $CALG_RC4)
  152.                 $DecryptPassword = _Crypt_DecryptData($SplitData[2], $DecryptKey, $CALG_RC4)
  153.                 $DecryptedUserName = BinaryToString($DecryptUserName)
  154.                 $DecryptedPassword = BinaryToString($DecryptPassword)
  155.                 $ListViewData = $DecryptedUserName & "|" & $DecryptedPassword
  156.                 GUICtrlCreateListViewItem($ListViewData, $List)
  157.             Next
  158.         EndIf
  159.  
  160.         GUISetState()
  161.     Else
  162.         GUISetState(@SW_ENABLE, $MainGui)
  163.         Sleep(100)
  164.         WinActivate($MainGui)
  165.         $Permission = 0
  166.         $FirstEntry = 0
  167.     EndIf
  168. EndFunc   ;==>_CreateEditGui
  169.  
  170. Func _Edit()
  171.     $Status = 0
  172.     $GetSelected = ControlListView($EditGui, "", $List, "GetSelected")
  173.     $GetUserName = ControlListView($EditGui, "", $List, "GetText", $GetSelected)
  174.     $GetPassword = ControlListView($EditGui, "", $List, "GetText", $GetSelected, 1)
  175.     While $Status = 0
  176.         $NewUsername = InputBox("Edit Credentials", "Please input the new username you wish to apply!", $GetUserName)
  177.         $NewPassword = InputBox("Edit Credentials", "Please input the new password you wish to apply!", $GetPassword)
  178.         If $NewUsername = "" Or $NewPassword = "" Then
  179.             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.")
  180.             $Status = 0
  181.         EndIf
  182.         If $NewUsername > "" And $NewPassword > "" Then
  183.             $CheckInformation = MsgBox(4, "Edit Credentials", "Is this the information you wish to save?" & @CRLF & @CRLF & "Username: " & $NewUsername & @CRLF & _
  184.                     "Password: " & $NewPassword)
  185.             If $CheckInformation = 6 Then
  186.                 If $NewUsername = "Blank" Then
  187.  
  188.                     $ReadIni = IniReadSection(@AppDataDir & "/Cred_Data/Credentials.ini", "Credentials")
  189.                     For $i = 1 To $ReadIni[0][0]
  190.                         $SplitData = StringSplit($ReadIni[$i][1], "|")
  191.                         $DecryptUserName = _Crypt_DecryptData($SplitData[1], $DecryptKey, $CALG_RC4)
  192.                         $DecryptedUserName = BinaryToString($DecryptUserName)
  193.                         If $DecryptedUserName = $GetUserName Then
  194.                             IniDelete(@AppDataDir & "/Cred_Data/Credentials.ini", "Credentials", $i)
  195.                         EndIf
  196.                     Next
  197.  
  198.                     $ReadIni = IniReadSection(@AppDataDir & "/Cred_Data/Credentials.ini", "Credentials")
  199.                     IniDelete(@AppDataDir & "/Cred_Data/Credentials.ini", "Credentials")
  200.                     For $i = 1 To $ReadIni[0][0]
  201.                         IniWrite(@AppDataDir & "/Cred_Data/Credentials.ini", "Credentials", $i, $ReadIni[$i][1])
  202.                     Next
  203.                 Else
  204.  
  205.                     $EncryptNewUsername = _Crypt_EncryptData($NewUsername, $DecryptKey, $CALG_RC4)
  206.                     $EncryptNewPassword = _Crypt_EncryptData($NewPassword, $DecryptKey, $CALG_RC4)
  207.                     $ReadIni = IniReadSection(@AppDataDir & "/Cred_Data/Credentials.ini", "Credentials")
  208.                     For $i = 1 To $ReadIni[0][0]
  209.                         $SplitData = StringSplit($ReadIni[$i][1], "|")
  210.                         $DecryptUserName = _Crypt_DecryptData($SplitData[1], $DecryptKey, $CALG_RC4)
  211.                         $DecryptedUserName = BinaryToString($DecryptUserName)
  212.                         If $DecryptedUserName = $GetUserName Then
  213.  
  214.                             IniWrite(@AppDataDir & "/Cred_Data/Credentials.ini", "Credentials", $i, $EncryptNewUsername & "|" & $EncryptNewPassword)
  215.                             ExitLoop
  216.                         EndIf
  217.  
  218.                     Next
  219.                 EndIf
  220.  
  221.                 MsgBox(0, "Edit Credentials", "Credentials have been changed!")
  222.                 GUIDelete($EditGui)
  223.                 _CreateEditGui()
  224.                 $Status = 1
  225.             Else
  226.                 $Status = 0
  227.             EndIf
  228.         EndIf
  229.     WEnd
  230. EndFunc   ;==>_Edit
  231.  
  232. Func _CloseGui()
  233.     $FirstEntry = 0
  234.     GUIDelete($EditGui)
  235.     GUISetState(@SW_ENABLE, $MainGui)
  236.     Sleep(100)
  237.     WinActivate($MainGui)
  238. EndFunc   ;==>_CloseGui
  239.  
  240.  
  241. Func _Exit()
  242.     Exit
  243. EndFunc   ;==>_Exit
  244.  
  245.  
  246. While 1
  247.     Sleep(10)
  248. WEnd
  249.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement