Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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

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?

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;
}

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

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.

![]() |
![]() |
![]() |

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