Caesar Cipher Function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ben the Coder

Caesar Cipher Function

Post by Ben the Coder » 29 Jun 2022, 11:24

Is the Caesar cipher function a legacy one or not? It's not working for me in my script!

gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Caesar Cipher Function

Post by gregster » 29 Jun 2022, 11:34

It's not a built-in function, so you should tell potential helpers about which function (version) we are talking... code and/or link would help.
You should also add how you called it.

Ben the Coder

Re: Caesar Cipher Function

Post by Ben the Coder » 29 Jun 2022, 12:14

Oh, ok...
Here's my encrypt code:

Code: Select all

vText := "mastpass"
vmastencrypted := ""
Loop, 26
{
	vIndex := A_Index-1
	Loop, Parse, vText
	{
		vNum := Ord(A_LoopField)
		if (vNum >= 65) && (vNum <= 90)
			vmastencrypted .= Chr(Mod(vNum-65+vIndex, 26)+65)
		else if (vNum >= 97) && (vNum <= 122)
			vmastencrypted .= Chr(Mod(vNum-97+vIndex, 26)+97)
		else
			vmastencrypted .= A_LoopField
	}
	vmastencrypted .= "`r`n"
}
MsgBox, % vOutput
But I've also tried this one:

Code: Select all

mastencrypted := caesarCipher(%mastpass%, 8, key)
Thanks!

gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Caesar Cipher Function

Post by gregster » 29 Jun 2022, 12:21

Without even running this, a few questions arise...
Where does vOutput in MsgBox, % vOutput come from? You never assign anything to it... perhaps you meant to display vmastencrypted instead.
Try MsgBox, % vmastencrypted

Where is the user-defined function caesarCipher() ? AHK doesn't know this function, if you don't provide it.
btw, variables in the parameter section of a function are rarely surrounded by %s - only if you want to do a double-deref... but if it's supposed to be a literal string, you should quote it.

Ben the Coder

Re: Caesar Cipher Function

Post by Ben the Coder » 29 Jun 2022, 12:28

Oh yeah, I see that the variable Output is meant to be mastencrypted. I'll try that, and I didn't know that variables in the parameter section of a function are rarely surrounded by %.
Also, what do you mean by
Where is the user-defined function caesarCipher() ? AHK doesn't know this function, if you don't provide it.
I'm still very much a beginner here!

gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Caesar Cipher Function

Post by gregster » 29 Jun 2022, 12:33

Well, like I said. caesarCipher() is not a built-in function. AHK doesn't know it, if you don't provide it.
You can define functions yourself (<-- please follow the link, and read about functions) by putting them into your script, or copypaste functions made by others. Then you can call them.

A simple user-defined function, and a function call:

Code: Select all

say("hello")	; function call

say(var) {		; function definition
	msgbox % var
}

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Caesar Cipher Function

Post by flyingDman » 29 Jun 2022, 16:11

This is probably the function that @Ben G is referring to: https://www.autohotkey.com/board/topic/82506-function-caesar-cipher/
In its simplest form:

Code: Select all

msgbox % z := caesarCipher("abcde", 8)
msgbox % caesarCipher(z, -8)

caesarCipher(text, shift, key = "abcdefghijklmnopqrstuvwxyz"){
	Loop, Parse, text
		encryptedString .= SubStr(key, Mod(Instr(key, A_LoopField, True) + shift, StrLen(key)), 1)
	return encryptedString
}
The bigger question is, why even use it. It provides very little protection and there are much more secure ways to encrypt strings. for instance:
viewtopic.php?f=6&t=23413
14.3 & 1.3.7

Ben the Coder

Re: Caesar Cipher Function

Post by Ben the Coder » 30 Jun 2022, 09:39

Thanks so much! I'll try out your resources.

Post Reply

Return to “Ask for Help (v1)”