Author Topic: [C][snippet] caesar cipher bruteforce  (Read 2904 times)

0 Members and 1 Guest are viewing this topic.

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
[C][snippet] caesar cipher bruteforce
« on: May 10, 2011, 07:13:30 pm »
This was for a friend, nothing complex, just tries to decode a string with every possible alpha in the alphabet.

Code: [Select]
#include <stdio.h>

char *substr(char *str, int begin, int len)
{
    int strLen=strlen(str);
    if(strLen<begin) return str;
    if((len>strLen) || (len==0)) len=strLen;
    if((strLen-begin)<len) len=strLen-begin;
    str+=begin;
    char *ret=(char *)malloc(len+1);
    memset(ret, 0, len+1);
    strncpy(ret, str, len);
    return ret;
}

int main()
{
    char cifrado[]="khoor";
    int l3n=strlen(cifrado);
    char alf[]="abcdefghijklmnopqrstuvwxyz";
    int len=strlen(alf);
    char mut[len];
    char test[l3n];
    int i=0;
    int x=0;
    int z=0;
    for(i=0; i<26; i++)
    {
        printf("Salto: %i -> ", i);
        memset(&mut, 0, len);
        memset(&test, 0, l3n);
        strcat(mut, substr(alf, i, 0));
        strcat(mut, substr(alf, 0, i));
        for(x=0; x<l3n; x++)
        {
            if(cifrado[x]!=' ')
            {
                z=0;
                while(mut[z]!=cifrado[x]) z++;   
                test[x]=alf[z];   
            } 
        }
        test[x]=0x00;
        printf("%s\n", test);
        //if(strstr(test, "hola")) printf("Bruted: %d\n", i);
    }
   
    return 0;
}