Vigenère cipher: Difference between revisions
Content added Content deleted
Line 2,250: | Line 2,250: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{works with|Rust|0.9}} |
|||
⚫ | |||
<lang Rust>use std::ascii::AsciiCast; |
|||
⚫ | |||
use std::str::from_utf8; |
|||
⚫ | |||
for in.each_char |c| { |
|||
static a: u8 = 'a' as u8; |
|||
⚫ | |||
if char::is_lowercase(c) { |
|||
⚫ | |||
⚫ | |||
let mut result = ~[]; |
|||
⚫ | |||
for b in input.bytes() { |
|||
⚫ | |||
if b.is_ascii() { |
|||
let ascii = b.to_ascii(); |
|||
⚫ | |||
⚫ | |||
result.push((b + (A - a))) |
|||
⚫ | |||
⚫ | |||
} |
|||
} |
} |
||
} |
|||
return result; |
|||
} |
} |
||
fn vigenere(key: &str, text: &str, is_encoding: bool) -> ~str { |
fn vigenere(key: &str, text: &str, is_encoding: bool) -> ~str { |
||
⚫ | |||
⚫ | |||
let text_bytes = uppercase_and_filter(text); |
|||
⚫ | |||
⚫ | |||
let mut result_bytes = ~[]; |
|||
let mut i = 0; |
|||
for c in text_bytes.iter() { |
|||
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); |
|||
i += 1; |
|||
} |
|||
return from_utf8(result_bytes).to_owned(); |
|||
} |
} |
||
fn main() { |
fn main() { |
||
let text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"; |
|||
let key = "VIGENERECIPHER"; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Valid characters for messages: A through Z, zero, 1 to 9, and full-stop (.) |
Valid characters for messages: A through Z, zero, 1 to 9, and full-stop (.) |