shortest code to encrypt a string
BrainFuck: 21
>,[+<[->+>+<<]>.>+>,]
Assumes EOF=zero
echo -n 'My secret password is 123456!' | beef encrypt.bf
==> N{#wjiym}*{mv36HJLNPR>
C — 67 56 49 characters
During compilation it might warn that q
is missing a type...
q;main(p){for(;p=~getchar();)putchar(q++%255-p);}
Thanks to Josh for the significantly shorter rewrites!
$ echo 'My secret password is 123456!' | ./_encrypt
N{#wjiym}*{m����v3}�6HJLNPR>(
Confirm it handles the rollover from 255 to 1 correctly by encrypting 343 x
s and checking for no x
s in the output:
$ echo xxxxxxx | sed -e 's/x/xxxxxxx/g' -e 's/x/xxxxxxx/g' | ./_encrypt | grep -o x | wc -l
0
Befunge 98 - 15 bytes
:~+'U3*%1+,1+#@
Keeps a counter (starting at 0) and adds that to the ~
input. Then, computes 255
(as 85 * 3
in the form of 'U3*
), then mods (%
). Then, it adds 1
and prints it, then adds one to the counter, then uses #
to skip over the @
. At the end of the input, the IP goes the other way and hits the @
, ending the program.
Alternatively, if your interpreter supports unicode, this 14 byte (13 char) solution works:
:~+'ÿ%1+,1+#@
Sample run (in command prompt, so some of the unicode characters are actually some ascii value):
This string will be encrypted using the predetermined method!2.7182818284590452353602874713526624
Output:
Ujlw%y{zrxr,äw{|1tx4zäzèÆèÅüü>öôèÉèDÖÄîHÖ£ÉÉÆóöó₧¢íÖÖVñ¥¡ó¬á^pmwrzu|v~yÇ}⌂ä|üâüâåàëäçÄÄîÉïÄæÅöòÆò