// Ruri implementation of CipherSaber-1. // By Tom Rothamel // This software has NO WARRANTY, and is released under the terms of the // GNU GPL. // // This needs Ruri 0.2 and IJVMA. Specifically, idbg 0.3 or the second // version if IJVMA is required. See // // http://onegeek.org/~tom/software/ruri/ and // http://onegeek.org/~tom/software/idbg/ const tff = 0xff; const tfs = 0x100; main { word s1; word s2; word klen; word c; word bm; word tff; word tfs; word i; word j; word k; word temp; tff = const tff; tfs = const tfs; s1 = alloca(tfs); s2 = alloca(tfs); klen = 0; out "Enter key:\n"; while (true) { c = getchar(1); if (c == '\n') break; s2[klen] = c; klen = klen + 1; } out "Enter ISV (first ten bytes of cyphertext, in hex):\n"; for (i = 0; i < 10; i = i + 1) { s2[klen] = readbyte(1); klen = klen + 1; } out "\nDone. Setting up keys.\n"; for (i = klen; i < tfs; i = i + 1) { s2[i] = s2[i - klen]; } for (i = 0; i < tfs; i = i + 1) { s1[i] = i; } j = 0; for (i = 0; i < tfs; i = i + 1) { j = (j + s1[i] + s2[i]) & tff; temp = s1[i]; s1[i] = s1[j]; s1[j] = temp; s2[i] = 0; } out "Setup done. Enter cyphertext in hex (will not echo):\n"; i = 0; j = 0; while (true) { c = readbyte(0); i = (i + 1) & tff; j = (j + s1[i]) & tff; temp = s1[i]; s1[i] = s1[j]; s1[j] = temp; k = (s1[i] + s1[j]) & tff; k = s1[k]; out (xor(c, k) & 0xff); } halt; } method xor(word a, word b) { return (~a & b) | (a & ~b); } method getchar(word echo) { word c; while (!(c = in)) {} if (echo) out c; return c; } method readbyte(word echo) { word val; word read; word c; val = 0; read = 0; while (true) { c = getchar(echo); if (c >= '0' && c <= '9') { read = read + 1; val = val << 4 + c - '0'; } else if (c >= 'a' && c <= 'f') { read = read + 1; val = val << 4 + c - 'a' + 10; } else if (c >= 'A' && c <= 'F') { read = read + 1; val = val << 4 + c - 'A' + 10; } if (read == 2) return val; } }