Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,14 @@
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. (If your program handles non-alphabetic characters in another way, make a note of it.)
{{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.
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:
* [[Rot-13]]
* [[Vigenère Cipher/Cryptanalysis]]

View file

@ -1,2 +1,4 @@
---
category:
- String manipulation
note: Encryption

View file

@ -0,0 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void upper_case(char *src)
{
while (*src != '\0') {
if (islower(*src)) *src &= ~0x20;
src++;
}
}
char* encipher(const char *src, char *key, int is_encode)
{
int i, klen, slen;
char *dest;
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];
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;
}
return dest;
}
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";
printf("Text: %s\n", str);
printf("key: %s\n", key);
cod = encipher(str, key, 1); printf("Code: %s\n", cod);
dec = encipher(cod, key, 0); printf("Back: %s\n", dec);
/* free(dec); free(cod); */ /* nah */
return 0;
}

View file

@ -0,0 +1,26 @@
(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))

View file

@ -1,21 +1,21 @@
import std.stdio, std.string;
string encrypt(in string txt, in string key) pure
string encrypt(in string txt, in string key) pure @safe
in {
assert(key.removechars("^A-Z") == key);
} body {
string res;
foreach (immutable i, immutable c; toUpper(txt).removechars("^A-Z"))
foreach (immutable i, immutable c; txt.toUpper.removechars("^A-Z"))
res ~= (c + key[i % $] - 2 * 'A') % 26 + 'A';
return res;
}
string decrypt(in string txt, in string key) pure
string decrypt(in string txt, in string key) pure @safe
in {
assert(key.removechars("^A-Z") == key);
} body {
string res;
foreach (immutable i, immutable c; toUpper(txt).removechars("^A-Z"))
foreach (immutable i, immutable c; txt.toUpper.removechars("^A-Z"))
res ~= (c - key[i % $] + 26) % 26 + 'A';
return res;
}

View file

@ -1,19 +1,20 @@
import std.stdio, std.range, std.ascii, std.string, std.algorithm,
std.conv;
enum mod = (in int m, in int n) pure nothrow => ((m % n) + n) % n;
enum mod = (in int m, in int n) pure nothrow @safe @nogc =>
((m % n) + n) % n;
enum _s2v = (in string s) pure /*nothrow*/ =>
enum _s2v = (in string s) pure /*nothrow*/ @safe =>
s.toUpper.removechars("^A-Z").map!q{ a - 'A' };
string _v2s(R)(R v) pure /*nothrow*/ {
string _v2s(R)(R v) pure /*nothrow*/ @safe {
return v.map!(x => uppercase[x.mod(26)]).text;
}
enum encrypt = (in string txt, in string key) pure /*nothrow*/ =>
enum encrypt = (in string txt, in string key) pure /*nothrow*/ @safe =>
txt._s2v.zip(key._s2v.cycle).map!q{ a[0] + a[1] }._v2s;
enum decrypt = (in string txt, in string key) pure /*nothrow*/ =>
enum decrypt = (in string txt, in string key) pure /*nothrow*/ @safe =>
txt._s2v.zip(key._s2v.cycle).map!q{ a[0] - a[1] }._v2s;
void main() {