0
\$\begingroup\$

This question already has an answer here:

Your task:

Create a program that encrypts a string with another string using a key, using the vigenere cipher.

What a vigenere cipher does

Ciphertext:   string  
Key:          keya   
 |             |
 v             v
string
keyake
 |
 v
(18)(19)(17)(08)(13)(06)
(10)(04)(24)(00)(10)(04)
 |
 v
(28)(23)(41)(08)(23)(10)
 |
 v
cxpixk

If it still isn't clear, read here.

Rules: No built-ins, shortest code in bytes wins, input and output will/should always be either completely lowercase, or completely uppercase.

string, keya -> cxpixk
aaaaaaaaaaaaaaaaaaaaaaaa,abcdefghijklmnopqrstuvwxyz -> abcdefghijklmnopqrstuvwxyz
vigenere, cipher -> xqvlrvtm
short, longkey -> dvbxd
\$\endgroup\$

marked as duplicate by Peter Taylor code-golf Jun 7 '17 at 6:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Similar, possible duplicate \$\endgroup\$ – xnor Jun 7 '17 at 3:21
  • 1
    \$\begingroup\$ This challenge could really benefit from a description of the cipher. \$\endgroup\$ – Dennis Jun 7 '17 at 5:45
  • \$\begingroup\$ If the key is longer than the text, should we lengthen the text? You do it in your second testcase, but not in the fourth testcase \$\endgroup\$ – ovs Jun 7 '17 at 5:58
  • \$\begingroup\$ Your plaintext of a's is two letters shorter than its ciphertext. \$\endgroup\$ – Dennis Jun 7 '17 at 6:52
2
\$\begingroup\$

Jelly, 9 bytes

2 bytes thanks to Dennis.

ṁ⁹,OS‘ịØA

Try it online!

How it works

ṁ⁹,OS‘ịØA            arguments: "KEYA", "STRING"

ṁ⁹         reshape   "KEYAKE"
  ,        pair      ["KEYAKE","STRING"]
   O       codepoint [[75,69,89,65,75,69],[83,84,82,73,78,71]]
    S      sum       [158,153,171,138,153,140]
     ‘     add 1     [159,154,172,139,154,141]
      ịØA  index ØA  "CXPIXK"
           where ØA is "ABCDEFGHIJKLMNOPSRQTUVWXYZ"
\$\endgroup\$
  • 1
    \$\begingroup\$ Uppercase letters would save two bytes. \$\endgroup\$ – Dennis Jun 7 '17 at 5:44
  • \$\begingroup\$ @Dennis nice observation. \$\endgroup\$ – Leaky Nun Jun 7 '17 at 6:14
1
\$\begingroup\$

Brachylog, 18 bytes

ạᵐz{+-₁₂%₂₆;Ạ∋₍}ᵐc

Try it online!

\$\endgroup\$
0
\$\begingroup\$

R, 114 bytes

l=letters
i=match
s=strsplit
function(m,k)cat(l[(i(s(m,'')[[1]],l)+i(s(k,'')[[1]],l)-2)%%26+1][1:nchar(m)],sep='')

Returns a function that takes the message and the key and prints the result to stdout. Will explain later (on mobile now) Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth, 15 bytes

s.e@GsxLG,@QkbE

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 3, 75 73 bytes

-2 bytes thanks to @LeakyNun

f=lambda a,b:a and chr((ord(a[0])+ord(b[0])+1)%26+64)+f(a[1:],b[1:]+b[0])

Try it online!

Takes input in uppercase

\$\endgroup\$
  • \$\begingroup\$ You don't need to minus 194. You just need to minus 12 for lowercase, and add 1 for uppercase, so using uppercase would be shorter. \$\endgroup\$ – Leaky Nun Jun 7 '17 at 6:17
  • \$\begingroup\$ (ord(a[0])+ord(b[0])+1)%26+64 won't work for Z. (ord(a[0])+ord(b[0]))%26+65 should (and saves two bytes). \$\endgroup\$ – Dennis Jun 7 '17 at 6:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.