?
Solved

Decrypting a string

Posted on 2002-03-05
4
Medium Priority
?
428 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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++.
Suggested Courses

771 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