Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Decrypting a string

Posted on 2002-03-05
4
Medium Priority
?
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

609 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