2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,16 +1,17 @@
|
|||
{{omit from|GUISS|would need to install an application that could do this}}
|
||||
{{omit from|Openscad}}
|
||||
|
||||
Implement a [[wp:Vigen%C3%A8re_cipher|Vigenère cypher]],
|
||||
both encryption and decryption.
|
||||
;Task:
|
||||
Implement a [[wp:Vigen%C3%A8re_cipher|Vigenère cypher]], both encryption and decryption.
|
||||
|
||||
The program should handle keys and text of unequal length,
|
||||
and should capitalize everything and discard non-alphabetic characters. <br>
|
||||
(If your program handles non-alphabetic characters in another way,
|
||||
make a note of it.)
|
||||
|
||||
;See also:
|
||||
* [[Caesar cipher]]
|
||||
* [[Rot-13]]
|
||||
* [[Substitution Cipher]]
|
||||
<br>
|
||||
|
||||
;Related tasks:
|
||||
* [[Caesar cipher]]
|
||||
* [[Rot-13]]
|
||||
* [[Substitution Cipher]]
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,56 +1,113 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <getopt.h>
|
||||
|
||||
void upper_case(char *src)
|
||||
#define NUMLETTERS 26
|
||||
#define BUFSIZE 4096
|
||||
|
||||
char *get_input(void);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
while (*src != '\0') {
|
||||
if (islower(*src)) *src &= ~0x20;
|
||||
src++;
|
||||
char const usage[] = "Usage: vinigere [-d] key";
|
||||
char sign = 1;
|
||||
char const plainmsg[] = "Plain text: ";
|
||||
char const cryptmsg[] = "Cipher text: ";
|
||||
bool encrypt = true;
|
||||
int opt;
|
||||
|
||||
while ((opt = getopt(argc, argv, "d")) != -1) {
|
||||
switch (opt) {
|
||||
case 'd':
|
||||
sign = -1;
|
||||
encrypt = false;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Unrecogized command line argument:'-%i'\n", opt);
|
||||
fprintf(stderr, "\n%s\n", usage);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char* encipher(const char *src, char *key, int is_encode)
|
||||
{
|
||||
int i, klen, slen;
|
||||
char *dest;
|
||||
if (argc - optind != 1) {
|
||||
fprintf(stderr, "%s requires one argument and one only\n", argv[0]);
|
||||
fprintf(stderr, "\n%s\n", usage);
|
||||
return 1;
|
||||
}
|
||||
|
||||
dest = strdup(src);
|
||||
upper_case(dest);
|
||||
upper_case(key);
|
||||
|
||||
/* strip out non-letters */
|
||||
for (i = 0, slen = 0; dest[slen] != '\0'; slen++)
|
||||
if (isupper(dest[slen]))
|
||||
dest[i++] = dest[slen];
|
||||
// Convert argument into array of shifts
|
||||
char const *const restrict key = argv[optind];
|
||||
size_t const keylen = strlen(key);
|
||||
char shifts[keylen];
|
||||
|
||||
dest[slen = i] = '\0'; /* null pad it, make it safe to use */
|
||||
|
||||
klen = strlen(key);
|
||||
for (i = 0; i < slen; i++) {
|
||||
if (!isupper(dest[i])) continue;
|
||||
dest[i] = 'A' + (is_encode
|
||||
? dest[i] - 'A' + key[i % klen] - 'A'
|
||||
: dest[i] - key[i % klen] + 26) % 26;
|
||||
char const *restrict plaintext = NULL;
|
||||
for (size_t i = 0; i < keylen; i++) {
|
||||
if (!(isalpha(key[i]))) {
|
||||
fprintf(stderr, "Invalid key\n");
|
||||
return 2;
|
||||
}
|
||||
char const charcase = (isupper(key[i])) ? 'A' : 'a';
|
||||
// If decrypting, shifts will be negative.
|
||||
// This line would turn "bacon" into {1, 0, 2, 14, 13}
|
||||
shifts[i] = (key[i] - charcase) * sign;
|
||||
}
|
||||
|
||||
return dest;
|
||||
do {
|
||||
fflush(stdout);
|
||||
// Print "Plain text: " if encrypting and "Cipher text: " if
|
||||
// decrypting
|
||||
printf("%s", (encrypt) ? plainmsg : cryptmsg);
|
||||
plaintext = get_input();
|
||||
if (plaintext == NULL) {
|
||||
fprintf(stderr, "Error getting input\n");
|
||||
return 4;
|
||||
}
|
||||
} while (strcmp(plaintext, "") == 0); // Reprompt if entry is empty
|
||||
|
||||
size_t const plainlen = strlen(plaintext);
|
||||
|
||||
char* const restrict ciphertext = calloc(plainlen + 1, sizeof *ciphertext);
|
||||
if (ciphertext == NULL) {
|
||||
fprintf(stderr, "Memory error\n");
|
||||
return 5;
|
||||
}
|
||||
|
||||
for (size_t i = 0, j = 0; i < plainlen; i++) {
|
||||
// Skip non-alphabetical characters
|
||||
if (!(isalpha(plaintext[i]))) {
|
||||
ciphertext[i] = plaintext[i];
|
||||
continue;
|
||||
}
|
||||
// Check case
|
||||
char const charcase = (isupper(plaintext[i])) ? 'A' : 'a';
|
||||
// Wrapping conversion algorithm
|
||||
ciphertext[i] = ((plaintext[i] + shifts[j] - charcase + NUMLETTERS) % NUMLETTERS) + charcase;
|
||||
j = (j+1) % keylen;
|
||||
}
|
||||
ciphertext[plainlen] = '\0';
|
||||
printf("%s%s\n", (encrypt) ? cryptmsg : plainmsg, ciphertext);
|
||||
|
||||
free(ciphertext);
|
||||
// Silence warnings about const not being maintained in cast to void*
|
||||
free((char*) plaintext);
|
||||
return 0;
|
||||
}
|
||||
char *get_input(void) {
|
||||
|
||||
int main()
|
||||
{
|
||||
const char *str = "Beware the Jabberwock, my son! The jaws that bite, "
|
||||
"the claws that catch!";
|
||||
const char *cod, *dec;
|
||||
char key[] = "VIGENERECIPHER";
|
||||
char *const restrict buf = malloc(BUFSIZE * sizeof (char));
|
||||
if (buf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
printf("Text: %s\n", str);
|
||||
printf("key: %s\n", key);
|
||||
fgets(buf, BUFSIZE, stdin);
|
||||
|
||||
cod = encipher(str, key, 1); printf("Code: %s\n", cod);
|
||||
dec = encipher(cod, key, 0); printf("Back: %s\n", dec);
|
||||
// Get rid of newline
|
||||
size_t const len = strlen(buf);
|
||||
if (buf[len - 1] == '\n') buf[len - 1] = '\0';
|
||||
|
||||
/* free(dec); free(cod); */ /* nah */
|
||||
return 0;
|
||||
return buf;
|
||||
}
|
||||
|
|
|
|||
28
Task/Vigen-re-cipher/Elixir/vigen-re-cipher.elixir
Normal file
28
Task/Vigen-re-cipher/Elixir/vigen-re-cipher.elixir
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
defmodule VigenereCipher do
|
||||
@base ?A
|
||||
@size ?Z - @base + 1
|
||||
|
||||
def encrypt(text, key), do: crypt(text, key, 1)
|
||||
|
||||
def decrypt(text, key), do: crypt(text, key, -1)
|
||||
|
||||
defp crypt(text, key, dir) do
|
||||
text = String.upcase(text) |> String.replace(~r/[^A-Z]/, "") |> to_char_list
|
||||
key_iterator = String.upcase(key) |> String.replace(~r/[^A-Z]/, "") |> to_char_list
|
||||
|> Enum.map(fn c -> (c - @base) * dir end) |> Stream.cycle
|
||||
Enum.zip(text, key_iterator)
|
||||
|> Enum.reduce('', fn {char, offset}, ciphertext ->
|
||||
[rem(char - @base + offset + @size, @size) + @base | ciphertext]
|
||||
end)
|
||||
|> Enum.reverse |> List.to_string
|
||||
end
|
||||
end
|
||||
|
||||
plaintext = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
key = "Vigenere cipher"
|
||||
ciphertext = VigenereCipher.encrypt(plaintext, key)
|
||||
recovered = VigenereCipher.decrypt(ciphertext, key)
|
||||
|
||||
IO.puts "Original: #{plaintext}"
|
||||
IO.puts "Encrypted: #{ciphertext}"
|
||||
IO.puts "Decrypted: #{recovered}"
|
||||
|
|
@ -1,29 +1,24 @@
|
|||
<html><head><title>Vigenère</title></head>
|
||||
<body><pre id='x'></pre>
|
||||
<script type="application/javascript">
|
||||
function disp(x) {
|
||||
var e = document.createTextNode(x + '\n');
|
||||
document.getElementById('x').appendChild(e);
|
||||
// helpers
|
||||
// helper
|
||||
function ordA(a) {
|
||||
return a.charCodeAt(0) - 65;
|
||||
}
|
||||
|
||||
function ord(x) { return x.charCodeAt(0) }
|
||||
function chr(x) { return String.fromCharCode(x) }
|
||||
function rot(a, b, decode) {
|
||||
return decode ? chr((26 + ord(a) - ord(b)) % 26 + ord('A'))
|
||||
: chr((26 + ord(a) + ord(b) - ord('A') * 2) % 26 + ord('A')) }
|
||||
|
||||
function trans(msg, key, decode) {
|
||||
var i = 0;
|
||||
key = key.toUpperCase();
|
||||
msg = msg.toUpperCase().replace(/[^A-Z]/g, '');
|
||||
return msg.replace(/([A-Z])/g,
|
||||
function($1) { return rot($1, key[i++ % key.length], decode) });
|
||||
// vigenere
|
||||
function vigenere(text, key, decode) {
|
||||
var i = 0, b;
|
||||
key = key.toUpperCase().replace(/[^A-Z]/g, '');
|
||||
return text.toUpperCase().replace(/[^A-Z]/g, '').replace(/[A-Z]/g, function(a) {
|
||||
b = key[i++ % key.length];
|
||||
return String.fromCharCode(((ordA(a) + (decode ? 26 - ordA(b) : ordA(b))) % 26 + 65));
|
||||
});
|
||||
}
|
||||
|
||||
var msg = "The quick brown fox Jumped over the lazy Dog the lazy dog lazy dog dog";
|
||||
var key = 'VIGENERECIPHER';
|
||||
var enc = trans(msg, key);
|
||||
var dec = trans(enc, key, 'decipher');
|
||||
// example
|
||||
var text = "The quick brown fox Jumped over the lazy Dog the lazy dog lazy dog dog";
|
||||
var key = 'alex';
|
||||
var enc = vigenere(text,key);
|
||||
var dec = vigenere(enc,key,true);
|
||||
|
||||
disp("Original:" + msg + "\nEncoded: " + enc + "\nDecoded: " + dec);
|
||||
</script></body></html>
|
||||
console.log(enc);
|
||||
console.log(dec);
|
||||
|
|
|
|||
85
Task/Vigen-re-cipher/PowerShell/vigen-re-cipher.psh
Normal file
85
Task/Vigen-re-cipher/PowerShell/vigen-re-cipher.psh
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
# Author: D. Cudnohufsky
|
||||
function Get-VigenereCipher
|
||||
{
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Text,
|
||||
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Key,
|
||||
|
||||
[switch] $Decode
|
||||
)
|
||||
|
||||
begin
|
||||
{
|
||||
$map = [char]'A'..[char]'Z'
|
||||
}
|
||||
|
||||
process
|
||||
{
|
||||
$Key = $Key -replace '[^a-zA-Z]',''
|
||||
$Text = $Text -replace '[^a-zA-Z]',''
|
||||
|
||||
$keyChars = $Key.toUpper().ToCharArray()
|
||||
$Chars = $Text.toUpper().ToCharArray()
|
||||
|
||||
function encode
|
||||
{
|
||||
|
||||
param
|
||||
(
|
||||
$Char,
|
||||
$keyChar,
|
||||
$Alpha = [char]'A'..[char]'Z'
|
||||
)
|
||||
|
||||
$charIndex = $Alpha.IndexOf([int]$Char)
|
||||
$keyIndex = $Alpha.IndexOf([int]$keyChar)
|
||||
$NewIndex = ($charIndex + $KeyIndex) - $Alpha.Length
|
||||
$Alpha[$NewIndex]
|
||||
|
||||
}
|
||||
|
||||
function decode
|
||||
{
|
||||
|
||||
param
|
||||
(
|
||||
$Char,
|
||||
$keyChar,
|
||||
$Alpha = [char]'A'..[char]'Z'
|
||||
)
|
||||
|
||||
$charIndex = $Alpha.IndexOf([int]$Char)
|
||||
$keyIndex = $Alpha.IndexOf([int]$keyChar)
|
||||
$int = $charIndex - $keyIndex
|
||||
if ($int -lt 0) { $NewIndex = $int + $Alpha.Length }
|
||||
else { $NewIndex = $int }
|
||||
$Alpha[$NewIndex]
|
||||
}
|
||||
|
||||
while ( $keyChars.Length -lt $Chars.Length )
|
||||
{
|
||||
$keyChars = $keyChars + $keyChars
|
||||
}
|
||||
|
||||
for ( $i = 0; $i -lt $Chars.Length; $i++ )
|
||||
{
|
||||
|
||||
if ( [int]$Chars[$i] -in $map -and [int]$keyChars[$i] -in $map )
|
||||
{
|
||||
if ($Decode) {$Chars[$i] = decode $Chars[$i] $keyChars[$i] $map}
|
||||
else {$Chars[$i] = encode $Chars[$i] $keyChars[$i] $map}
|
||||
|
||||
$Chars[$i] = [char]$Chars[$i]
|
||||
[string]$OutText += $Chars[$i]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$OutText
|
||||
$OutText = $null
|
||||
}
|
||||
}
|
||||
|
|
@ -4,16 +4,16 @@ def encrypt(message, key):
|
|||
|
||||
# convert to uppercase.
|
||||
# strip out non-alpha characters.
|
||||
message = filter(lambda _: _.isalpha(), message.upper())
|
||||
message = filter(str.isalpha, message.upper())
|
||||
|
||||
# single letter encrpytion.
|
||||
def enc(c,k): return chr(((ord(k) + ord(c)) % 26) + ord('A'))
|
||||
def enc(c,k): return chr(((ord(k) + ord(c) - 2*ord('A')) % 26) + ord('A'))
|
||||
|
||||
return "".join(starmap(enc, zip(message, cycle(key))))
|
||||
|
||||
def decrypt(message, key):
|
||||
|
||||
# single letter decryption.
|
||||
def dec(c,k): return chr(((ord(c) - ord(k)) % 26) + ord('A'))
|
||||
def dec(c,k): return chr(((ord(c) - ord(k) - 2*ord('A')) % 26) + ord('A'))
|
||||
|
||||
return "".join(starmap(dec, zip(message, cycle(key))))
|
||||
|
|
|
|||
|
|
@ -1,31 +1,29 @@
|
|||
/*REXX program encrypts uppercased text using the Vigenère cypher. */
|
||||
/*REXX program encrypts (and displays) uppercased text using the Vigenère cypher.*/
|
||||
@.1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
L=length(@.1)
|
||||
do j=2 to L; jm=j-1; q=@.jm
|
||||
@.j=substr(q,2,L-1)left(q,1)
|
||||
do j=2 to L; jm=j-1; q=@.jm
|
||||
@.j=substr(q, 2, L-1)left(q, 1)
|
||||
end /*j*/
|
||||
|
||||
cypher = space('WHOOP DE DOO NO BIG DEAL HERE OR THERE',0)
|
||||
cypher = space('WHOOP DE DOO NO BIG DEAL HERE OR THERE', 0)
|
||||
oMsg = 'People solve problems by trial and error; judgement helps pick the trial.'
|
||||
oMsgU = oMsg; upper oMsgU
|
||||
cypher_=copies(cypher,length(oMsg)%length(cypher))
|
||||
cypher_= copies(cypher, length(oMsg) % length(cypher) )
|
||||
say ' original text =' oMsg
|
||||
xMsg=Ncypher(oMsgU)
|
||||
say ' cyphered text =' xMsg
|
||||
bMsg=Dcypher(xMsg)
|
||||
say 're-cyphered text =' bMsg
|
||||
exit
|
||||
/*───────────────────────────────Ncypher subroutine─────────────────────*/
|
||||
Ncypher: parse arg stuff; #=1; nMsg=
|
||||
do i=1 for length(stuff); x=substr(stuff,i,1) /*pick off 1 char.*/
|
||||
if \datatype(x,'U') then iterate /*not a letter? Then ignore it.*/
|
||||
j=pos(x,@.1); nMsg=nMsg || substr(@.j,pos(substr(cypher_,#,1),@.1),1)
|
||||
#=#+1 /*bump the character counter. */
|
||||
end
|
||||
return nMsg
|
||||
/*───────────────────────────────Dcypher subroutine──────────────────────*/
|
||||
Dcypher: parse arg stuff; #=1; dMsg=
|
||||
do i=1 for length(stuff); x=substr(cypher_,i,1) /*pick off 1 char.*/
|
||||
j=pos(x,@.1); dMsg=dMsg || substr(@.1, pos(substr(stuff,i,1),@.j),1)
|
||||
end
|
||||
return dMsg
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
Ncypher: parse arg x; nMsg=; #=1 /*unsupported char? ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
|
||||
do i=1 for length(x); j=pos(substr(x,i,1), @.1); if j==0 then iterate
|
||||
nMsg=nMsg || substr(@.j, pos( substr( cypher_, #, 1), @.1), 1); #=#+1
|
||||
end /*j*/
|
||||
return nMsg
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
Dcypher: parse arg x; dMsg=
|
||||
do i=1 for length(x); j=pos(substr(cypher_, i, 1), @.1)
|
||||
dMsg=dMsg || substr(@.1, pos( substr(x, i, 1), @.j), 1)
|
||||
end /*j*/
|
||||
return dMsg
|
||||
|
|
|
|||
|
|
@ -1,30 +1,29 @@
|
|||
/*REXX program encrypts uppercased text using the Vigenère cypher. */
|
||||
@abc='abcdefghijklmnopqrstuvwxyz'; @abcU=@abc; upper @abcU
|
||||
/*REXX program encrypts (and displays) most text using the Vigenère cypher. */
|
||||
@abc= 'abcdefghijklmnopqrstuvwxyz'; @abcU=@abc; upper @abcU
|
||||
@.1 = @abcU || @abc'0123456789~`!@#$%^&*()_-+={}|[]\:;<>?,./" '''
|
||||
L=length(@.1)
|
||||
do j=2 to length(@.1); jm=j-1; q=@.jm
|
||||
@.j=substr(q,2,length(@.1)-1)left(q,1)
|
||||
do j=2 to length(@.1); jm=j-1; q=@.jm
|
||||
@.j=substr(q, 2, L-1)left(q, 1)
|
||||
end /*j*/
|
||||
|
||||
cypher = space('WHOOP DE DOO NO BIG DEAL HERE OR THERE',0)
|
||||
cypher = space('WHOOP DE DOO NO BIG DEAL HERE OR THERE', 0)
|
||||
oMsg = 'Making things easy is just knowing the shortcuts. --- Gerard J. Schildberger'
|
||||
cypher_=copies(cypher,length(oMsg)%length(cypher))
|
||||
cypher_= copies(cypher, length(oMsg) % length(cypher) )
|
||||
say ' original text =' oMsg
|
||||
xMsg=Ncypher(oMsg)
|
||||
say ' cyphered text =' xMsg
|
||||
bMsg=Dcypher(xMsg)
|
||||
say 're-cyphered text =' bMsg
|
||||
exit
|
||||
/*───────────────────────────────Ncypher subroutine─────────────────────*/
|
||||
Ncypher: parse arg stuff; #=1; nMsg=
|
||||
do i=1 for length(stuff); x=substr(stuff,i,1) /*pick off 1 char.*/
|
||||
j=pos(x,@.1); if j==0 then iterate /*character not supported? Ignore*/
|
||||
nMsg=nMsg || substr(@.j,pos(substr(cypher_,#,1),@.1),1); #=#+1
|
||||
end /*j*/
|
||||
return nMsg
|
||||
/*───────────────────────────────Dcypher subroutine──────────────────────*/
|
||||
Dcypher: parse arg stuff; #=1; dMsg=
|
||||
do i=1 for length(stuff); x=substr(cypher_,i,1) /*pick off 1 char.*/
|
||||
j=pos(x,@.1); dMsg=dMsg || substr(@.1, pos(substr(stuff,i,1),@.j),1)
|
||||
end /*j*/
|
||||
return dMsg
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
Ncypher: parse arg x; nMsg=; #=1 /*unsupported char? ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
|
||||
do i=1 for length(x); j=pos(substr(x,i,1), @.1); if j==0 then iterate
|
||||
nMsg=nMsg || substr(@.j, pos( substr( cypher_, #, 1), @.1), 1); #=#+1
|
||||
end /*j*/
|
||||
return nMsg
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
Dcypher: parse arg x; dMsg=
|
||||
do i=1 for length(x); j=pos(substr(cypher_, i, 1), @.1)
|
||||
dMsg=dMsg || substr(@.1, pos( substr(x, i, 1), @.j), 1)
|
||||
end /*j*/
|
||||
return dMsg
|
||||
|
|
|
|||
|
|
@ -1,52 +1,28 @@
|
|||
class Vigenere(val key: String) {
|
||||
object Vigenere {
|
||||
|
||||
val key = "LEMON"
|
||||
val chars = 'A' to 'Z'
|
||||
|
||||
private def rotate(p: Int, s: IndexedSeq[Char]) = s.drop(p) ++ s.take(p)
|
||||
val vSquare = (for (i <- Range(0, 25)) yield rotate(i, chars)).flatten
|
||||
|
||||
private val chars = 'A' to 'Z'
|
||||
def encrypt(s: String) = enrOrDecode(0, s.toUpperCase, encode)
|
||||
def decrypt(s: String) = enrOrDecode(0, s.toUpperCase, decode)
|
||||
|
||||
private val vSquare = (chars ++
|
||||
rotate(1, chars) ++
|
||||
rotate(2, chars) ++
|
||||
rotate(3, chars) ++
|
||||
rotate(4, chars) ++
|
||||
rotate(5, chars) ++
|
||||
rotate(6, chars) ++
|
||||
rotate(7, chars) ++
|
||||
rotate(8, chars) ++
|
||||
rotate(9, chars) ++
|
||||
rotate(10, chars) ++
|
||||
rotate(11, chars) ++
|
||||
rotate(12, chars) ++
|
||||
rotate(13, chars) ++
|
||||
rotate(14, chars) ++
|
||||
rotate(15, chars) ++
|
||||
rotate(16, chars) ++
|
||||
rotate(17, chars) ++
|
||||
rotate(18, chars) ++
|
||||
rotate(19, chars) ++
|
||||
rotate(20, chars) ++
|
||||
rotate(21, chars) ++
|
||||
rotate(22, chars) ++
|
||||
rotate(23, chars) ++
|
||||
rotate(24, chars) ++
|
||||
rotate(25, chars))
|
||||
|
||||
private var encIndex = -1
|
||||
private var decIndex = -1
|
||||
|
||||
def encode(c: Char) = {
|
||||
encIndex += 1
|
||||
if (encIndex == key.length) encIndex = 0
|
||||
if (chars.contains(c)) vSquare((c - 'A') * 26 + key(encIndex) - 'A') else c
|
||||
private def enrOrDecode(i: Int , in: String, op:(Char, Int) => Char): String = {
|
||||
if (in.length == 0) in else {
|
||||
val index = if (i == key.length) 0 else i
|
||||
(if (chars.contains(in.head)) op(in.head, index) else in.head) + enrOrDecode(index + 1, in.tail, op)
|
||||
}
|
||||
}
|
||||
|
||||
def decode(c: Char) = {
|
||||
decIndex += 1
|
||||
if (decIndex == key.length) decIndex = 0
|
||||
if (chars.contains(c)) {
|
||||
val baseIndex = (key(decIndex) - 'A') * 26
|
||||
private def encode(c: Char, index: Int): Char = {
|
||||
vSquare((c - 'A') * 26 + key(index) - 'A')
|
||||
}
|
||||
|
||||
private def decode(c: Char, index: Int) = {
|
||||
val baseIndex = (key(index) - 'A') * 26
|
||||
val nextIndex = vSquare.indexOf(c, baseIndex)
|
||||
chars(nextIndex - baseIndex)
|
||||
} else c
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
val text = "ATTACKATDAWN"
|
||||
val myVigenere = new Vigenere("LEMON")
|
||||
val encoded = text.map(c => myVigenere.encode(c))
|
||||
println("Plaintext => " + text)
|
||||
println("Ciphertext => " + encoded)
|
||||
println("Decrypted => " + encoded.map(c => myVigenere.decode(c)))
|
||||
val text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
val encoded = Vigenere.encrypt(text)
|
||||
val decoded = Vigenere.decrypt(text)
|
||||
println("Plain text => " + text)
|
||||
println("Cipher text => " + encoded)
|
||||
println("Decrypted => " + decoded)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue