how do i encycrpt a mobile phone number

the number is a contact number,For privacy reasons this has to be encrypted by printing the next ASCII character for each number.
useing loops for this.
Last edited on
How about something like:

1
2
3
for(int i = 0; i < numLength; i++) {
	printf("%c", num[i]+1);
}
This type of simple encryption is known as a type of "rolling code" because every letter is rolled over to the next, either by a set constant or by a value todo with it's position.

What fafner has showed you is one of the simplest which is to add a constant value of 1 to every letter (i.e. ABC becomes BCD). What's better than this is to use the position of the letter to increment the ASCII code (usually pos+1)... (i.e. ABC becomes BDF).

Here's an example of how to use the slightly improved version of the rolling code.

1
2
3
4
for(int i=0; i < str.length(); i++){ //Loops through characters in string 'str' encrycting each one
    str[i] += (1+i); //This adds the position (+1) to encrypt
}
cout << str << endl; //Print encrypted number 


You may notice many things are different between the 2 codes which have almost identical functionality, This is mainly because fafners code is written in C and mine in C++, Also mine can be used directly in a sample project because is encrypts the content of the string itself rather outputting and encrypted version of the string (and leaving the string as it was before) and you can also use mine to store the encrypted version of the string to a file and keep it encrypted...

[To decrypt you'll need to write a function that edits the string by -(1+pos) instead of +(1+pos) ]

Hope this has helped (I can't see how it cant after that essay XD )
Good luck with C (or C++)
Topic archived. No new replies allowed.