Code:
/**************************************************************************
filename decode_jumbledstring.c
author inequity
date 12/13/2010
Brief Description:
This program reads in a binary file which as been jumbled using a
a very simple algorithm. The original text has been encoded using
a 5 digit string of lowercase characters, so this program attempts
to decode it as fast as possible using different string (which
isn't very fast at the moment)
**************************************************************************/
#include <stdio.h> /* printf, fopen, fclose, fseek, fgetc, feof */
#include <stdlib.h> /* malloc, free */
#include <string.h>
#define DEBUG /* comment this out to turn off unnecessary printfs */
#define PASSWORD_LENGTH 5
int decode(const unsigned char secret_phrase[], /* encrypted string */
unsigned char decoded_phrase[], /* plain text string */
unsigned char password[], /* password used */
int length, /* string length */
int *passes /* amount of attempts*/
);
int main(void)
{
/* This file contains the encrypted message */
const char *filename = "secret_phrase.bin";
/* Account for NULL terminator */
unsigned char password[PASSWORD_LENGTH + 1] = {0};
unsigned char *secret_phrase; /* Encoded phrase */
unsigned char *plain_text; /* Decoded phrase */
FILE *infile; /* File pointer we're reading from */
size_t size; /* Number of bytes in the file */
size_t count; /* Number of bytes fread from the file */
int found; /* Was the password found */
int length; /* Length of the string */
int *passes; /* ++'d each time we try a new string */
/* initially set number of passes (attempts) we've made to zero */
*passes = 0;
/* Open in binary (raw) since we don't want any translations */
infile = fopen(filename, "rb");
if (!infile)
{
printf("Can't open %s for reading.\n", filename);
return -1;
}
/* Count bytes in the file */
size = 0;
fgetc(infile);
while (!feof(infile))
{
size++;
fgetc(infile);
}
/* The file was empty */
if (!size)
{
printf("Nothing to decode.\n");
return -2;
}
/* Set cursor back to beginning of the file */
fseek(infile, 0, SEEK_SET);
length = sizeof(unsigned char)*(size+1);
/* Allocate the proper amount of memory */
secret_phrase = (unsigned char *)malloc(size + 1);
plain_text = (unsigned char *)malloc(size + 1);
/* Now read in the secret phrase from the file */
count = fread(secret_phrase, 1, size, infile);
if (count != size)
{
printf("Expected %i bytes, but only read %i.\n", size, count);
return -3;
}
/* Terminate the string of bytes */
secret_phrase[count] = 0;
/* Done with the file reading */
fclose(infile);
printf("%s\n",secret_phrase);
/* Attempt to decode the characters */
found = decode(secret_phrase, plain_text, password, length, passes);
/* Did we find the password or not? */
if (found)
{
printf("\n\nFOUND!\n");
printf("The password is: %s\n", password);
printf("The message was: %s\n", plain_text);
printf("Tried %d combinations.\n",*passes);
}
else
printf("\n\nThe secret phrase was not discovered.\n");
/* Free the memory that we allocated. */
free(secret_phrase);
free(plain_text);
return 0;
}
int decode(const unsigned char secret_phrase[], /* encrypted string */
unsigned char decoded_phrase[], /* plain text string */
unsigned char password[], /* password used */
int length, /* length of string */
int *passes /* number of attempts*/
)
{
int i, j, k, l, m, n; /* Loop counters */
unsigned char charPtr; /* Pointer to read values of characters */
unsigned char tempStr[200]; /* Temporary to hold string from current */
/* decode attempt. */
int weirdChars = 0; /* How many bad characters we've encountered */
int leastWeirdChars = 40; /* Least bad characters we've found in an attempt */
int spacesFound = 0; /* Spaces found in current attempt */
int mostSpacesFound = 0; /* Most spaces we've found in an attempt yet */
/* Increment through the alphabet for each of the letters in the password */
for(i = 'a'; i <= 'z'; i++)
{
for(j = 'a'; j <= 'z'; j++)
{
for(k = 'a'; k <= 'z'; k++)
{
for(l = 'a'; l <= 'z'; l++)
{
for(m = 'a'; m <= 'z'; m++)
{
/* Initialize variables */
*passes += 1;
weirdChars = 0;
spacesFound = 0;
/* Set password characters to loop counter values */
password[0] = i;
password[1] = j;
password[2] = k;
password[3] = l;
password[4] = m;
/* For each character in the string */
for(n = 0; n < length; n++)
{
/* Set charPtr to the secret_phrase minus the matching password char */
charPtr = secret_phrase[n] - (password[n % PASSWORD_LENGTH]);
/* uppercase 65-90, lowercase 97-122, 32, 33, 44, 46, 63, 59 */
/* regular char, uppercase */
if( charPtr >= 65 && charPtr <= 90)
{
}
/* regular char, lowercase */
else if( charPtr >= 97 && charPtr <= 122)
{
}
/* check if character is ? ! . , ; */
else if( charPtr == 33 || charPtr == 44 || charPtr == 46
|| charPtr == 59 || charPtr == 63 )
{
}
else if( charPtr == 34 )
{
}
else if( charPtr == 32 )
{
spacesFound++;
}
else
{
weirdChars++;
}
tempStr[n] = charPtr;
if(weirdChars > leastWeirdChars)
{
n = length;
}
}
if(weirdChars <= leastWeirdChars && spacesFound >= mostSpacesFound)
{
tempStr[length-1] = ' ';
tempStr[length] = ' ';
for(n = 0; n <= length; n++)
{
decoded_phrase[n] = tempStr[n];
}
decoded_phrase[length] = 0;
#ifdef DEBUG
printf("%s - password: %s\n",decoded_phrase,password);
#endif
leastWeirdChars = weirdChars;
mostSpacesFound = spacesFound;
if(weirdChars < 2)
{
return 1;
}
}
}
}
}
}
}
return 0;
}
Code:
'á,²ZUaIxO"A¥-s"aÖ%OÜO,YÖO"UOàx"+YÖÜâI<ZOçÇE_O"YOaå"DO_æ3,AYè"UIàO"OOâ"ÜIUOx"EOà"íO_à"ÖÖExáç?%çâé,ÑxaäOUYçOÑOÑ"àÅxO"áÅÜá¡
S?"I y,kng2`CD,2,t d{w"thw3yorv3$th{?m" w+eapw3{ou,3nip.R"Yo╪3yerw3pot2{krev3hor2Oqur2utai?+. y?^"hi,ƒqpo+toic2⌂cnd2?css@ - password: aabin
S?!I y,jng2`BD,2,s d{w!thw3xorv3#th{?l" w+dapw3zou,3mip.R!Yo╪3xerw3oot2{jrev3gor2Opur2usai?+- y?^!hi,ƒppo+tnic2⌂bnd2?bss@ - password: aacin
S? K y,ipg2`AF,2,r"d{w vhw3wqrv3"vh{?k$ w+ccpw3yqu,3lkp.R [o╪3wgrw3nqt2{itev3fqr2Oowr2urci?+,"y?^ ji,ƒoro+tmkc2⌂apd2?aus@ - password: aadgn
S? J y,iog2`AE,2,r!d{w uhw3wprv3"uh{?k# w+cbpw3ypu,3ljp.R Zo╪3wfrw3npt2{isev3fpr2Oovr2urbi?+,!y?^ ii,ƒoqo+tmjc2⌂aod2?ats@ - password: aadhn
S? I"y,ini2`AD.2,r f{w tjw3wotv3"tj{?k""w+carw3yow,3lir.R Yq╪3wetw3nov2{irgv3fot2Oout2urak?+, {?^ hk,ƒopq+tmie2⌂anf2?asu@ - password: aadil
S? I!y,inh2`AD-2,r e{w tiw3wosv3"ti{?k"!w+caqw3yov,3liq.R Yp╪3wesw3nou2{irfv3fos2Oous2uraj?+, z?^ hj,ƒopp+tmid2⌂ane2?ast@ - password: aadim
S? I y,ing2`AD,2,r d{w thw3worv3"th{?k" w+capw3you,3lip.R Yo╪3werw3not2{irev3for2Oour2urai?+, y?^ hi,ƒopo+tmic2⌂and2?ass@ - password: aadin
S⌂ I y?ing2_AD,2?r d{v thw2worv2"th{?k" w.capw2you,2lip.Q Yo╪2werw2not2zirev2for2<our2trai?., y?╪ hi,,opo+smic2~and2⌂ass@ - password: abdin
S~ I y?ing2^AD,2?r d{u thw1worv1"th{⌂k" w,capw1you,1lip.P Yo╪1werw1not2yirev1for2Sour2srai?,, y?+ hi,?opo+rmic2}and2~ass@ - password: acdin
S} I y⌂ing2]AD,2⌂r d{t thw0worv0"th{~k" wƒcapw0you,0lip.O Yo╪0werw0not2xirev0for2%our2rrai?ƒ, y?. hi,?opo+qmic2|and2}ass@ - password: addin
S| I y~ing2\AD,2~r d{s thw/worv/"th{}k" w,capw/you,/lip.N Yo╪/werw/not2wirev/for2^our2qrai?,, y?, hi,⌂opo+pmic2{and2|ass@ - password: aedin
S{ I y}ing2[AD,2}r d{r thw.worv."th{|k" w?capw.you,.lip.M Yo╪.werw.not2virev.for2╪our2prai??, y?ƒ hi,~opo+omic2zand2{ass@ - password: afdin
Sy I y{ing2YAD,2{r d{p thw,worv,"th{zk" w⌂capw,you,,lip.K Yo╪,werw,not2tirev,for2.our2nrai?⌂, y?? hi,|opo+mmic2xand2yass@ - password: ahdin
So I yqing2OAD,2qr d{f thw"worv""th{pk" wucapw"you,"lip.A Yo╪"werw"not2jirev"for2{our2drai?u, y?w hi,ropo+cmic2nand2oass@ - password: ardin
Sn I yping2NAD,2pr d{e thw!worv!"th{ok" wtcapw!you,!lip.@ Yo╪!werw!not2iirev!for2zour2crai?t, y?v hi,qopo+bmic2mand2nass@ - password: asdin
Sm J yoiog2MAE,2or!d{d uhw wprv "uh{nk# wscbpw ypu, ljp.? Zo╪ wfrw npt2hisev fpr2yovr2brbi?s,!y?u ii,poqo+amjc2laod2mats@ - password: atdhn
Sm I"yoini2MAD.2or f{d tjw wotv "tj{nk""wscarw yow, lir.? Yq╪ wetw nov2hirgv fot2yout2brak?s, {?u hk,popq+amie2lanf2masu@ - password: atdil
Sm I!yoinh2MAD-2or e{d tiw wosv "ti{nk"!wscaqw yov, liq.? Yp╪ wesw nou2hirfv fos2yous2braj?s, z?u hj,popp+amid2lane2mast@ - password: atdim
Sm I yoing2MAD,2or d{d thw worv "th{nk" wscapw you, lip.? Yo╪ werw not2hirev for2your2brai?s, y?u hi,popo+amic2land2mass@ - password: atdin
Rm I xoing1MAD,1or dzd thv woru "thznk" vscapv youƒ lip,? Yo+ werv not1hireu for1your1brai⌂s, y?u hi?popo.amic1land1mass? - password: btdin
Om I uoing.MAD,.or dwd ths worr "thwnk" sscaps you? lip?? Yoƒ wers not.hirer for.your.brai|s, y}u hi~popo,amic.land.mass< - password: etdin
Mm I soing,MAD,,or dud thq worp "thunk" qscapq you~ lip? Yo? werq not,hirep for,your,braizs, y{u hi|popo?amic,land,mass: - password: gtdin
Cm I ioing"MAD,"or dkd thg worf "thknk" gscapg yout lipu? Yow werg not"hiref for"your"braips, yqu hirpopovamic"land"mass0 - password: qtdin
Bm I hoing!MAD,!or djd thf wore "thjnk" fscapf yous lipt? Yov werf not!hiree for!your!braios, ypu hiqpopouamic!land!mass/ - password: rtdin
Ao I gqing OAD, qr dif the"word""thipk" eucape"your"lipsA You"were"not jired"for {our drainu, yow hipropotcmic nand oass. - password: srdin
An I gping NAD, pr die the!word!"thiok" etcape!your!lips@ You!were!not iired!for zour craint, yov hipqopotbmic mand nass. - password: ssdin
Am J goiog MAE, or!did uhe wprd "uhink# escbpe ypur ljps? Zou wfre npt hised fpr yovr brbins,!you iippoqotamjc laod mats. - password: stdhn
Am I"goini MAD. or fid tje wotd "tjink""escare yowr lirs? Yqu wete nov hirgd fot yout brakns, {ou hkppopqtamie lanf masu. - password: stdil
Am I!goinh MAD- or eid tie wosd "tiink"!escaqe yovr liqs? Ypu wese nou hirfd fos yous brajns, zou hjppopptamid lane mast. - password: stdim
Am I going MAD, or did the word "think" escape your lips? You were not hired for your brains, you hippopotamic land mass. - password: stdin
FOUND!
The password is: stdin
The message was: Am I going MAD, or did the word "think" escape your lips? You were not hired for your brains, you hippopotamic land mass.
Tried 8561762 combinations.
Anyways, any help would be greatly appreciated. And any comments on my coding style are also greatly appreciated.