Solved

Decrypting a string

Posted on 2002-03-05
4
414 Views
Last Modified: 2010-04-01
Hi,
I found this code on the internet as an example of encryption (bad encryption). The site said that i could easily modify the code to create a decrypter for that same encryption scheme. I tried doing the process in reverse, but it never seemed to work for me. Could someone please advise me on how to modify this into a decrypter?

Note: this is just for fun!

Code:
//////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROUNDS 393
#define MAXPASSLEN 64
#define MAXCHAR 127
#define FIX 93

void lshift(char *str, unsigned int size, unsigned int times);

int main(int argc, char *argv[])
{
    unsigned int mod, i, j, k;    
    unsigned int passlen;
    unsigned char passwd[MAXPASSLEN+2];

    if (argc != 2) {
        fprintf(stderr, "Usage: %s string\n", argv[0]);
        exit(1);
    }

    passwd[MAXPASSLEN + 1] = '\0';
    strncpy((char*)passwd, argv[1], MAXPASSLEN + 1);
    passlen = strlen((char*)passwd);

    if (passlen > MAXPASSLEN) {
        fprintf(stderr, "Password can't be over %d characters.\n", MAXPASSLEN);
        exit(1);
    }

    for(i = 0; i <= ROUNDS; i++) {
     
     lshift((char*)passwd, passlen, i);
        mod = (i + passlen - 1) % passlen;

     if (mod < (passlen - 1)) {
         for (j = 1; j <= mod + 1; j++) {
          k = passlen - j;    
          passwd[k] += mod + 1;
          if (passwd[k] > MAXCHAR) passwd[k] -= FIX;
         }
     }

     printf("%d pass: %s\n", i, passwd);      
    }

    printf("encrypted characters: '%s'\n", passwd);    
    exit(0);
}



void lshift(char *str, unsigned int size, unsigned int times)
{
    unsigned char tmpch;
    unsigned int i, j;

    for (j = 1; j <= times; j++) {  
     tmpch = str[0];

     for(i = 0; i <= size; i++) {
         str[i] = str[i+1];
        }

        str[size-1] = tmpch;
    }

}
0
Comment
Question by:xnet
4 Comments
 
LVL 2

Expert Comment

by:jonnin
ID: 6847075
yuck.  I can't fix that in the time I have, but here is also a quick enc/dec for fun

int i;
srand(password); //numeric password, make from string as
//you see fit.

for( each char to encrypt)
enc_char = orig_char ^ rand();

the same code will decrypt, due to the truth table of xor
reset random numbers with srand(password) first of course!

and its nice and short =)

0
 

Expert Comment

by:Retro97a
ID: 6884903
Hy ... i see your Problem!
I think if you encrypt your password with this code you cannot decrypt it! But why do u want do decrypt it ?

You can save the password encrypted and if you want to verify the password encrypt it again and compare the two strings

Mfg Retro
0
 

Author Comment

by:xnet
ID: 6887148
it is possible, you can decrypt anything that you can encrypt reliably. I know this is decryptable.
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 6887959
//////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROUNDS 393
#define MAXPASSLEN 64
#define MAXCHAR 127
#define FIX 93
void rshift(char *str, unsigned int size, unsigned int times);

int main(int argc, char *argv[])
{
   int i;
   unsigned int mod, j, k;    
   unsigned int passlen;
   unsigned char passwd[MAXPASSLEN+2];

   if (argc != 2) {
       fprintf(stderr, "Usage: %s string\n", argv[0]);
       exit(1);
   }

   passwd[MAXPASSLEN + 1] = '\0';
   strncpy((char*)passwd, argv[1], MAXPASSLEN + 1);
   passlen = strlen((char*)passwd);

   if (passlen > MAXPASSLEN) {
       fprintf(stderr, "Password can't be over %d characters.\n", MAXPASSLEN);
       exit(1);
   }

   for( i = ROUNDS; i >= 0 ; i-- ){
     printf("%d pass: (%s)\n", i, passwd);      

     mod = (i + passlen - 1) % passlen;
     if (mod < (passlen - 1)) {
        for( j = 1; j <= mod + 1; j++ ){
         k = passlen - j;    
         passwd[k] -= mod + 1;
         if (passwd[k] < MAXCHAR-FIX) passwd[k] += FIX;
        }
     }

     rshift((char*)passwd, passlen, i);
   }

   printf("decrypted characters: '%s'\n", passwd);    
   exit(0);
}

void rshift(char *str, unsigned int size, unsigned int times)
{
   unsigned char tmpch;
   unsigned int i, j;

   for( j = 1; j <= times; j++ ){  
     tmpch = str[size-1];

     for( i = size-1; i > 0; i-- ){
        str[i] = str[i-1];
     }

     str[0] = tmpch;
   }

}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question