Solved

Decrypting a string

Posted on 2002-03-05
4
408 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
//////////////////////////////////////////////////
#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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now