Cryptography

This is a discussion on Cryptography within the C++ Programming forums, part of the General Programming Boards category; This is the problem: ---------------------------------------------------------------------------------------------- (Cryptography) A company wants to transmit data over the telephone, but is concerned that its ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

    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
    securely. Your program should 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 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.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    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;
    }

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Convoluted (and codeformed? :P ) example aside, I think the OP really needs to read up on arrays.

    [edit] A note for arpsmack:

    Through some sort of black magic, cout is automatically flushed whenever you use cin.
    cin and cout are related, but only because they are tied together so that any request for input using cin will automatically flush cout.
    http://www.daniweb.com/forums/thread23372.html
    [/edit]

    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?
    Last edited by dwks; 08-13-2008 at 07:56 PM.

  5. #5
    deletedforumuser
    Guest
    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.
    Last edited by kevinawad; 08-13-2008 at 07:53 PM.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    dwks, all I have to say to you is: Codeform rules!

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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.

    dwks, all I have to say to you is: Codeform rules!
    Thank you.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    deletedforumuser
    Guest
    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.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    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.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    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;
    }

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Nice, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Cryptography
    By Vamsianitha in forum C Programming
    Replies: 3
    Last Post: 03-05-2003, 01:31 PM
  3. What does this mean for the world of cryptography?
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-07-2002, 11:55 PM
  4. cryptography
    By tsarena in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2002, 04:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21