Jump to content

Vigenère cipher: Difference between revisions

Line 2,218:
elements on that side until the other side runs out. In particular, repeating
the key naturally falls out of this cyclic dwimmery, as does repeating the various constants to be applied with any of several operations to every element of the list. Factoring out the canonicalization and decanonicalization lets us see quite clearly that the only difference between encryption and decryptions is the sign of the vector addition/subtraction. Since hyperops are inherently parallelizable, this algorithm might run well in your GPU.
 
=={{header|Phix}}==
<lang Phix>enum type mode ENCRYPT = +1, DECRYPT = -1 end type
 
function Vigenere(string s, string key, mode m)
string res = ""
integer k = 1, ch
s = upper(s)
for i=1 to length(s) do
ch = s[i]
if ch>='A' and ch<='Z' then
res &= 'A'+mod(ch+m*(key[k]+26),26)
k = mod(k,length(key))+1
end if
end for
return res
end function
constant key = "LEMON",
s = "ATTACK AT DAWN",
e = Vigenere(s,key,ENCRYPT),
d = Vigenere(e,key,DECRYPT)
printf(1,"Original: %s\nEncrypted: %s\nDecrypted: %s\n",{s,e,d})</lang>
{{Out}}
<pre>
Original: ATTACK AT DAWN
Encrypted: LXFOPVEFRNHR
Decrypted: ATTACKATDAWN
</pre>
 
=={{header|PHP}}==
7,820

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.