Encrypt a string using vigenere techniques

0

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

bleh

Posted 2017-06-07T02:21:02.933

Reputation: 161

Question was closed 2017-06-07T06:15:22.710

Similar, possible duplicate – xnor – 2017-06-07T03:21:48.620

1This challenge could really benefit from a description of the cipher. – Dennis – 2017-06-07T05:45:11.793

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 – ovs – 2017-06-07T05:58:21.533

Your plaintext of a's is two letters shorter than its ciphertext. – Dennis – 2017-06-07T06:52:01.783

Answers

2

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"

Leaky Nun

Posted 2017-06-07T02:21:02.933

Reputation: 45 011

1Uppercase letters would save two bytes. – Dennis – 2017-06-07T05:44:29.350

@Dennis nice observation. – Leaky Nun – 2017-06-07T06:14:56.823

1

Brachylog, 18 bytes

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

Try it online!

Leaky Nun

Posted 2017-06-07T02:21:02.933

Reputation: 45 011

0

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!

Giuseppe

Posted 2017-06-07T02:21:02.933

Reputation: 21 077

0

Pyth, 15 bytes

s.e@GsxLG,@QkbE

Try it online!

Leaky Nun

Posted 2017-06-07T02:21:02.933

Reputation: 45 011

0

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

ovs

Posted 2017-06-07T02:21:02.933

Reputation: 21 408

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. – Leaky Nun – 2017-06-07T06:17:17.967

(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). – Dennis – 2017-06-07T06:51:08.290