# text encryption

## Recommended Posts

Hey everyone, im getting pretty frustrated here. What I need to do is create a text encryption program that accepts some text and stores it in a dynamically allocated array of chars. I need to then encrypt each element of the array by adding 7 to the ASCII code and then MOD by 10. For example if the ASCII code is 65 I need to break it down into 6 and 5 then add 7 and MOD 10, then put the number back together and print out the ASCII character. Yes, this is homework, but the focus is supposed to be on the pointer aspect of it. . .I'm struggling with the encryption/decryption functions. They seem unstable as they sometimes kick out the same encrypted values for two different characters! Thank for the help!
#include<iostream>
#include<string>
#include<cctype>
using namespace std;

int encrypt(int);
int decrypt(int);

int main()
{

cout<<"Would you like to (e)ncrypt or (d)ecrypt? (e/d):  ";
string choice = "";
getline(cin, choice);
while(choice != "e" && choice != "E" && choice != "d" && choice != "D")
{
cout<<"Would you like to (e)ncrypt or (d)ecrypt? (e/d):  ";
getline(cin, choice);
}

if(choice == "e" || choice == "E")
{
string text;
choice = "";
cout<<"Enter the text to encrypt:  \n";

getline(cin, text);

char* pValues = new char[text.length()];

for(int i = 0; i < text.length(); i++)
{
pValues[i] = int (text[i]);
pValues[i] = encrypt(pValues[i]);
pValues[i] = char (pValues[i]);
cout<< pValues[i];

}
}

else if(choice == "d" || choice == "D")
{
choice = "";
cout<<"Enter the text to decrypt:  \n";
string text;
getline(cin, text);

char* pValues = new char[text.length()];

for(int i = 0; i < text.length(); i++)
{
pValues[i] = int (text[i]);
pValues[i] = decrypt(pValues[i]);
pValues[i] = char (pValues[i]);
cout<< pValues[i];
}

}
cout<<endl;
system("pause");
return 0;
}

int decrypt(int x)
{
int result = 0;
int a = 0;
int b = 0;
int c = 0;
if(x >= 10 && x < 100)
{
a = x / 10;
b = x - a * 10;
a = (a - 7) + 10;
b = (b - 7) + 10;
a = a * 10;
result = a + b;
}
else if(x >= 100)
{
a = x / 100;
b = (x - (a*100)) / 10;
c = (x - (a*100)) - (b*10);
a = (a - 7) + 10;
b = (b - 7) + 10;
c = (c - 7) + 10;
a = a * 100;
b = b * 10;
result = a + b + c;
}
return result;
}

int encrypt(int x)
{
int result = 0;
int a = 0;
int b = 0;
int c = 0;
if(x >= 10 && x < 100)
{
a = x / 10;
b = x - a * 10;
a = (a + 7) % 10;
b = (b + 7) % 10;
a = a * 10;
result = a + b;
}
else if(x >= 100)
{
a = x / 100;
b = (x - (a*100)) / 10;
c = (x - (a*100)) - (b*10);
a = (a + 7) % 10;
b = (b + 7) % 10;
c = (c + 7) % 10;
a = a * 100;
b = b * 10;
result = a + b + c;
}
return result;
}



##### Share on other sites
given Input as an ASCII value (note--- limited to 99 as ASCII is 0..127)

It doesnt make sense to do ASCII over 99 because by adding 7 to the hundreds digit, you would get values in the 700s and 800s which are not ASCII for the output value...... (you might have to ask your instructor for a clarification, as many lower case letters have ASCII values over 100)

http://www.lookuptables.com/ << ASCII table found Yahoo's in 5 seconds

' get seperate input decimal digits
I2 = Input / 10 'upper
I1 = Input - (I2*10) 'lower

O1 = (I1+7) % 10
O2 = (I2+7) % 10

'reassemble them for the output value
Output = (O2*10) + O1

beware though, many ASCII values are not printable, so you probably want to output the ordinal (decimal form) and only the printable form if it IS printable

Over time you will learn that arithmetic and logical functions can do alot of work that you might now think requires if-then logic. Ive only been programming for about 30 years so I can do things like this in my sleep...
Keep at it, this kind of learning is accumulative.

##### Share on other sites
My definition of a cipher says that it has to be reversible. Considering that your plaintext alphabet has at least 26 letters and your ciphertext only 10, there's no way to recover the original text. At least not unless there's some plaintext restriction you haven't told us about. Consider:

'A' = 65
('A' + 7) % 10 = 72 % 10 = 2

'K' = 75
('K' + 7) % 10 = 82 % 10 = 2

And so A and K are indistinguishable after encryption.
This is an example of the collision you speak of, and there is no way around it. If you do find a way around this, be sure to patent it, as you'll have found a method of binary encoding with at least 260% efficiency [wink].

Are you sure you're supposed to take the 10-modulus? I suspect that a 26 or higher (often 64 or 83) modulus was recommended to you, as it's then possible to retain the important information. The most spartan of such modulus-ciphers would convert the text to capitals, remove anything that isn't a letter, subtract 65 and take the 26-modulus. This would preserve no whitespace, capitalisation or punctuation, however. The rules is that if you need to preserve n plaintext letters, you can't take a modulus below n without losing information.

Regards

##### Share on other sites
Quote:
 Original post by TheAdmiralMy definition of a cipher says that it has to be reversible. Considering that your plaintext alphabet has at least 26 letters and your ciphertext only 10, there's no way to recover the original text. At least not unless there's some plaintext restriction you haven't told us about. Consider:'A' = 65('A' + 7) % 10 = 72 % 10 = 2'K' = 75('K' + 7) % 10 = 82 % 10 = 2And so A and K are indistinguishable after encryption.This is an example of the collision you speak of, and there is no way around it. If you do find a way around this, be sure to patent it, as you'll have found a method of binary encoding with at least 260% efficiency [wink].Are you sure you're supposed to take the 10-modulus? I suspect that a 26 or higher (often 64 or 83) modulus was recommended to you, as it's then possible to retain the important information. The most spartan of such modulus-ciphers would convert the text to capitals, remove anything that isn't a letter, subtract 65 and take the 26-modulus. This would preserve no whitespace, capitalisation or punctuation, however. The rules is that if you need to preserve n plaintext letters, you can't take a modulus below n without losing information.RegardsAdmiral

Read his post again closely and check the code. He says, although a bit unclear, the add and modulo is for each digit in the base-10 representation of the ASCII value. So the 'A' and 'K' encrypts to 32 and 42, respectively.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627718
• Total Posts
2978792

• 9
• 9
• 21
• 14
• 12