/*
 * Netscape Communicator vX.X Password Cracker
 * (c) 2000 Elli0t <elli0t@i.am>
 * Decrypts passwords from registry or file prefs.js
 *
 * For non-commercial use only.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char    p[12], dp[9],
        key[] = { 0x56, 0xc9, 0xef, 0x4a, 0x9b, 0xbe, 0x5a, 0x42 };

char *
rot13 (char *str) 
{
    int   i;
    char  c, s;
    for(i=0;i<strlen(str);i++)
    {
        c=str[i];
        if (c>='a' && c<='z') s='a';
        else if (c>='A' && c<='Z') s='A';
        else s=0;
        if(s>0) c=(c-s+13)%26;
        str[i]=c+s;
    }
    return str;
}

char *
strrev(char *ptr)
{
    int        i,l;
    char    t;
    l = strlen(ptr)-1;
    for(i=0;i<=l>>1;i++)
    {
        t = ptr[i];
        ptr[i] = ptr[l-i];
        ptr[l-i] = t;
    }
    return ptr;
}

int
cvt_ascii( unsigned char a )
{
   if      ( (a >= 'A') && (a <= 'Z') ) return (int)(a - 'A');
   else if ( (a >= 'a') && (a <= 'z') )
        return 26 + (int)(a - 'a');
   else if ( (a >= '0') && (a <= '9' ) )
        return 52 + (int)(a - '0');
   else if ( a == '+' ) return 62;
   else if ( a == '/' ) return 63;
   else if ( a == '=' ) return -2;
   else                 return -1;
}

int
base64_decode (char *buf, char *dest)
{
   int i, sh=0, ds=0, offs=0;
   unsigned char bv;
   unsigned long ac=0, vl;

   for (i = 0; (buf[i] != '\n') && (buf[i] != '\0') && (ds >= 0); i++ )
   {
      if (((buf[i] >= 'A') && (buf[i] <= 'Z') ) ||
          ((buf[i] >= 'a') && (buf[i] <= 'z') ) ||
          ((buf[i] >= '0') && (buf[i] <= '9') ) ||
           (buf[i] == '+') ||
           (buf[i] == '/') || 
           (buf[i] == '=') )
         ds = 1;
      else
         ds = -2;
   }
   if ( ds <= 0 )  ds = 0;
   for (i = 0; (buf[i] != '\n') && (buf[i] != '\0'); i++)
   {
      vl = cvt_ascii( buf[i] );
      if ( vl < 64 ) {
         ac <<= 6;
         sh += 6;
         ac |= vl;
         if ( sh >= 8 ) {
            sh -= 8;
            vl = ac >> sh;
            bv = (unsigned char)vl & 0xFFl;
            *(dest+offs) = bv;
            offs++;
         }
      }
      else break;
   }
   return offs;
}

void main()
{
    int     i, cn;
    printf("Netscape Communicator Password Cracker (c) Elli0t\n");
    printf("Encrypted password: ");
    scanf("%12s", p);
    if(*p=='=')
        rot13(strrev(p));
    cn = base64_decode(p, dp);
    for(i=0;i<cn;i++)
        dp[i] ^= key[i%8];
    printf("Password: '%s'\n", dp);
}


