Vigenère cipher: Difference between revisions
mv jq entry
(mv jq entry) |
|||
Line 1,610:
console.log(enc);
console.log(dec);</lang>
=={{header|jq}}==▼
{{works with|jq}}▼
'''Works with gojq, the Go implementation of jq'''▼
<lang jq>def vigenere(text; key; encryptp):▼
# retain alphabetic characters only▼
def n:▼
ascii_upcase | explode | map(select(65 <= . and . <= 90)) | [., length];▼
(text | n) as [$xtext, $length]▼
| (key | n) as [$xkey, $keylength]▼
| reduce range(0; $length) as $i (null;▼
($i % $keylength) as $ki▼
| . + [if encryptp▼
then (($xtext[$i] + $xkey[$ki] - 130) % 26) + 65▼
else (($xtext[$i] - $xkey[$ki] + 26) % 26) + 65▼
end] )▼
| implode;▼
# Input: sample text▼
def example($key):▼
vigenere(.; $key; true)▼
| . as $encoded▼
| ., vigenere($encoded; $key; false) ;▼
"Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"▼
| (., example("VIGENERECIPHER")),▼
"",▼
(., example("ROSETTACODE"))</lang>▼
{{out}}▼
<pre>▼
Beware the Jabberwock, my son! The jaws that bite, the claws that catch!▼
WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY▼
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH▼
Beware the Jabberwock, my son! The jaws that bite, the claws that catch!▼
SSOEKXTJSMESPWVPHCMABWFBLLXCAYGWLRHTMMXTJSFPRKKXATTEOWGY▼
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre>▼
=={{header|Jsish}}==
Line 1,653 ⟶ 1,690:
<pre>prompt$ jsish -u vigenereCipher.jsi
[PASS] vigenereCipher.jsi</pre>
▲=={{header|jq}}==
▲{{works with|jq}}
▲'''Works with gojq, the Go implementation of jq'''
▲<lang jq>def vigenere(text; key; encryptp):
▲ # retain alphabetic characters only
▲ def n:
▲ ascii_upcase | explode | map(select(65 <= . and . <= 90)) | [., length];
▲ (text | n) as [$xtext, $length]
▲ | (key | n) as [$xkey, $keylength]
▲ | reduce range(0; $length) as $i (null;
▲ ($i % $keylength) as $ki
▲ | . + [if encryptp
▲ then (($xtext[$i] + $xkey[$ki] - 130) % 26) + 65
▲ else (($xtext[$i] - $xkey[$ki] + 26) % 26) + 65
▲ end] )
▲ | implode;
▲# Input: sample text
▲def example($key):
▲ vigenere(.; $key; true)
▲ | . as $encoded
▲ | ., vigenere($encoded; $key; false) ;
▲"Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
▲| (., example("VIGENERECIPHER")),
▲ "",
▲ (., example("ROSETTACODE"))</lang>
▲{{out}}
▲<pre>
▲Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
▲WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
▲BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
▲Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
▲SSOEKXTJSMESPWVPHCMABWFBLLXCAYGWLRHTMMXTJSFPRKKXATTEOWGY
▲BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre>
|