Crypt

October 28, 2011

Our solution uses a do with two variables that are stepped in parallel: key cycles through the characters of the key and char indexes through the input file:

(define (crypt key infile outfile)
  (define (last-pair xs) (if (null? (cdr xs)) xs (last-pair (cdr xs))))
  (define (cycle xs) (set-cdr! (last-pair xs) xs) xs)
  (with-input-from-file infile (lambda ()
    (with-output-to-file outfile (lambda ()
      (do ((key (cycle (map char->integer (string->list key))) (cdr key))
           (char (read-char) (read-char)))
          ((eof-object? char))
        (display (integer->char (logxor (car key) (char->integer char))))))))))

The xor function has been called logxor since the early days of Lisp, and though it’s not part of R5RS Scheme, most Scheme interpreters provide it; if yours doesn’t, there is a portable version in the Standard Prelude. R6RS Scheme broke compatibility with the past, as it did in so many needless ways, by calling the function bitwise-xor.

You can see the program at http://programmingpraxis.codepad.org/SUkyyUpu.

Advertisement

Pages: 1 2

7 Responses to “Crypt”

  1. Graham said

    Sorry I’ve taken so long to join in; here’s a Python solution:

    from itertools import cycle, imap
    
    def crypt(key, infile, outfile):
        kstream = cycle(key)
        text_xor = lambda a, b: chr(ord(a) ^ ord(b))
        with open(infile) as inf:
            with open(outfile, 'w') as outf:
                outf.write(''.join(imap(text_xor, inf.read(), kstream)))
    
  2. A javascript solution

    function crypt(file,key) {
    var key_index = 0,
    cont = 0,
    end_file_cod = [],
    end_file = [],
    key_size = key.length;
    file_size = file.length;

    for (; cont < file_size; cont++) {
    var file_cd = (file[cont].charCodeAt(0));
    var key_cd = (key[key_index].charCodeAt(0));
    end_file_cod.push(file_cd ^ key_cd);
    key_index = (key_index == key_size – 1) ? 0 : key_index += 1;
    }

    for (var j = 0; j < end_file_cod.length; j++) {
    end_file.push(String.fromCharCode(end_file_cod[j]));
    }
    return end_file.join("");
    }

  3. klawztro said

    function crypt(file,key) {
    var key_index = 0,
    cont = 0,
    end_file_cod = [],
    end_file = [],
    key_size = key.length;
    file_size = file.length;

    for (; cont < file_size; cont++) {
    var file_cd = (file[cont].charCodeAt(0));
    var key_cd = (key[key_index].charCodeAt(0));
    end_file_cod.push(file_cd ^ key_cd);
    key_index = (key_index == key_size – 1) ? 0 : key_index += 1;
    }

    for (var j = 0; j < end_file_cod.length; j++) {
    end_file.push(String.fromCharCode(end_file_cod[j]));
    }
    return end_file.join("");
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: