# Cryptography

• 08-13-2008
freddyvorhees
Cryptography
This is the problem:
----------------------------------------------------------------------------------------------
(Cryptography) A company wants to transmit data over the telephone,
but is concerned that its phones could be tapped. All of the data are
transmitted as four-digit integers. The company has asked you to write
a program that encrypts the data so that it can be transmitted more
it as follows: Replace each digit by (the sum of that digit plus 7)
modulus 10. Then, swap the first digit with the third, swap the second
digit with the fourth and print the encrypted integer. Write a separate
program that inputs an encrypted four digit integer and decrypts it to
form the original number.

--------------------------------------------------
Sample Run of the encrypting program
--------------------------------------------------

Enter numbers to encrypt (0000 to end):

1234 1111 3290 1800 0000

The encrypted numbers are:

0189 8888 6709 7785

--------------------------------------------------
Sample Run of the decrypting program
--------------------------------------------------

Enter numbers to decrypt (0000 to end):

0189 8888 6709 7785 0000

The decrypted numbers are:

1234 1111 3290 1800
------------------------------------------------------------------------
All I have is this:
Code:

```#include <iostream> using namespace std; int main() {         int digit1, digit2, digit3, digit4, encrypt1, encrypt2, encrypt3, encrypt4,number;         cin>>number;                digit4 = number % 10;         digit3 = ((number % 100) - digit4)/10;         digit2 = ((number % 1000) - digit3)/100;         digit1 = ((number - number % 1000))/1000;                 encrypt1 = (digit3 + 7)%10;                encrypt2 = (digit4 + 7)%10;         encrypt3 = (digit1 + 7)%10;         encrypt4 = (digit2 + 7)%10;                 if (number == 0)         {                 cout<<endl;         }         else         {         while (number != 0)         {         cout<<encrypt1<<encrypt2<<encrypt3<<encrypt4<<"\n";         cin>>number;         digit4 = number % 10;         digit3 = ((number % 100) - digit4)/10;         digit2 = ((number % 1000) - digit3)/100;         digit1 = ((number - number % 1000))/1000;         encrypt1 = (digit3 + 7)%10;                encrypt2 = (digit4 + 7)%10;         encrypt3 = (digit1 + 7)%10;         encrypt4 = (digit2 + 7)%10;         }         } }```
I have no idea how to output the encrypted numbers all at the same time.
• 08-13-2008
hk_mp5kpdw
• 08-13-2008
arpsmack
If anyone can come up with something more insane than this, I'd like to see it! LOL
Code:

```#include <iostream> #include <iomanip> using namespace std; class Number { private:   int val;   void internal_crypt(int key); public:   Number(void) : val(0) {}   Number(int n) : val(n) {}   int value(void) { return val; }   void encrypt(void) { internal_crypt(7); }   void decrypt(void) { internal_crypt(3); }   friend istream& operator>>(istream &input, Number &n) { return input >> n.val; }   friend ostream& operator<<(ostream &output, const Number &n) { return output << setfill('0') << setw(4) << n.val; } }; void Number::internal_crypt(int key) {   char r;   int result = 0, omg = 10;     #define foo r = val % 10; val /= 10; omg = (omg * 10) % 9999; result += ((r + key) % 10) * omg;   foo foo foo foo   #undef foo     val = result; } int main(void) {   Number n;   int c;     cout << "Enter 1 to encrypt, 2 to decrypt: ";   cout.flush();   cin >> c;     if (c != 1 && c != 2) {       cout << "Illegal value entered." << endl;       return 1;   }     cout << "Enter the numbers to " << (c == 1 ? "encrypt" : "decrypt") << ":" << endl;     while (1) {       cin >> n;             if (n.value() == 0) break;       if (n.value() > 9999 || n.value() < 0) {         cout << endl << "Illegal value entered: " << n;         return 1;       }             if (c == 1) n.encrypt();       else n.decrypt();             cout << n << " ";   }     return 0; }```
• 08-13-2008
dwks
Convoluted (and codeformed? :P ) example aside, I think the OP really needs to read up on arrays.

 A note for arpsmack:

Through some sort of black magic, cout is automatically flushed whenever you use cin. :)
Quote:

cin and cout are related, but only because they are tied together so that any request for input using cin will automatically flush cout.
[/edit]

Quote:

I have no idea how to output the encrypted numbers all at the same time.
Print spaces instead of newlines in your output? Read data one line at a time, and then parse the numbers in that line, and then finally print a newline?
• 08-13-2008
deletedforumuser
Here's my code.

Code:

``` #include <iostream> #include <fstream> #include <string> #include <windows.h> using namespace std;   void encrypt(char str[],int key) { unsigned int i; for(i=0;i<strlen(str);++i) { if(str[i] != key) str[i] = str[i] - key; } } void decrypt(char str[],int key) { unsigned int i; for(i=0;i<strlen(str);++i) { if(str[i] != key) str[i] = str[i] + key; } }   int main() { bool ProgramRunning = true; int key; int choice; char FileLoc[1000]; char str[100000]; string All; string DataRenew;     fstream PhpFile; ofstream Write; while(ProgramRunning) { key = 0; system("cls"); cout <<"1. Encrypt a file."<<endl; cout <<"2. Decrypt a file."<<endl; cout <<"3. Add Encrypted files to server."<<endl; cout <<"4. Remove Encrypted files from server."<<endl; cin >> choice;   switch (choice) { case 1: key = 0; system("cls"); cout <<"Write the file location."<<endl; cout <<">> "; cin >> FileLoc; cin.get(); PhpFile.open(FileLoc); if (!PhpFile) { key = 0; system("cls"); cout << "Could not find file.Press enter to continue."; cin.get(); } else if(PhpFile) { key = 0; system("cls"); cout <<"File found."<<endl; cout <<"\n"; cout <<"File code: \n\n\n\n"<<endl; while(!PhpFile.eof()) { PhpFile.getline(str, 100000); All = All + str + "\n"; } strcpy_s(str, All.c_str()); cout << str <<" \n\n\n\n"<<endl;   cout <<"Please write the encryption key. NUMBERS ONLY!"<<endl; cout <<">> "; cin >> key; cin.get(); system("cls"); encrypt(str,key); cout<<"Encrypted value = "<<str<<endl; key = 0; PhpFile.close(); cout <<"\n\n"; cout <<"Do you wish to save the Encrypted file?"<<endl; cout <<"1.Yes"<<endl; cout <<"2.No"<<endl; cout <<">> "<<endl; cin >> choice; switch (choice) { case 1: Write.open(FileLoc); Write << str; Write.close(); break; case 2: key = 0; cin.get(); break; } break; } case 2: key = 0; system("cls"); cout <<"Write the file location."<<endl; cout <<">> "; cin >> FileLoc; cin.get(); PhpFile.open(FileLoc); if (!PhpFile) { key = 0; system("cls"); cout << "Could not find file.Press enter to continue."; choice = 0; cin.get(); cin.get(); } else if(PhpFile) { key = 0; system("cls"); cout <<"\n"; cout <<"File code: \n\n\n\n"<<endl; while(!PhpFile.eof()) { PhpFile.getline(str, 100000); All = All + str + "\n"; } strcpy_s(str, All.c_str()); cout << str <<" \n\n\n\n"<<endl; cout <<"Please write the key."<<endl; cout <<">> "; cin >> key; cin.get(); while(key != 124) { PhpFile.getline(str, 100000); All = All + str; strcpy_s(str, All.c_str()); key++; system("cls"); cout <<key<<endl; decrypt(str,key); cout<<"Decrypted value = "<<str<<endl; DataRenew = DataRenew + str; All = ""; strcpy_s(str, All.c_str()); PhpFile.close(); PhpFile.open(FileLoc); } Write.open(FileLoc); Write << DataRenew; Write.close();   PhpFile.close(); cout <<"\n\n";   cout <<"Do you wish to save the Decrypted file?"<<endl; cout <<"1.Yes"<<endl; cout <<"2.No"<<endl; cout <<">> "<<endl; cin >> choice; switch (choice) { case 1: Write.open(FileLoc); Write << str; Write.close(); break; case 2: break; } cin.get(); break; } } } return 0; }```
I didn't continue working on it, i was doing this to learn.

And a small bug, when you encrypt a file. close the program, and re open it again to decrypt. just modify it in your needs.

+ You can modify this in whatever you want. example:

Make an encrypter program. That will encrypt automaticly and send it to the server.
The decrypter will read the file and decrypt it. and save it into a log file.
• 08-13-2008
arpsmack
dwks, all I have to say to you is: Codeform rules!
• 08-13-2008
dwks
Your code could use some improvement. For example: it's not indented at all; it's sprinkled with non-standardness like system("cls"), windows.h, and strcpy_s(); it uses a lot of magic numbers like 124 and 100000; it inefficiently calls strlen() inside loop conditions; it uses while(!eof); and it's not very well modularized.

But as you said, it was a learning program, so anyway . . . it's not something I'd show to someone else who's trying to learn, though. :)

Quote:

dwks, all I have to say to you is: Codeform rules!
Thank you. :D
• 08-13-2008
deletedforumuser
This code , i made 3 weeks after i started c++.

Now, im a bit more experienced, i just checked in my program list. and saw it.

So i just post it hehe.

It's been 2 months now. im starting 3d soon. I stopped working on the ascii rpg because i decided to start on win32 API.

So i guess ill just stop posting bad stuff. I'm having a problem with visual studio right now.

-By the way. If you look at the oldest thread i posted. You would laugh at me. I was 13. My english was so bad. Go check it out. I'm 15 now. i started programming when i was 11. but i quit directly because a hello world program was too hard for me...

My english is still a little bit bad now. But better then before by the way. If your looking to laugh at my english. check my oldest thread.
• 08-13-2008
dwks
I'm just saying that I probably wouldn't have learned very much from your code were I in the original poster's position. Sorry if I offended you.

Just running the program through indent would help enormously. Here you go, I've done it for you. And through codeform too, of course. ;)
Code:

```#include <iostream> #include <fstream> #include <string> #include <windows.h> using namespace std; void encrypt (char str[], int key) {   unsigned int i;   for (i = 0; i < strlen (str); ++i)     {       if (str[i] != key)         str[i] = str[i] - key;     } } void decrypt (char str[], int key) {   unsigned int i;   for (i = 0; i < strlen (str); ++i)     {       if (str[i] != key)         str[i] = str[i] + key;     } } int main () {   bool ProgramRunning = true;   int key;   int choice;   char FileLoc[1000];   char str[100000];   string All;   string DataRenew;   fstream PhpFile;   ofstream Write;   while (ProgramRunning)     {       key = 0;       system ("cls");       cout << "1. Encrypt a file." << endl;       cout << "2. Decrypt a file." << endl;       cout << "3. Add Encrypted files to server." << endl;       cout << "4. Remove Encrypted files from server." << endl;       cin >> choice;       switch (choice)         {         case 1:           key = 0;           system ("cls");           cout << "Write the file location." << endl;           cout << ">> ";           cin >> FileLoc;           cin.get ();           PhpFile.open (FileLoc);           if (!PhpFile)             {               key = 0;               system ("cls");               cout << "Could not find file.Press enter to continue.";               cin.get ();             }           else if (PhpFile)             {               key = 0;               system ("cls");               cout << "File found." << endl;               cout << "\n";               cout << "File code: \n\n\n\n" << endl;               while (!PhpFile.eof ())                 {                   PhpFile.getline (str, 100000);                   All = All + str + "\n";                 }               strcpy_s (str, All.c_str ());               cout << str << " \n\n\n\n" << endl;               cout << "Please write the encryption key. NUMBERS ONLY!" <<                 endl;               cout << ">> ";               cin >> key;               cin.get ();               system ("cls");               encrypt (str, key);               cout << "Encrypted value = " << str << endl;               key = 0;               PhpFile.close ();               cout << "\n\n";               cout << "Do you wish to save the Encrypted file?" << endl;               cout << "1.Yes" << endl;               cout << "2.No" << endl;               cout << ">> " << endl;               cin >> choice;               switch (choice)                 {                 case 1:                   Write.open (FileLoc);                   Write << str;                   Write.close ();                   break;                 case 2:                   key = 0;                   cin.get ();                   break;                 }               break;             }         case 2:           key = 0;           system ("cls");           cout << "Write the file location." << endl;           cout << ">> ";           cin >> FileLoc;           cin.get ();           PhpFile.open (FileLoc);           if (!PhpFile)             {               key = 0;               system ("cls");               cout << "Could not find file.Press enter to continue.";               choice = 0;               cin.get ();               cin.get ();             }           else if (PhpFile)             {               key = 0;               system ("cls");               cout << "\n";               cout << "File code: \n\n\n\n" << endl;               while (!PhpFile.eof ())                 {                   PhpFile.getline (str, 100000);                   All = All + str + "\n";                 }               strcpy_s (str, All.c_str ());               cout << str << " \n\n\n\n" << endl;               cout << "Please write the key." << endl;               cout << ">> ";               cin >> key;               cin.get ();               while (key != 124)                 {                   PhpFile.getline (str, 100000);                   All = All + str;                   strcpy_s (str, All.c_str ());                   key++;                   system ("cls");                   cout << key << endl;                   decrypt (str, key);                   cout << "Decrypted value = " << str << endl;                   DataRenew = DataRenew + str;                   All = "";                   strcpy_s (str, All.c_str ());                   PhpFile.close ();                   PhpFile.open (FileLoc);                 }               Write.open (FileLoc);               Write << DataRenew;               Write.close ();               PhpFile.close ();               cout << "\n\n";               cout << "Do you wish to save the Decrypted file?" << endl;               cout << "1.Yes" << endl;               cout << "2.No" << endl;               cout << ">> " << endl;               cin >> choice;               switch (choice)                 {                 case 1:                   Write.open (FileLoc);                   Write << str;                   Write.close ();                   break;                 case 2:                   break;                 }               cin.get ();               break;             }         }     }   return 0; }```
• 08-14-2008
freddyvorhees
Quote:

Print spaces instead of newlines in your output? Read data one line at a time, and then parse the numbers in that line, and then finally print a newline?
I dont really understand what you mean.what I meant is, if I input 3 numbers, the 3 numbers will be encrypted. But I dont know here to store the value of those 3 numbers. Because our sir wants that you can input as much number as you like, and then output them all at the same time, like in the sample. What Ive done is you can intput the number it will be encrypted then stored in encrypt1 for the 1st digit,and so on. It will be outputed then it will be ready for the next loop.
• 08-15-2008
dwks
First of all, you probably don't need to actually store all of the numbers. You could parse each number at a time, as you are doing. You don't have to do any operations that I can see which involve every number, so why not deal with just one number at a time?

Of course, you'll need to be able to tell how many numbers are on a line. You can't just print "Enter numbers to encrypt ..." before every number, because if the user enters more than one number at a time, there will be spurious output. The best way to deal with this is to read in one line at a time. Then you can parse that line into numbers with stringstreams.
Code:

```#include <string>  // for std::string #include <sstream>  // for std::istringstream #include <iostream>  // for std::cin using namespace std;  // I wouldn't recommend this, but you already have it // ... string line; getline(cin, line); istringstream ss(line); int number; while(ss >> number) {     // handle number in some way }```
All you have to do in your output to make it look like you handled all the numbers at once is to only print one newline per line the user enters. In other words, print spaces for each number on a line, and then print a newline after you've processed all the numbers on a line.
• 08-15-2008
arpsmack
If all you want to do is store up all the numbers and print them all at once, just use one of the STL containers. For example, using a list:
Code:

```int main(void) {   Number n;   list<Number> numbers;     cout << "Enter the numbers to encrypt:" << endl;     while (true) {       cin >> n;       if (n.value() == 0) break;       n.encrypt();       numbers.push_back(n);   }     for (list<Number>::iterator i = numbers.begin(); i != numbers.end(); i++)       cout << *i << " ";     return 0; }```
• 08-15-2008
freddyvorhees
Nice, thanks!