Advertisement
badeip

sophos - level1

Dec 21st, 2011
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.05 KB | None | 0 0
  1. // by petter wahlman, http://www.twitter.com/badeip
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8.  
  9. #define ROTATION 13
  10. static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  11.  
  12. void decodeblock(unsigned char *input, char *output)
  13. {
  14.     unsigned char out[4];
  15.     out[0] = input[0] << 2 | input[1] >> 4;
  16.     out[1] = input[1] << 4 | input[2] >> 2;
  17.     out[2] = input[2] << 6 | input[3] >> 0;
  18.     out[3] = '\0';
  19.     strncat(output, (const char *)out, sizeof(out));
  20. }
  21.  
  22. char *unbase(char *input)
  23. {
  24.     int c, phase, i;
  25.     unsigned char in[4];
  26.     char *output;
  27.     char *p;
  28.  
  29.     output = calloc(1, strlen(input) << 1);
  30.     phase = 0;
  31.     i = 0;
  32.     while(input[i]) {
  33.         c = input[i];
  34.         if('=' == c) {
  35.             decodeblock(in, output);
  36.             break;
  37.         }
  38.         p = strchr(b64, c);
  39.         if (p) {
  40.             in[phase] = p - b64;
  41.             phase = (phase + 1) % 4;
  42.             if (phase == 0) {
  43.                 decodeblock(in, output);
  44.                 in[0]=in[1]=in[2]=in[3]=0;
  45.             }
  46.         }
  47.         i++;
  48.     }
  49.     return output;
  50. }
  51.  
  52. int rotate(char c, int pos)
  53. {
  54.     char alpha[pos << 1];
  55.     int i;
  56.  
  57.     for (i = 0; i < sizeof(alpha); i++)
  58.         alpha[i] = 'a' + i;
  59.  
  60.     return (c < alpha[0] || c > alpha[sizeof(alpha)-1]) ? c : alpha[(c + (sizeof(alpha) >> 1) - alpha[0]) % sizeof(alpha)];
  61. }
  62.  
  63. void revert(char *str)
  64. {
  65.     char c;
  66.     int i;
  67.  
  68.     for (i = 0; i < (strlen(str) >> 1); i++) {
  69.         c = str[i];
  70.         str[i] = str[strlen(str)-i - 1];
  71.         str[strlen(str) -i - 1] = c;
  72.     }
  73. }
  74.  
  75. int main(int argc, char **argv)
  76. {
  77.     char challenge[] = "=ImYndmbn1ieiBnLmJWdjJmZ";
  78.     char *unbased;
  79.     int i, r;
  80.  
  81.     revert(challenge);
  82.     unbased = unbase(challenge);
  83.  
  84.     for (i = 0; i < strlen(unbased); i++) {
  85.         r = rotate(unbased[i], 13);
  86.         if (r < 1)
  87.             break;
  88.         printf("%c", r);
  89.     }
  90.     putchar('\n');
  91.  
  92.     free(unbased);
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement