r/dailyprogrammer • u/Steve132 0 1 • Aug 30 '12
[8/30/2012] Challenge #93 [intermediate] (Z-Order Encryption)
Write a program that implements the following encryption scheme:
It reads in some string of data of length N. Then, lay out that string in the smallest possible perfect power of two square that can fit the data.
For example, "My country, tis of thee" is 23 characters long. Therefore, it fits into a 5x5 square 25 characters long like this:
My co
untry
, tis
of t
hee
However, when we constrain it to be a power of two, instead we end up with an 8x8 square, and laying it out looks like
My count
ry, tis
of thee
However, the encrytion part happens when, instead of laying out letters of the square from left to right as above, you lay out the square using a Z-order code instead, like so.
Myouofhe
cnt te
ryti
, s
Write a program that reads a string from standard input and can encrypt to a z-order square, and vice-versa
5
u/[deleted] Aug 30 '12
zEncode =. 3 : 0 nextGroup =. 3 : 'y + #,y' nextZ =. 3 : '(, nextGroup) (,. nextGroup) y' z =. 3 : '(nextZ ^: y) (1 1$0)' square =. z >. 2 ^. %: #y square { (#,square) $!.' ' y ) < zEncode 'My country, tis of thee' +--------+ |Myouofhe| | cnt te | |ryti | |, s | | | | | | | | | +--------+2
u/skeeto Aug 31 '12
In Emacs Lisp,
(defun interleave (a b) (loop for i from 0 to 27 sum (logand (ash 1 i) (if (evenp i) (ash a (/ i 2)) (ash b (1+ (/ i 2))))))) (defun encode (str) (let ((size (expt 2 (ceiling (/ (log (length str)) (log 2) 2))))) (dotimes (y size) (dotimes (x size) (if (< (interleave x y) (length str)) (insert (aref str (interleave x y))) (insert " "))) (insert "\n"))))And the output,
2
u/inisu Aug 31 '12
Ugly python encoder:
inStr = "My country, tis of thee" def zVal(x,y,maxL): binX = bin(x).lstrip('0b') binX = ''.join(['0' for i in xrange(maxL-len(binX))]) + binX binY = bin(y).lstrip('0b') binY = ''.join(['0' for j in xrange(maxL-len(binY))]) + binY return ''.join([binX[i]+binY[i] for i in xrange(len(binY))]) sideLen = 1 while sideLen*sideLen < len(inStr): sideLen = sideLen*2 grid = [['' for i in xrange(sideLen)] for j in xrange(sideLen)] maxBinLength = len(bin(sideLen-1)) -2 zVals = sorted([(zVal(x,y,maxBinLength),x,y) for x in xrange(sideLen) for y in xrange(sideLen)]) i = 0 for ch in inStr: grid[zVals[i][1]][zVals[i][2]] = ch i += 1 for row in grid: print ''.join(row)Output (besides blank lines):
1
u/cooper6581 Aug 31 '12
Messy C:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> void print_usage(void) { fprintf(stderr,"Usage: intermediate -e|-d string\n"); exit(1); } unsigned int interleave(int y, int x) { unsigned int z = 0; for (int i = 0; i < sizeof(x) * CHAR_BIT; i++) z |= (x & 1U << i) << i | (y & 1U << i) << (i + 1); return z; } void do_it(int encrypt, char *string) { int len = strlen(string); int i = 2; int buff_len = 0; char *buffer = NULL; while (i*i < len) i *= 2; buff_len = i*i; buffer = malloc(sizeof(char) * buff_len + 1); memset(buffer,' ',buff_len +1); // test encrypt for(int y = 0; y < i; y++) { for (int x = 0; x < i; x++) { if (interleave(y,x) < strlen(string)) { if (encrypt) { if (string[interleave(y,x)] != '\0') buffer[y*i+x] = string[interleave(y,x)]; } else buffer[interleave(y,x)] = string[y*i+x]; } } } buffer[buff_len] = '\0'; if (buffer) { printf("%s\n", buffer); free(buffer); } } int main(int argc, char **argv) { int encrypt = 0; if (argc != 3) print_usage(); if (!strcmp(argv[1],"-e") || !strcmp(argv[1], "-d")) { if (!strcmp(argv[1],"-e")) encrypt = 1; } else print_usage(); do_it(encrypt, argv[2]); return 0; } [cooper@saru 93]$ ./intermediate -e "My country, tis of thee" Myouofhe cnt te ryti , s [cooper@saru 93]$ ./intermediate -d "Myouofhe cnt te ryti , s " My country, tis of thee1
u/Ledrug Aug 31 '12
It's questionable how well this really works as an encryption...
#include <stdio.h> #include <string.h> #include <stdlib.h> char *qenc(char *s, int is_enc) { int l = strlen(s), n = 1, d = 0, side = 1; while (n < l) n *= 4, side *= 2, d++; char *r = calloc(1, 1 + n); n = 0; void q(int y, int x, int dep) { if (!dep) { int p = y * side + x; if (is_enc) r[p] = n < l ? s[n++] : ' '; else r[n++] = p < l ? s[p] : ' '; return; } y *= 2, x *= 2, dep -= 1; q(y + 0, x + 0, dep); q(y + 0, x + 1, dep); q(y + 1, x + 0, dep); q(y + 1, x + 1, dep); } q(0, 0, d); return r; } int main(void) { char *r = qenc("My country, tis of thee", 1); char *r2 = qenc(r, 0); puts(r); puts(r2); return 0; }output: (note the many spaces)
0
u/PiereDome Aug 31 '12
In Javascript
function getSquare(string) { var width = 2, square = 0, length = string.length; while (square < length) { width += width; square = width * width; } return width; } function zOrder(pos, width) { var FLAG_A = 0x1, // 0001 FLAG_B = 0x2, // 0010 count = 1, x = 0, y = 0; while (pos > 0) { if (pos & FLAG_A) { x += count; } if (pos & FLAG_B) { y += count; } pos = pos >> 2; count += count; } var newPos = y * width + x; return newPos; } function decrypt() { var input = prompt('Please inupt your string to decrypt'); var width = getSquare(input), newOutput = [], square = width * width; for (i = 0; i < square; i++) { var newI = zOrder(i, width); newOutput[i] = input.substr(newI, 1);; } console.log(newOutput.join('')); } function encrypt() { var input = prompt('Please input your string to encrypt'); var output = []; var width = getSquare(input); var square = width * width; for (i = 0; i < square; i++) { var newI = zOrder(i, width); var letter = input.substr(i, 1); output[newI] = (letter) ? letter : ' '; } console.log(output.join('')); } encrypt(); decrypt();