Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -9,6 +9,8 @@ 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:
|
||||
;See also:
|
||||
* [[Caesar cipher]]
|
||||
* [[Rot-13]]
|
||||
* [[Vigenère Cipher/Cryptanalysis]]
|
||||
* [[Substitution Cipher]]
|
||||
<br>
|
||||
|
|
|
|||
4
Task/Vigen-re-cipher/Befunge/vigen-re-cipher-1.bf
Normal file
4
Task/Vigen-re-cipher/Befunge/vigen-re-cipher-1.bf
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
"VIGENERECIPHER">>>>1\:!v>"A"-\:00p0v
|
||||
>>!#:0#-0#1g#,*#<+:v:-1$_^#!:\+1g00p<
|
||||
\"{"\v>9+2*%"A"+^>$>~>:48*\`#@_::"`"`
|
||||
*84*`<^4+"4"+g0\_^#!+`*55\`\0::-"A"-*
|
||||
4
Task/Vigen-re-cipher/Befunge/vigen-re-cipher-2.bf
Normal file
4
Task/Vigen-re-cipher/Befunge/vigen-re-cipher-2.bf
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
"VIGENERECIPHER">>>>1\:!v>"A"-\:00p0v
|
||||
>>!#:0#-0#1g#,*#<+:v:-1$_^#!:\+1g00p<
|
||||
\"{"\v>9+2*%"A"+^>$>~>:48*\`#@_::"`"`
|
||||
*84*`<^4+"4"-g0\_^#!+`*55\`\0::-"A"-*
|
||||
|
|
@ -1,20 +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 @safe @nogc =>
|
||||
immutable mod = (in int m, in int n) pure nothrow @safe @nogc =>
|
||||
((m % n) + n) % n;
|
||||
|
||||
enum _s2v = (in string s) pure /*nothrow*/ @safe =>
|
||||
immutable _s2v = (in string s) pure /*nothrow*/ @safe =>
|
||||
s.toUpper.removechars("^A-Z").map!q{ a - 'A' };
|
||||
|
||||
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*/ @safe =>
|
||||
immutable 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*/ @safe =>
|
||||
immutable 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() {
|
||||
|
|
|
|||
62
Task/Vigen-re-cipher/Fortran/vigen-re-cipher.f
Normal file
62
Task/Vigen-re-cipher/Fortran/vigen-re-cipher.f
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
program vigenere_cipher
|
||||
implicit none
|
||||
|
||||
character(80) :: plaintext = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!", &
|
||||
ciphertext = ""
|
||||
character(14) :: key = "VIGENERECIPHER"
|
||||
|
||||
|
||||
call encrypt(plaintext, ciphertext, key)
|
||||
write(*,*) plaintext
|
||||
write(*,*) ciphertext
|
||||
call decrypt(ciphertext, plaintext, key)
|
||||
write(*,*) plaintext
|
||||
|
||||
contains
|
||||
|
||||
subroutine encrypt(intxt, outtxt, k)
|
||||
character(*), intent(in) :: intxt, k
|
||||
character(*), intent(out) :: outtxt
|
||||
integer :: chrn
|
||||
integer :: cp = 1, kp = 1
|
||||
integer :: i
|
||||
|
||||
outtxt = ""
|
||||
do i = 1, len(trim(intxt))
|
||||
select case(intxt(i:i))
|
||||
case ("A":"Z", "a":"z")
|
||||
select case(intxt(i:i))
|
||||
case("a":"z")
|
||||
chrn = iachar(intxt(i:i)) - 32
|
||||
|
||||
case default
|
||||
chrn = iachar(intxt(i:i))
|
||||
|
||||
end select
|
||||
|
||||
outtxt(cp:cp) = achar(modulo(chrn + iachar(k(kp:kp)), 26) + 65)
|
||||
cp = cp + 1
|
||||
kp = kp + 1
|
||||
if(kp > len(k)) kp = kp - len(k)
|
||||
|
||||
end select
|
||||
end do
|
||||
end subroutine
|
||||
|
||||
subroutine decrypt(intxt, outtxt, k)
|
||||
character(*), intent(in) :: intxt, k
|
||||
character(*), intent(out) :: outtxt
|
||||
integer :: chrn
|
||||
integer :: cp = 1, kp = 1
|
||||
integer :: i
|
||||
|
||||
outtxt = ""
|
||||
do i = 1, len(trim(intxt))
|
||||
chrn = iachar(intxt(i:i))
|
||||
outtxt(cp:cp) = achar(modulo(chrn - iachar(k(kp:kp)), 26) + 65)
|
||||
cp = cp + 1
|
||||
kp = kp + 1
|
||||
if(kp > len(k)) kp = kp - len(k)
|
||||
end do
|
||||
end subroutine
|
||||
end program
|
||||
41
Task/Vigen-re-cipher/Julia/vigen-re-cipher.julia
Normal file
41
Task/Vigen-re-cipher/Julia/vigen-re-cipher.julia
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
function encrypt(msg::ASCIIString, key::ASCIIString)
|
||||
msg = uppercase(join(filter(isalpha, collect(msg))))
|
||||
len = length(msg)
|
||||
key = uppercase(join(filter(isalpha, collect(key))))
|
||||
|
||||
if length(key) < len
|
||||
key = (key^(div(len - length(key), length(key)) + 2))[1:len]
|
||||
end
|
||||
|
||||
enc = Array(Char, len)
|
||||
|
||||
for i=1:length(msg)
|
||||
enc[i] = Char((Int(msg[i]) + Int(key[i]) - 130) % 26 + 65)
|
||||
end
|
||||
|
||||
join(enc)
|
||||
end
|
||||
|
||||
function decrypt(enc::ASCIIString, key::ASCIIString)
|
||||
enc = uppercase(join(filter(isalpha, collect(enc))))
|
||||
len = length(enc)
|
||||
key = uppercase(join(filter(isalpha, collect(key))))
|
||||
|
||||
if length(key) < len
|
||||
key = (key^(div(len - length(key), length(key)) + 2))[1:len]
|
||||
end
|
||||
|
||||
msg = Array(Char, len)
|
||||
|
||||
for i=1:length(enc)
|
||||
msg[i] = Char((Int(enc[i]) - Int(key[i]) + 26) % 26 + 65)
|
||||
end
|
||||
|
||||
join(msg)
|
||||
end
|
||||
|
||||
const msg = "Attack at dawn."
|
||||
const key = "LEMON"
|
||||
|
||||
println(encrypt(msg, key))
|
||||
println(decrypt(encrypt(msg, key), key))
|
||||
45
Task/Vigen-re-cipher/PHP/vigen-re-cipher.php
Normal file
45
Task/Vigen-re-cipher/PHP/vigen-re-cipher.php
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
$str = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
$key = "VIGENERECIPHER";
|
||||
|
||||
printf("Text: %s\n", $str);
|
||||
printf("key: %s\n", $key);
|
||||
|
||||
$cod = encipher($str, $key, true); printf("Code: %s\n", $cod);
|
||||
$dec = encipher($cod, $key, false); printf("Back: %s\n", $dec);
|
||||
|
||||
function encipher($src, $key, $is_encode)
|
||||
{
|
||||
$key = strtoupper($key);
|
||||
$src = strtoupper($src);
|
||||
$dest = '';
|
||||
|
||||
/* strip out non-letters */
|
||||
for($i = 0; $i <= strlen($src); $i++) {
|
||||
$char = substr($src, $i, 1);
|
||||
if(ctype_upper($char)) {
|
||||
$dest .= $char;
|
||||
}
|
||||
}
|
||||
|
||||
for($i = 0; $i <= strlen($dest); $i++) {
|
||||
$char = substr($dest, $i, 1);
|
||||
if(!ctype_upper($char)) {
|
||||
continue;
|
||||
}
|
||||
$dest = substr_replace($dest,
|
||||
chr (
|
||||
ord('A') +
|
||||
($is_encode
|
||||
? ord($char) - ord('A') + ord($key[$i % strlen($key)]) - ord('A')
|
||||
: ord($char) - ord($key[$i % strlen($key)]) + 26
|
||||
) % 26
|
||||
)
|
||||
, $i, 1);
|
||||
}
|
||||
|
||||
return $dest;
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
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) }
|
||||
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!!!";
|
||||
|
|
|
|||
|
|
@ -1,55 +1,49 @@
|
|||
use std::ascii::AsciiCast;
|
||||
use std::str::from_utf8;
|
||||
use std::ascii::AsciiExt;
|
||||
|
||||
static A: u8 = 'A' as u8;
|
||||
static a: u8 = 'a' as u8;
|
||||
|
||||
fn uppercase_and_filter(input: &str) -> ~[u8] {
|
||||
let mut result = ~[];
|
||||
fn uppercase_and_filter(input: &str) -> Vec<u8> {
|
||||
let alphabet = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
let mut result = Vec::new();
|
||||
|
||||
for b in input.bytes() {
|
||||
if b.is_ascii() {
|
||||
let ascii = b.to_ascii();
|
||||
if ascii.is_lower() {
|
||||
// We know it's ascii, so just do the math directly
|
||||
result.push((b + (A - a)))
|
||||
} else if ascii.is_upper() {
|
||||
result.push(b);
|
||||
}
|
||||
for c in input.chars() {
|
||||
// Ignore anything that is not in our short list of chars. We can then safely cast to u8.
|
||||
if alphabet.iter().any(|&x| x as char == c) {
|
||||
result.push(c.to_ascii_uppercase() as u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
fn vigenere(key: &str, text: &str, is_encoding: bool) -> ~str {
|
||||
fn vigenere(key: &str, text: &str, is_encoding: bool) -> String {
|
||||
|
||||
let key_bytes = uppercase_and_filter(key);
|
||||
let text_bytes = uppercase_and_filter(text);
|
||||
let key_bytes = uppercase_and_filter(key);
|
||||
let text_bytes = uppercase_and_filter(text);
|
||||
|
||||
let mut result_bytes = ~[];
|
||||
let mut result_bytes = Vec::new();
|
||||
|
||||
for (i, c) in text_bytes.iter().enumerate() {
|
||||
let c2 = if is_encoding {
|
||||
(c + key_bytes[i % key_bytes.len()] - 2 * A) % 26 + A
|
||||
} else {
|
||||
(c - key_bytes[i % key_bytes.len()] + 26) % 26 + A
|
||||
};
|
||||
result_bytes.push(c2);
|
||||
}
|
||||
for (i, c) in text_bytes.iter().enumerate() {
|
||||
let c2 = if is_encoding {
|
||||
(c + key_bytes[i % key_bytes.len()] - 2 * A) % 26 + A
|
||||
} else {
|
||||
(c + 26 - key_bytes[i % key_bytes.len()]) % 26 + A
|
||||
};
|
||||
result_bytes.push(c2);
|
||||
}
|
||||
|
||||
return from_utf8(result_bytes).to_owned();
|
||||
String::from_utf8(result_bytes).unwrap()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
let key = "VIGENERECIPHER";
|
||||
let text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
let key = "VIGENERECIPHER";
|
||||
|
||||
println!("Text: {:s}", text);
|
||||
println!("Key: {:s}", key);
|
||||
println!("Text: {}", text);
|
||||
println!("Key: {}", key);
|
||||
|
||||
let encoded = vigenere(key, text, true);
|
||||
println!("Code: {:s}", encoded);
|
||||
let decoded = vigenere(key, encoded, false);
|
||||
println!("Back: {:s}", decoded);
|
||||
let encoded = vigenere(key, text, true);
|
||||
println!("Code: {}", encoded);
|
||||
let decoded = vigenere(key, &encoded, false);
|
||||
println!("Back: {}", decoded);
|
||||
}
|
||||
|
|
|
|||
47
Task/Vigen-re-cipher/VBScript/vigen-re-cipher.vb
Normal file
47
Task/Vigen-re-cipher/VBScript/vigen-re-cipher.vb
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
Function Encrypt(text,key)
|
||||
text = OnlyCaps(text)
|
||||
key = OnlyCaps(key)
|
||||
j = 1
|
||||
For i = 1 To Len(text)
|
||||
ms = Mid(text,i,1)
|
||||
m = Asc(ms) - Asc("A")
|
||||
ks = Mid(key,j,1)
|
||||
k = Asc(ks) - Asc("A")
|
||||
j = (j Mod Len(key)) + 1
|
||||
c = (m + k) Mod 26
|
||||
c = Chr(Asc("A")+c)
|
||||
Encrypt = Encrypt & c
|
||||
Next
|
||||
End Function
|
||||
|
||||
Function Decrypt(text,key)
|
||||
key = OnlyCaps(key)
|
||||
j = 1
|
||||
For i = 1 To Len(text)
|
||||
ms = Mid(text,i,1)
|
||||
m = Asc(ms) - Asc("A")
|
||||
ks = Mid(key,j,1)
|
||||
k = Asc(ks) - Asc("A")
|
||||
j = (j Mod Len(key)) + 1
|
||||
c = (m - k + 26) Mod 26
|
||||
c = Chr(Asc("A")+c)
|
||||
Decrypt = Decrypt & c
|
||||
Next
|
||||
End Function
|
||||
|
||||
Function OnlyCaps(s)
|
||||
For i = 1 To Len(s)
|
||||
char = UCase(Mid(s,i,1))
|
||||
If Asc(char) >= 65 And Asc(char) <= 90 Then
|
||||
OnlyCaps = OnlyCaps & char
|
||||
End If
|
||||
Next
|
||||
End Function
|
||||
|
||||
'testing the functions
|
||||
orig_text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
orig_key = "vigenerecipher"
|
||||
WScript.StdOut.WriteLine "Original: " & orig_text
|
||||
WScript.StdOut.WriteLine "Key: " & orig_key
|
||||
WScript.StdOut.WriteLine "Encrypted: " & Encrypt(orig_text,orig_key)
|
||||
WScript.StdOut.WriteLine "Decrypted: " & Decrypt(Encrypt(orig_text,orig_key),orig_key)
|
||||
Loading…
Add table
Add a link
Reference in a new issue