Computing.Net > Forums > Programming > Encryption and Decryption

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Encryption and Decryption

Reply to Message Icon

Name: Gizmorj
Date: January 22, 2004 at 10:45:01 Pacific
OS: NT
CPU/Ram: pent 4 256
Comment:

I am using this program to read a four-digit integer and encrypt it as follows:
Replace each digit by - (the sum of that digit plus 7) modulus 10. then,
swap the first digit with the the third, swap the second digit with the
fourth and print the encrypted integer.


#include <stdio.h>

int main()

{
/*new*/
int temp;

int digit1;
int digit2;
int digit3;
int digit4;
int ndigit1;
int ndigit2;
int ndigit3;
int ndigit4;

printf( "\nEnter a four-digit integer to be encrypted. \n");
scanf("%d",&temp);
/*scanf("%d%d%d%d", &digit1, &digit2, &digit3, &digit4); */
/* new */
digit1 = temp/1000;
temp = temp - digit1 * 1000;
digit2 = temp/100;
temp = temp - digit2 *100;
digit3 = temp/10;
temp = temp - digit2 * 10;
digit4 = temp;
/* original */
ndigit1 = (digit1 + 7) % 10;
ndigit2 = (digit2 + 7) % 10;
ndigit3 = (digit3 + 7) % 10;
ndigit4 = (digit4 + 7) % 10;

/* new */
temp = (ndigit3 * 1000)+(ndigit4*100)+(ndigit1*10)+ndigit2;
printf("%04d\n",temp);

/* printf( "%d%d%d%d", ndigit3, ndigit4, ndigit1, ndigit2); */

}


I got the encrypting part of it down, but what if I wanted to run this same program but after printing out the encrypted integer also be able to decrypt the integer back to the orginal integer.

Can someone assist me with this?
Your help would be appreciated



Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: January 22, 2004 at 11:00:54 Pacific
Reply:

An encrypted value that can be decrypted by reversing the process that encypted it in the first place is very weak and will soon be cracked.

If this is some kind of sercurity code what you need to do is to store the encryped value somewhere. When the un-enccypted value is entered, encrypt it and compare it with encrypted value stored previously. No need to un-encrypt at all.

For a far better encyption alogorithm have a look a MD5.

http://userpages.umbc.edu/~mabzug1/cs/md5/md5.html

Stuart


0

Response Number 2
Name: Sord
Date: January 22, 2004 at 11:57:15 Pacific
Reply:

If you want something that will encrypt and decrypt, check out PGP, or the RSA algorithm.

Your algorithm to encrypt is:
((i + 7) % 10)
Where i is the current number.

So to decrypt, you would have to find what added with 7 but modded with 10 is the number you have.
So you know:
0 would be 7
1 would be 8
2 would be 9
3 would be 0
4 would be 1
5 would be 2
6 would be 3
7 would be 4
8 would be 5
9 would be 6
Seeing a pattern?
You are basically just rotating numbers as seen. Notice that if the result - 7 is positive, you have your number, if it is negative, try result + 3.
Understand?


1

Response Number 3
Name: Sord
Date: January 22, 2004 at 12:06:49 Pacific
Reply:

Here's an example of what I would do to decrypt.

#include <stdio.h>

int main()
{
int i;
char tmp;
char code[4];

printf("Enter encrypted code: ");
scanf("%s", &code);

/* Swap letters */
tmp = code[0];
code[0] = code[2];
code[2] = tmp;
tmp = code[1];
code[1] = code[3];
code[3] = tmp;

/* Decrypt */
for (i = 0; i < 4; i++)
if (code[i] - '0' - 7 < 0)
code[i] += 3;
else
code[i] -= 7;

/* Display it */
printf("Here you go: %s\n", code);

return 0;
}


-1

Response Number 4
Name: Infinite Recursion
Date: January 22, 2004 at 12:34:23 Pacific
Reply:

Look into the

AES Crypto Toolkit...

IR


1

Response Number 5
Name: Infinite Recursion
Date: January 22, 2004 at 12:35:25 Pacific
Reply:

Well, the link didn't post right...
Here it is:

http://csrc.nist.gov/CryptoToolkit/aes/


0

Related Posts

See More



Response Number 6
Name: SN
Date: January 22, 2004 at 12:52:55 Pacific
Reply:

I would guess this is just playing around or maybe a homework assignment...If you're seriously trying to encrypt something then try one of the suggestions above, but if you're doing it for kicks or school you would just do the reverse of the encryption...First swap all the numbers back, then change each digit to (digit+3)%10.

-SN


0

Response Number 7
Name: Sord
Date: January 22, 2004 at 22:19:20 Pacific
Reply:

That makes it easier than checking if subtracting by 7 is negative...
I knew there was an easier way, good thing I'm not a cryptanalysist.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Encryption and Decryption

encrypt and decrypt query string www.computing.net/answers/programming/encrypt-and-decrypt-query-string/15417.html

Urgent!!Encypt n decrypt www.computing.net/answers/programming/urgentencypt-n-decrypt/11991.html

Help wit a little Java program www.computing.net/answers/programming/help-wit-a-little-java-program-/94.html