Jump to content

Vigenère cipher: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 423:
Cipher text: Wmceei klg Rpifvmeugx, qp wqv! Ioi avey xuek fkbt, alv xtgaf xyev kpagy!
Plain text: Beware the Jabberwock, my son! The jaws that bite, the claws that catch!</pre>
 
=={{header|C sharp|C#}}==
<lang csharp>
using System;
 
namespace VigenereCipher
{
class VCipher
{
public string encrypt(string txt, string pw, int d)
{
int pwi = 0, tmp;
string ns = "";
txt = txt.ToUpper();
pw = pw.ToUpper();
foreach (char t in txt)
{
if (t < 65) continue;
tmp = t - 65 + d * (pw[pwi] - 65);
if (tmp < 0) tmp += 26;
ns += Convert.ToChar(65 + ( tmp % 26) );
if (++pwi == pw.Length) pwi = 0;
}
 
return ns;
}
};
 
class Program
{
static void Main(string[] args)
{
VCipher v = new VCipher();
 
string s0 = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!",
pw = "VIGENERECIPHER";
 
Console.WriteLine(s0 + "\n" + pw + "\n");
string s1 = v.encrypt(s0, pw, 1);
Console.WriteLine("Encrypted: " + s1);
s1 = v.encrypt(s1, "VIGENERECIPHER", -1);
Console.WriteLine("Decrypted: " + s1);
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
</lang>
{{out}}
<pre>
Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
VIGENERECIPHER
 
Encrypted: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
</pre>
 
=={{header|C++}}==
Line 502 ⟶ 558:
<pre>
Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
Encrypted: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
</pre>
 
=={{header|C sharp|C#}}==
<lang csharp>
using System;
 
namespace VigenereCipher
{
class VCipher
{
public string encrypt(string txt, string pw, int d)
{
int pwi = 0, tmp;
string ns = "";
txt = txt.ToUpper();
pw = pw.ToUpper();
foreach (char t in txt)
{
if (t < 65) continue;
tmp = t - 65 + d * (pw[pwi] - 65);
if (tmp < 0) tmp += 26;
ns += Convert.ToChar(65 + ( tmp % 26) );
if (++pwi == pw.Length) pwi = 0;
}
 
return ns;
}
};
 
class Program
{
static void Main(string[] args)
{
VCipher v = new VCipher();
 
string s0 = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!",
pw = "VIGENERECIPHER";
 
Console.WriteLine(s0 + "\n" + pw + "\n");
string s1 = v.encrypt(s0, pw, 1);
Console.WriteLine("Encrypted: " + s1);
s1 = v.encrypt(s1, "VIGENERECIPHER", -1);
Console.WriteLine("Decrypted: " + s1);
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
</lang>
{{out}}
<pre>
Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
VIGENERECIPHER
 
Encrypted: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
Line 588:
print(decrypted);
}</lang>
 
=={{header|Common Lisp}}==
This doesn't assume anything about character codes other than A-Z being a contiguous block (but still, we could be using EBCDIC. Who knows.)
<lang lisp>(defun strip (s)
(remove-if-not
(lambda (c) (char<= #\A c #\Z))
(string-upcase s)))
 
(defun vigenère (s key &key decipher
&aux (A (char-code #\A))
(op (if decipher #'- #'+)))
(labels
((to-char (c) (code-char (+ c A)))
(to-code (c) (- (char-code c) A)))
(let ((k (map 'list #'to-code (strip key))))
(setf (cdr (last k)) k)
(map 'string
(lambda (c)
(prog1
(to-char
(mod (funcall op (to-code c) (car k)) 26))
(setf k (cdr k))))
(strip s)))))
 
(let* ((msg "Beware the Jabberwock... The jaws that... the claws that catch!")
(key "vigenere cipher")
(enc (vigenère msg key))
(dec (vigenère enc key :decipher t)))
(format t "msg: ~a~%enc: ~a~%dec: ~a~%" msg enc dec))</lang>
{{out}}
<pre>msg: Beware the Jabberwock... The jaws that... the claws that catch!
enc: WMCEEIKLGRPIFVMEUGXXYILILZXYVBZLRGCEYAIOEKXIZGU
dec: BEWARETHEJABBERWOCKTHEJAWSTHATTHECLAWSTHATCATCH</pre>
 
=={{header|Clojure}}==
Line 714 ⟶ 681:
Encrypted : WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted : BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre>
 
=={{header|Common Lisp}}==
This doesn't assume anything about character codes other than A-Z being a contiguous block (but still, we could be using EBCDIC. Who knows.)
<lang lisp>(defun strip (s)
(remove-if-not
(lambda (c) (char<= #\A c #\Z))
(string-upcase s)))
 
(defun vigenère (s key &key decipher
&aux (A (char-code #\A))
(op (if decipher #'- #'+)))
(labels
((to-char (c) (code-char (+ c A)))
(to-code (c) (- (char-code c) A)))
(let ((k (map 'list #'to-code (strip key))))
(setf (cdr (last k)) k)
(map 'string
(lambda (c)
(prog1
(to-char
(mod (funcall op (to-code c) (car k)) 26))
(setf k (cdr k))))
(strip s)))))
 
(let* ((msg "Beware the Jabberwock... The jaws that... the claws that catch!")
(key "vigenere cipher")
(enc (vigenère msg key))
(dec (vigenère enc key :decipher t)))
(format t "msg: ~a~%enc: ~a~%dec: ~a~%" msg enc dec))</lang>
{{out}}
<pre>msg: Beware the Jabberwock... The jaws that... the claws that catch!
enc: WMCEEIKLGRPIFVMEUGXXYILILZXYVBZLRGCEYAIOEKXIZGU
dec: BEWARETHEJABBERWOCKTHEJAWSTHATTHECLAWSTHATCATCH</pre>
 
=={{header|D}}==
Line 940:
<pre>Ciphertext: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre>
 
=={{header|F_Sharp|F#}}==
<lang fsharp>
module vigenere =
let keyschedule (key:string) =
let s = key.ToUpper().ToCharArray() |> Array.filter System.Char.IsLetter
let l = Array.length s
(fun n -> int s.[n % l])
 
let enc k c = ((c + k - 130) % 26) + 65
let dec k c = ((c - k + 130) % 26) + 65
let crypt f key = Array.mapi (fun n c -> f (key n) c |> char)
 
let encrypt key (plaintext:string) =
plaintext.ToUpper().ToCharArray()
|> Array.filter System.Char.IsLetter
|> Array.map int
|> crypt enc (keyschedule key)
|> (fun a -> new string(a))
 
let decrypt key (ciphertext:string) =
ciphertext.ToUpper().ToCharArray()
|> Array.map int
|> crypt dec (keyschedule key)
|> (fun a -> new string(a))
 
let passwd = "Vigenere Cipher"
let cipher = vigenere.encrypt passwd "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
let plain = vigenere.decrypt passwd cipher
printfn "%s\n%s" cipher plain
</lang>
 
<pre>C:\src\fsharp>fsi vigenere.fsx
WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
</pre>
 
=={{header|Factor}}==
Line 1,048 ⟶ 1,084:
WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre>
 
=={{header|F_Sharp|F#}}==
<lang fsharp>
module vigenere =
let keyschedule (key:string) =
let s = key.ToUpper().ToCharArray() |> Array.filter System.Char.IsLetter
let l = Array.length s
(fun n -> int s.[n % l])
 
let enc k c = ((c + k - 130) % 26) + 65
let dec k c = ((c - k + 130) % 26) + 65
let crypt f key = Array.mapi (fun n c -> f (key n) c |> char)
 
let encrypt key (plaintext:string) =
plaintext.ToUpper().ToCharArray()
|> Array.filter System.Char.IsLetter
|> Array.map int
|> crypt enc (keyschedule key)
|> (fun a -> new string(a))
 
let decrypt key (ciphertext:string) =
ciphertext.ToUpper().ToCharArray()
|> Array.map int
|> crypt dec (keyschedule key)
|> (fun a -> new string(a))
 
let passwd = "Vigenere Cipher"
let cipher = vigenere.encrypt passwd "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
let plain = vigenere.decrypt passwd cipher
printfn "%s\n%s" cipher plain
</lang>
 
<pre>C:\src\fsharp>fsi vigenere.fsx
WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
</pre>
 
=={{header|Go}}==
Line 1,535:
VIGENERECIPHER
WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
 
=={{header|Lua}}==
Line 2,443:
Decrypting:
perl cipher.pl DEC (cipher text) (key)</pre>
 
=={{header|Perl 6}}==
{{Works with|rakudo|2015-11-14}}
<lang perl6>sub s2v ($s) { $s.uc.comb(/ <[ A..Z ]> /)».ord »-» 65 }
sub v2s (@v) { (@v »%» 26 »+» 65)».chr.join }
 
sub blacken ($red, $key) { v2s(s2v($red) »+» s2v($key)) }
sub redden ($blk, $key) { v2s(s2v($blk) »-» s2v($key)) }
 
my $red = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
my $key = "Vigenere Cipher!!!";
 
say $red;
say my $black = blacken($red, $key);
say redden($black, $key);</lang>
{{out}}
<pre>Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre>
This is a natural job for hyperoperators, which can vectorize any operator.
For infix operators the pointy end indicates which side to "dwim", repeating
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}}==
Line 2,907 ⟶ 2,884:
"BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH"
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2015-11-14}}
<lang perl6>sub s2v ($s) { $s.uc.comb(/ <[ A..Z ]> /)».ord »-» 65 }
sub v2s (@v) { (@v »%» 26 »+» 65)».chr.join }
 
sub blacken ($red, $key) { v2s(s2v($red) »+» s2v($key)) }
sub redden ($blk, $key) { v2s(s2v($blk) »-» s2v($key)) }
 
my $red = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
my $key = "Vigenere Cipher!!!";
 
say $red;
say my $black = blacken($red, $key);
say redden($black, $key);</lang>
{{out}}
<pre>Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre>
This is a natural job for hyperoperators, which can vectorize any operator.
For infix operators the pointy end indicates which side to "dwim", repeating
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|Red}}==
note: this program is much longer than it needed to be - because i couldn't resist
Line 3,505 ⟶ 3,507:
Key: Vigenère cipher
Plain Text: This is a ünicode string 😃
Cipher Text: Š±°¸nıÅeacŅ¾±¨ÁšÁ�®g¸Âĺ»³gc🙌
Decoded: This is a ünicode string 😃</pre>
 
10,333

edits

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