Skip to content

Instantly share code, notes, and snippets.

@spytheman
Created May 4, 2020 09:56
Show Gist options
  • Select an option

  • Save spytheman/47b68bb6bf569a1c2eb2695a6141cf12 to your computer and use it in GitHub Desktop.

Select an option

Save spytheman/47b68bb6bf569a1c2eb2695a6141cf12 to your computer and use it in GitHub Desktop.
import os
import rand
import time
import encoding.base64
fn main() {
rand.seed(time.sys_mono_now())
mut ascii_data_output := ''
mut key := ''
process_menu := os.input('Enter the process you would like to occur:\n1. Encipher \n2. Decipher\n')
data := os.input('Simple XOR cipher\nEnter data: ')
eprintln('data.len: $data.len')
if process_menu == '1' {
key = key_generator(data)
ascii_data_output = cipher(data, key)
println('Enciphered data:\n${base64.encode(ascii_data_output)}\nKey:\n${base64.encode(key)}')
} else if process_menu == '2' {
key_input := os.input('Enter key to xor: ')
ascii_data_output = cipher(base64.decode(data), base64.decode(key_input))
println('Deciphered data:\n$ascii_data_output')
}
}
fn key_generator(data_input string) string {
mut generated_key := ''
for _ in 0 .. data_input.len {
generated_key += byte(33 + rand.next(93)).str()
}
return generated_key
}
fn cipher(input_data, key_string string) string {
mut output_string := ''
for iteration in 0 .. input_data.len {
output_string += byte(input_data[iteration] ^ key_string[iteration % key_string.len]).str()
}
return output_string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment