Encrypt/decrypt Text with a hotkey

Post your working scripts, libraries and tools
User avatar
AlleyArtwork
Posts: 30
Joined: 09 Jun 2015, 21:08

Encrypt/decrypt Text with a hotkey

23 Apr 2018, 22:49

This might seem silly but I wanted to be able to quickly be able send encrypted messages over company IM, slack, email, etc.
It could be venting over a sensitive topic, it could be relaying a new password-safe master password to the team, or sending messages while people may be nearby that you'd rather them not catch a glimpse if they're over your shoulder.
I made adjustments to the original function's output methods for quick text messaging of any kind, feel free to modify or improve!

This might be overkill but I combined 2 encryption methods I found on the forums (could not find original topic for B64 but I linked to RC4 post in comments).
It is a cascading cipher, and the Key is automatically set to daily changing variables, with the option to use a custom key instead of the daily rotating default key.

Use:
- Highlight Plain Text and press CTRL+E to encrypt it and overwrite plain text with encrypted text, then you can send your message/IM/email etc.
- Highlight Encrypted Text and press CTRL+D to decrypt it and display the message in a MsgBox pop up.
- pass key automatically changes daily while encrypting or decrypting. During the hotkey press you have a prompt to use a completely custom pass key as well.

Code: Select all

; ====================
; https://autohotkey.com/board/topic/6316-rc4-encryption-to-hex-stream/
; Base64 + RC4 Encode / Decode a string (binary-to-text encoding)
; Encrypt Functions
; =========================

b64Encode(string)
{
    VarSetCapacity(bin, StrPut(string, "UTF-8")) && len := StrPut(string, &bin, "UTF-8") - 1 
    if !(DllCall("crypt32\CryptBinaryToString", "ptr", &bin, "uint", len, "uint", 0x1, "ptr", 0, "uint*", size))
        throw Exception("CryptBinaryToString failed", -1)
    VarSetCapacity(buf, size << 1, 0)
    if !(DllCall("crypt32\CryptBinaryToString", "ptr", &bin, "uint", len, "uint", 0x1, "ptr", &buf, "uint*", size))
        throw Exception("CryptBinaryToString failed", -1)
    return StrGet(&buf)
}

b64Decode(string)
{
    if !(DllCall("crypt32\CryptStringToBinary", "ptr", &string, "uint", 0, "uint", 0x1, "ptr", 0, "uint*", size, "ptr", 0, "ptr", 0))
        throw Exception("CryptStringToBinary failed", -1)
    VarSetCapacity(buf, size, 0)
    if !(DllCall("crypt32\CryptStringToBinary", "ptr", &string, "uint", 0, "uint", 0x1, "ptr", &buf, "uint*", size, "ptr", 0, "ptr", 0))
        throw Exception("CryptStringToBinary failed", -1)
    return StrGet(&buf, size, "UTF-8")
}


RC4txt2hex(Data,Pass) {
   Format := A_FormatInteger
   SetFormat Integer, Hex
   b := 0, j := 0
   VarSetCapacity(Result,StrLen(Data)*2)
   Loop 256
      a := A_Index - 1
     ,Key%a% := Asc(SubStr(Pass, Mod(a,StrLen(Pass))+1, 1))
     ,sBox%a% := a
   Loop 256
      a := A_Index - 1
     ,b := b + sBox%a% + Key%a%  & 255
     ,sBox%a% := (sBox%b%+0, sBox%b% := sBox%a%) ; SWAP(a,b)
   Loop Parse, Data
      i := A_Index & 255
     ,j := sBox%i% + j  & 255
     ,k := sBox%i% + sBox%j%  & 255
     ,sBox%i% := (sBox%j%+0, sBox%j% := sBox%i%) ; SWAP(i,j)
     ,Result .= SubStr(Asc(A_LoopField)^sBox%k%, -1, 2)
   StringReplace Result, Result, x, 0, All
   SetFormat Integer, %Format%
   Return Result
}

