September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,56 +0,0 @@
#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

@ -1,4 +0,0 @@
Text: Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
key: VIGENERECIPHER
Code: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Back: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH

View file

@ -1,26 +0,0 @@
(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,3 +0,0 @@
msg: Beware the Jabberwock... The jaws that... the claws that catch!
enc: WMCEEIKLGRPIFVMEUGXXYILILZXYVBZLRGCEYAIOEKXIZGU
dec: BEWARETHEJABBERWOCKTHEJAWSTHATTHECLAWSTHATCATCH

View file

@ -0,0 +1,26 @@
// version 1.1.3
fun vigenere(text: String, key: String, encrypt: Boolean = true): String {
val t = if (encrypt) text.toUpperCase() else text
val sb = StringBuilder()
var ki = 0
for (c in t) {
if (c !in 'A'..'Z') continue
val ci = if (encrypt)
(c.toInt() + key[ki].toInt() - 130) % 26
else
(c.toInt() - key[ki].toInt() + 26) % 26
sb.append((ci + 65).toChar())
ki = (ki + 1) % key.length
}
return sb.toString()
}
fun main(args: Array<String>) {
val key = "VIGENERECIPHER"
val text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
val encoded = vigenere(text, key)
println(encoded)
val decoded = vigenere(encoded, key, false)
println(decoded)
}

View file

@ -0,0 +1,194 @@
/* Rexx */
Do
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
key = 'LEMON'
pt = 'Attack at dawn!'
Call test key, pt
key = 'N'
Call test key, pt
key = 'B'
Call test key, pt
pt = alpha
key = 'A'
Call test key, pt
pt = sampledata()
key = 'Hamlet; Prince of Denmark'
Call test key, pt
Return
End
Exit
vigenere:
Procedure Expose alpha
Do
Parse upper Arg meth, key, text
Select
When 'ENCIPHER'~abbrev(meth, 1) = 1 then df = 1
When 'DECIPHER'~abbrev(meth, 1) = 1 then df = -1
Otherwise Do
Say meth 'invalid. Must be "ENCIPHER" or "DECIPHER"'
Exit
End
End
text = stringscrubber(text)
key = stringscrubber(key)
code = ''
Do l_ = 1 to text~length()
M = alpha~pos(text~substr(l_, 1)) - 1
k_ = (l_ - 1) // key~length()
K = alpha~pos(key~substr(k_ + 1, 1)) - 1
C = mod((M + K * df), alpha~length())
C = alpha~substr(C + 1, 1)
code = code || C
End l_
Return code
Return
End
Exit
vigenere_encipher:
Procedure Expose alpha
Do
Parse upper Arg key, plaintext
Return vigenere('ENCIPHER', key, plaintext)
End
Exit
vigenere_decipher:
Procedure Expose alpha
Do
Parse upper Arg key, ciphertext
Return vigenere('DECIPHER', key, ciphertext)
End
Exit
mod:
Procedure
Do
Parse Arg N, D
Return (D + (N // D)) // D
End
Exit
stringscrubber:
Procedure Expose alpha
Do
Parse upper Arg cleanup
cleanup = cleanup~space(0)
Do label f_ forever
x_ = cleanup~verify(alpha)
If x_ = 0 then Leave f_
cleanup = cleanup~changestr(cleanup~substr(x_, 1), '')
end f_
Return cleanup
End
Exit
test:
Procedure Expose alpha
Do
Parse Arg key, pt
ct = vigenere_encipher(key, pt)
Call display ct
dt = vigenere_decipher(key, ct)
Call display dt
Return
End
Exit
display:
Procedure
Do
Parse Arg text
line = ''
o_ = 0
Do c_ = 1 to text~length()
b_ = o_ // 5
o_ = o_ + 1
If b_ = 0 then line = line' '
line = line || text~substr(c_, 1)
End c_
Say '....+....|'~copies(8)
Do label l_ forever
Parse Var line w1 w2 w3 w4 w5 w6 W7 w8 w9 w10 w11 w12 line
pline = w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12
Say pline~strip()
If line~strip()~length() = 0 then Leave l_
End l_
Say
Return
End
Exit
sampledata:
Procedure
Do
NL = '0a'x
X = 0
antic_disposition. = ''
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "To be, or not to be--that is the question:"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "Whether 'tis nobler in the mind to suffer"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "The slings and arrows of outrageous fortune"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "Or to take arms against a sea of troubles"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "And by opposing end them. To die, to sleep--"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "No more--and by a sleep to say we end"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "The heartache, and the thousand natural shocks"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "That flesh is heir to. 'Tis a consummation"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "Devoutly to be wished. To die, to sleep--"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "To sleep--perchance to dream: ay, there's the rub,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "For in that sleep of death what dreams may come"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "When we have shuffled off this mortal coil,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "Must give us pause. There's the respect"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "That makes calamity of so long life."
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "For who would bear the whips and scorns of time,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "Th' oppressor's wrong, the proud man's contumely"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "The pangs of despised love, the law's delay,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "The insolence of office, and the spurns"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "That patient merit of th' unworthy takes,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "When he himself might his quietus make"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "With a bare bodkin? Who would fardels bear,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "To grunt and sweat under a weary life,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "But that the dread of something after death,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "The undiscovered country, from whose bourn"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "No traveller returns, puzzles the will,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "And makes us rather bear those ills we have"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "Than fly to others that we know not of?"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "Thus conscience does make cowards of us all,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "And thus the native hue of resolution"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "Is sicklied o'er with the pale cast of thought,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "And enterprise of great pith and moment"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "With this regard their currents turn awry"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "And lose the name of action. -- Soft you now,"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "The fair Ophelia! -- Nymph, in thy orisons"
X = X + 1; antic_disposition.0 = X; antic_disposition.X = "Be all my sins remembered."
melancholy_dane = ''
Do l_ = 1 for antic_disposition.0
melancholy_dane = melancholy_dane || antic_disposition.l_ || NL
End l_
Return melancholy_dane
End
Exit

View file

@ -0,0 +1,22 @@
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})

View file

@ -1,12 +1,12 @@
func s2v(s) { s.uc.scan(/[A-Z]/)»ord»() »-» 65 };
func v2s(v) { (v »%» 26 »+» 65)»chr»().join };
func blacken (red, key) { v2s(s2v(red) »+« s2v(key)) };
func redden (blk, key) { v2s(s2v(blk) »-« s2v(key)) };
var red = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
var key = "Vigenere Cipher!!!";
say red;
say (var black = blacken(red, key));
say redden(black, key);
func s2v(s) { s.uc.scan(/[A-Z]/).map{.ord} »-» 65 }
func v2s(v) { v »%» 26 »+» 65 -> map{.chr}.join }
 
func blacken (red, key) { v2s(s2v(red) »+« s2v(key)) }
func redden (blk, key) { v2s(s2v(blk) »-« s2v(key)) }
 
var red = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
var key = "Vigenere Cipher!!!"
 
say red
say (var black = blacken(red, key))
say redden(black, key)

View file

@ -0,0 +1,12 @@
fcn encipher(src,key,is_encode){
upperCase:=["A".."Z"].pump(String);
src=src.toUpper().inCommon(upperCase); // only uppercase
key=key.toUpper().inCommon(upperCase).pump(List,"toAsc");
const A="A".toAsc();
klen:=Walker.cycle(key.len()); // 0,1,2,3,..,keyLen-1,0,1,2,3, ...
src.pump(String,'wrap(c){ i:=klen.next(); c=c.toAsc();
(A + ( if(is_encode) c - A + key[i] - A;
else c - key[i] + 26 ) % 26).toChar()
});
}

View file

@ -0,0 +1,9 @@
str := "Beware the Jabberwock, my son! The jaws that bite, "
"the claws that catch!";
key := "Vigenere Cipher";
println("Text: ", str);
println("key: ", key);
cod := encipher(str, key, True); println("Code: ", cod);
dec := encipher(cod, key, False); println("Back: ", dec);