# Thread: how to encrypt/decrypt

1. ## how to encrypt/decrypt

I need to encrypt/decrypt a set of numbers such as '1234'. The next stept to encrypting this number set is to ad '7' to each of the charaters ex = 1(becomes 8), 2(becomes 9), 3(becomes 0) and 4(becomes 1). Now, the addition part isn't causing me headaches yet, but how to pull the 1, 2,3, & the 4 out and add the 7 to them. Can someone show me how this works. Without running the code, it took me all of 15 seconds to realize that 1234 + 7777 wasn't what I wanted.

Thanks
BitWise

2. Do you want to get these numbers (1234) from the user? This is how you would do this:
Code:
```char string[20];
int value[4];

fgets(string, sizeof(string), stdin);
sscanf(string, "%d%d%d%d", &value[0], &value[1], &value[2], &value[3]);```
This puts each value into the array.

3. Hi
You could try storing your numbers as a string - the general way of encryption.
Code:
```char numbers[5] = "1234";
char encrypt[5];
int i;

for(i = 0; i < 5; i++)
encrypt[i] = numbers[i] + 7;

puts(encrypt);  /*show encrypted numbers*/```

4. That won't quite work, as '9' + 7 is not '6' as desired.

This, I think, will work.
try this:

Code:
```int temp, int exp = 0, int encrypted = 0, int normal;

//set normal to the val to be encrypted.

while(normal > 0){
temp = normal % 10; // gets the last digit of normal
temp += 7; //adds 7
temp = temp % 10; //does wraparound such that 10 = 0, 11 = 1, etc.
encrypted += temp * pow(10,exp); // puts this digit in its appropriate place,
exp ++; //now, we want to do the next digit, so increase the exponent,
normal /= 10; //shift all the digits down, discarding the lowest which we've already encrypted.
}```
Note, you can condense a lot of these steps(you can do it in 3 lines), but it's clearer written like this.

Popular pages Recent additions