RC4hex2txt(Data,Pass) {
   b := 0, j := 0, x := "0x"
   VarSetCapacity(Result,StrLen(Data)//2)
   Loop 256
      a := A_Index - 1
     ,Key%a% := Asc(SubStr(Pass, Mod(a,StrLen(Pass))+1, 1))
     ,sBox%a% := a
   Loop 256
      a := A_Index - 1
     ,b := b + sBox%a% + Key%a%  & 255
     ,sBox%a% := (sBox%b%+0, sBox%b% := sBox%a%) ; SWAP(a,b)
   Loop % StrLen(Data)//2
      i := A_Index  & 255
     ,j := sBox%i% + j  & 255
     ,k := sBox%i% + sBox%j%  & 255
     ,sBox%i% := (sBox%j%+0, sBox%j% := sBox%i%) ; SWAP(i,j)
     ,Result .= Chr((x . SubStr(Data,2*A_Index-1,2)) ^ sBox%k%)
   Return Result
}

; =========================
; Encrypt/Decrypt Hotkeys
; CTRL+E - Encrypt Highlighted text
; CTRL+D - Decrypt highlighted text
; =========================

; CTRL+E - Encrypt Highlighted text
^e::
RC4Pass = %A_MM%%A_DD%%A_YWeek%%A_WDay%%A_DDDD%         ; Sets Pass Key for the message.
AlternateClipboard = %Clipboard%		; save current clipboard as alternate variable
Clipboard := ""
Sendinput, ^c
ClipWait, 2
if ErrorLevel
	Goto, ClipFailed
MsgBox, 36, Pass Key, Do you want to use the default Pass Key to ENCRYPT your message?	; Prompt to use daily Pass Key or Custom
IfMsgBox, No
	InputBox,RC4Pass, Custom Key, Enter the Pass Key to ENCTRYPT the message						
RC4Enc := RC4txt2hex(clipboard,RC4Pass)	; RC4 encrypt with Pass Key
encoded := b64Encode( RC4Enc )			; B64 encrypt

If WinActive("ahk_class AppName1") || WinActive("ahk_class AppName2") ; IF statement for commonly used IMs to output as single line for cleaner IM messages. Use WindowSpy to get App Names.
   {
   StringReplace, encoded, encoded, `r`n,, all
   }

Clipboard = %encoded%
clipwait, 2
send, ^v
sleep, 200
Clipboard = %AlternateClipboard%		; Set clipboard to original clipboard data
encoded =
RC4Pass =
return

; CTRL+D - Decrypt highlighted text
^d::
RC4Pass = %A_MM%%A_DD%%A_YWeek%%A_WDay%%A_DDDD%          ; Sets Pass Key for the message.
AlternateClipboard = %Clipboard%		; save current clipboard as alternate variable
Clipboard := ""
Sendinput, ^c
ClipWait, 2
if ErrorLevel
	Goto, ClipFailed
MsgBox, 36, Pass Key, Do you want to use the default Pass Key to DECRYPT your message?	; Prompt to use daily Pass Key or Custom
IfMsgBox, No
	InputBox,RC4Pass, Cipher Key, Enter the Cipher key to DECRYPT the message 
decode := b64Decode( clipboard )		; B64 decrypt
MsgBox % RC4hex2txt(decode,RC4Pass)		; RC4 decrypt with Pass Key
Clipboard = %AlternateClipboard%
sleep, 100
decode =
RC4Pass =
return

ClipFailed:
Clipboard := AlternateClipboard
Msgbox, An error occured while copying the text... try again.
return
loop {
MsgBox, Life is like an Infinite Message Box of chocolates.`nThere are %a_index% chocolates in this box.
}
carno
Posts: 129
Joined: 20 Jun 2014, 16:48

Re: Encrypt/decrypt Text with a hotkey

30 Jun 2018, 23:16

Also, this:

Code: Select all

; http://autohotkey.com/board/topic/90702-encrypt-decrypt-text/
; https://www.autohotkey.com/boards/viewtopic.php?t=5838

; ===================================================================================
; AHK Version ...: AHK_L 1.1.11.01 x64 Unicode
; Win Version ...: Windows 7 Professional x64 SP1
; Author ........: Originated from jNizM, tweaked by Soft
; Script ........: EnCrypt_DeCrypt.ahk
; Description ...: Encrypt & Decrypt Data
; License .......: WTFPL
; ===================================================================================

; GLOBAL SETTINGS ===================================================================

#NoEnv
#SingleInstance force

#Include Crypt.ahk
#Include CryptConst.ahk
#Include CryptFoos.ahk

; SCRIPT ============================================================================

Gui, Margin, 10, 10
Gui, Font, s10, Segoe UI

Gui, Add, Edit, xm ym w300 h120 vStr hwndEditEnDecrypt, Enter text or drag file to en/decrypt here.

Gui, Add, Edit, xm y+5 w300 vStr2, Password
;Gui, Add, Edit, xm y+5 w300 vStr2 hwndEditPw,
;EM_SetCueBanner(EditPw, "Password")
Gui, Add, Edit, xm y+10 w300 h120 vEnDeCrypt ReadOnly, En/Decrypted text appears here and is also copied to the clipboard!

Gui, Add, DropDownList, xm y+5 w300 AltSubmit vEncryption, RC4 (Rivest Cipher)
    |RC2 (Rivest Cipher)
    |3DES (Data Encryption Standard)
    |3DES 112 (Data Encryption Standard)
    |AES 128 (Advanced Encryption Standard)
    |AES 192 (Advanced Encryption Standard)
    |AES 256 (Advanced Encryption Standard)||
Gui, Add, Button, xm-1 y+5 w100, Encrypt
Gui, Add, Button, xm+201 yp w100, Decrypt
Gui, +LastFound
Gui, Show,, En/Decrypt
WinSet, Redraw
Return

ButtonEncrypt:
    Gui, Submit, NoHide
    GuiControl,, EnDeCrypt, % Crypt.Encrypt.StrEncrypt(Str, Str2, Encryption, 1)
    Clipboard := % Crypt.Encrypt.StrEncrypt(Str, Str2, Encryption, 1)
Return

ButtonDecrypt:
    Gui, Submit, NoHide
    GuiControl,, EnDeCrypt, % Crypt.Encrypt.StrDecrypt(Str, Str2, Encryption, 1)
    Clipboard := % Crypt.Encrypt.StrDecrypt(Str, Str2, Encryption, 1)
Return

GuiDropFiles:
if A_GuiControl = Str
{
    Draged := FileOpen(A_GuiEvent, "r")
    GuiControl,, Str, % Draged.Read()
 
}
Return

; EXIT ==============================================================================

GuiClose:
GuiEscape:
ExitApp

EM_SetCueBanner(hWnd, Cue)
{
    Static EM_SETCUEBANNER := 0x1501
    Return DllCall("User32.dll\SendMessage", "Ptr", hWnd, "UInt", EM_SETCUEBANNER, "Ptr", True, "WStr", Cue)
}

Return to “Scripts and Functions”

Who is online

Users browsing this forum: nilsonrdg, yl3d and 44 guests