Solved

Decrypting a string

Posted on 2002-03-05
4
409 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

  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 …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

919 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

22 Experts available now in Live!

Get 1:1 Help Now