Decrytion

This is a discussion on Decrytion within the C++ Programming forums, part of the General Programming Boards category; Wanted to know if there is any way in which the following output ./a.out will generate a file for me ...

  1. #1
    Gcc
    Gcc is offline
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Question Decrytion

    Wanted to know if there is any way in which the following output ./a.out will generate a file for me 255 Seperate times, I know this may sound weird but basically what i am doing is decrypting a file using brute force.

    student:~/encoder1$ ./a.out text.bin attempt (1 - 255)

    for every time i attempt to try a KEY which works (1-255) a new file should be generated in my folder, the reason being so that i can see which is the correct file with the written text in plain English rather than the Unencypted file.

    I just want to know if this is even possible, or is there an alternate way around this for examples by creating a for loop within my code so that only the correct file which is in UPPER or LOWER case in English without and special charectors is detected indicating to me the correct key.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Sure, just read the bash manual to find out how to make a for loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425

    Exp

    Maybe just append to one file but depends how much text you are talking about, or name your file by counter index and create new in loop, but are you sure of the potential permutations of your approach, brute force usually huge perms

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Also consider a rudimentary AI to filter candidates, set a number of strings as common words like 'the' or there', 'and' etc, compare content against strings and incr a counter wit each match, set a threshold to define if count high enough to qualify

  5. #5
    Gcc
    Gcc is offline
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    still same problem

    Code:
    /*
      Encrypts a file using a permutation cipher.
    
      The permutation is simply ^ with a key [0-255]. 
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    void usage(char *);
    
    
    main(int argc, char *argv[])
    {
    	FILE *encryptedout;
    	FILE *plainin;
    	int key;
    	char *plainfilename, *encryptedfilename;
    
    
    	if (argc != 4) usage(argv[0]);
    	plainfilename = argv[1];
    	encryptedfilename = argv[2];
    	key = atoi(argv[3]);  
    	  
    	plainin = fopen(plainfilename,"r");
    	encryptedout = fopen(encryptedfilename,"w");
         	  
    	int c;
    	while((c = getc(plainin)) != EOF)
    	{
    	  // The following line is the encryption algorithm
    	  char ec = (char) (c^key);
    	  putc(ec,encryptedout);
    	}
    
    	close(plainin);
    	close(encryptedout);
    }
    
    
    
    
    
    void usage(char * commandname)
    {
      printf("USAGE: %s plaintextfile encryptedfile key\n",commandname);        
      printf(" Where plaintextfile is the file you wish to encrypt\n");
      printf(" and the encrypted version is placed in encryptedfile.\n");
      printf(" key takes a value in the range (0-255).\n");
      exit(0);
    }
    As you can see from the code the problem is that i cant come up with some sort of loop which allows me to try 255 keys without the manual brute force method . Ive had a look into the Bash manual but cant seem to find anything specific to what im looking for and unfortunately pipping is too complicated for me to fully understand given my current knowledge of C.

    I need to come up with some way to do do 255 keys without having to manually try it , or even if i have to manually type it the there should be some kind of indication showing me that the file contains the unencypted file .. I can cheat around this as I already know what the key for my file is, so if there is some way i can get a detection of a sequence of words within the file then it will know it is unencrypted.

    Is there anyway to overcome this problem ?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Like this
    Code:
    $ for ((i=0;i<10;i++)) do echo $i ; done
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Then perhaps
    $ for ((i=0;i<10;i++)) do ./prog file file $i > result_${i}.txt ; done
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I had to write something like this once and used a c-based spell checker on the ouput to test to see if my attempts to decrypt had usable results so it was like:
    1. For <mykey> in <possibility space>
    2. Use <mykey> to decrypt the first 100 or so bytes of the input file
    3. User spell checker on results.
    4. If misspelled words was < some preset limit then:
    5. Key is probably correct, continue decrypting data file.

    This actually worked well since my target files were all text-based. Obviously if your files were different you would use something other than a spell checker to test...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21