# Gronsfeld Cipher

## Recommended Posts

The CTF we were playing this evening had a gronsfeld cipher, so here is my first stab at a function that would solve it. Due to the way I am rolling through the alphabet all characters must be uppercase (and specials removed or stripped in the func, i opted for removing them manually for example purposes), any improvements are welcome.

```local \$aTestKey[8] = [0,7,2,7,1,9,7,8]

\$aTestStrCreate = stringsplit(stringupper("ICECTFIDONTTHINKGRONSFELDLIKESMONDAYS") , "" , 2)

\$aTestStrSolve = stringsplit(stringupper("IjgJUOPLOUVAIRUSGYQUTOLTDSKRFBTWNKCFT") , "", 2)

ConsoleWrite(_GronsfeldCipher(\$aTestKey , \$aTestStrCreate , 0) & @LF)
ConsoleWrite(_GronsfeldCipher(\$aTestKey , \$aTestStrSolve , 1) & @LF)

Func _GronsfeldCipher(\$aKey , \$aStr , \$action)  ; 0 = Create , 1 = Solve

\$k = -1
\$aOut = ""

If \$action = 1 Then

For \$i = 0 to ubound(\$astr) - 1
\$k+=1
If asc(\$astr[\$i]) - \$aKey[\$k] < 65 Then
\$aOut &= chrw(asc(\$astr[\$i]) + 26 - \$aKey[\$k])
Else
\$aOut &= chrw(asc(\$astr[\$i]) - \$aKey[\$k])
EndIf
If \$k = ubound(\$aKey) - 1 Then \$k = -1
Next

Else

For \$i = 0 to ubound(\$astr) - 1
\$k+=1
If asc(\$astr[\$i]) + \$aKey[\$k] > 90 Then
\$aOut &= chrw(asc(\$astr[\$i]) - 26 + \$aKey[\$k])
Else
\$aOut &= chrw(asc(\$astr[\$i]) + \$aKey[\$k])
EndIf
If \$k = ubound(\$aKey) - 1 Then \$k = -1
Next

EndIf

return \$aOut

EndFunc```

Edited by iamtheky
improvements

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

##### Share on other sites

added to the Func so you can both create and solve them, suppose I'll get around to fixing it to add lowercase support as well.

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

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...