|
| 1 | +script "PuzzleToolsLib" |
| 2 | + |
| 3 | +function toNumber pChar |
| 4 | + local tNum |
| 5 | + put chartonum(toUpper(pChar)) - 64 into tNum |
| 6 | + if tNum < 1 or tNum > 26 then |
| 7 | + throw "char not alphabetical" |
| 8 | + else |
| 9 | + return tNum |
| 10 | + end if |
| 11 | +end toNumber |
| 12 | + |
| 13 | +function fromNumber pNum |
| 14 | + local tNum |
| 15 | + put ((pNum - 1) mod 26) + 65 into tNum |
| 16 | + return numtochar(tNum) |
| 17 | +end fromNumber |
| 18 | + |
| 19 | +function caesarShift pTarget, pAmount |
| 20 | + local tOutput |
| 21 | + if pAmount is not a number then |
| 22 | + put toNumber(pAmount) - 1 into pAmount |
| 23 | + end if |
| 24 | + repeat for each char tChar in pTarget |
| 25 | + put fromNumber(toNumber(tChar) + pAmount) after tOutput |
| 26 | + end repeat |
| 27 | + return tOutput |
| 28 | +end caesarShift |
| 29 | + |
| 30 | +function vigenereShift pTarget, pCipher |
| 31 | + local tOutput, tCipherSize, tCount |
| 32 | + put the number of chars of pCipher into tCipherSize |
| 33 | + repeat for each char tChar in pTarget |
| 34 | + add 1 to tCount |
| 35 | + put caesarShift(tChar, char (((tCount - 1) mod tCipherSize)+1) of pCipher) after tOutput |
| 36 | + end repeat |
| 37 | + return tOutput |
| 38 | +end vigenereShift |
| 39 | + |
| 40 | +function caesarDecode pTarget, pAmount |
| 41 | + if pAmount is not a number then |
| 42 | + put toNumber(pAmount) - 1 into pAmount |
| 43 | + end if |
| 44 | + return caesarShift(pTarget, 26 - pAmount) |
| 45 | +end caesarDecode |
| 46 | + |
| 47 | +function vigenereDecode pTarget, pKey |
| 48 | + local tOutput, tKeySize, tCount |
| 49 | + put the number of chars of pKey into tKeySize |
| 50 | + repeat for each char tChar in pTarget |
| 51 | + add 1 to tCount |
| 52 | + put tOutput & caesarDecode(tChar, char (((tCount) mod tKeySize)+1) of pKey) into tOutput |
| 53 | + end repeat |
| 54 | + return tOutput |
| 55 | +end vigenereDecode |
0 commit comments