Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Stuck with one program.

Stuck with one program.

Scheduled Pinned Locked Moved Managed C++/CLI
securityhelptutorialquestionlounge
6 Posts 4 Posters 16 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    B Sudhir
    wrote on last edited by
    #1

    I was praciticing an exercise in the book called C How to program by deitel. In which a question is there about cryptography. Following is the question (Also notice to the phrase mentioned in BOLD letters).

    Quote:

    (Enforcing Privacy with Cryptography) The explosive growth of Internet communications and data storage on Internet-connected computers has greatly increased privacy concerns. The field of cryptography is concerned with coding data to make it difficult (and hopefully—with the most advanced schemes—impossible) for unauthorized users to read. In this exercise you’ll investigate a simple scheme for encrypting and decrypting data. A company that wants to send data over the Internet has asked you to write a program that will encrypt it so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer. Write a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the encryption scheme) to form the original number. [Optional reading project: Research “public key cryptography” in general and the PGP (Pretty Good Privacy) specific public key scheme. You may also want to investigate the RSA scheme, which is widely used in industrial-strength applications.]

    Following is the code block I've written for Encryption Scheme.

    #include

    void main(){
    int iFirst;
    int iSecond;
    int iThird;
    int iFourth;
    int iTemp;

    printf("\\nEnter four digits (with a space in between): ");
    scanf("%d %d %d %d", &iFirst, &iSecond, &iThird, &iFourth);
    
    iFirst = iFirst + 7;
    iSecond = iSecond + 7;
    iThird = iThird + 7;
    iFourth = iFourth + 7;
    
    iFirst = iFirst % 10;
    iSecond = iSecond % 10;
    iThird = iThird % 10;
    iFourth = iFourth % 10;
    
    //Swapping First <--> Third & Second <--> Fourth
    
    iTemp = iFirst;
    iFirst = iThird;
    iThird = iTemp;
    
    iTemp = iSecond;
    iSecond = iFourth;
    iFourth = iTemp;
    
    printf("\\nEncrypted Data - %d%d%d%d", iFirst, iSecond, iThird, iFourth);
    

    }

    Output -

    Quote:

    Enter fou

    J A M 3 Replies Last reply
    0
    • B B Sudhir

      I was praciticing an exercise in the book called C How to program by deitel. In which a question is there about cryptography. Following is the question (Also notice to the phrase mentioned in BOLD letters).

      Quote:

      (Enforcing Privacy with Cryptography) The explosive growth of Internet communications and data storage on Internet-connected computers has greatly increased privacy concerns. The field of cryptography is concerned with coding data to make it difficult (and hopefully—with the most advanced schemes—impossible) for unauthorized users to read. In this exercise you’ll investigate a simple scheme for encrypting and decrypting data. A company that wants to send data over the Internet has asked you to write a program that will encrypt it so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer. Write a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the encryption scheme) to form the original number. [Optional reading project: Research “public key cryptography” in general and the PGP (Pretty Good Privacy) specific public key scheme. You may also want to investigate the RSA scheme, which is widely used in industrial-strength applications.]

      Following is the code block I've written for Encryption Scheme.

      #include

      void main(){
      int iFirst;
      int iSecond;
      int iThird;
      int iFourth;
      int iTemp;

      printf("\\nEnter four digits (with a space in between): ");
      scanf("%d %d %d %d", &iFirst, &iSecond, &iThird, &iFourth);
      
      iFirst = iFirst + 7;
      iSecond = iSecond + 7;
      iThird = iThird + 7;
      iFourth = iFourth + 7;
      
      iFirst = iFirst % 10;
      iSecond = iSecond % 10;
      iThird = iThird % 10;
      iFourth = iFourth % 10;
      
      //Swapping First <--> Third & Second <--> Fourth
      
      iTemp = iFirst;
      iFirst = iThird;
      iThird = iTemp;
      
      iTemp = iSecond;
      iSecond = iFourth;
      iFourth = iTemp;
      
      printf("\\nEncrypted Data - %d%d%d%d", iFirst, iSecond, iThird, iFourth);
      

      }

      Output -

      Quote:

      Enter fou

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      To perform a reverse operation you have to not only reverse the operation direction but also the operations itself. So you have to:

      // swap here as already done
      // Can use the same code as the encoding because there is no difference in the result

      // Reverse adding 7
      iFirst -= 7;
      // ...

      // Reverse modulo 10 operation
      if (iFirst < 0)
      iFirst += 10;
      // ...

      [EDIT] Adding 7 followed by modulo 10 must be treated as single operation here (the modulo just ensures that the result is a single digit). Similar for the reverse operation: Subtract 7 and ensure that the result is a positive single digit number. [/EDIT]

      B M 2 Replies Last reply
      0
      • J Jochen Arndt

        To perform a reverse operation you have to not only reverse the operation direction but also the operations itself. So you have to:

        // swap here as already done
        // Can use the same code as the encoding because there is no difference in the result

        // Reverse adding 7
        iFirst -= 7;
        // ...

        // Reverse modulo 10 operation
        if (iFirst < 0)
        iFirst += 10;
        // ...

        [EDIT] Adding 7 followed by modulo 10 must be treated as single operation here (the modulo just ensures that the result is a single digit). Similar for the reverse operation: Subtract 7 and ensure that the result is a positive single digit number. [/EDIT]

        B Offline
        B Offline
        B Sudhir
        wrote on last edited by
        #3

        Cool... It worked so well Thank you so much for help. :)

        1 Reply Last reply
        0
        • B B Sudhir

          I was praciticing an exercise in the book called C How to program by deitel. In which a question is there about cryptography. Following is the question (Also notice to the phrase mentioned in BOLD letters).

          Quote:

          (Enforcing Privacy with Cryptography) The explosive growth of Internet communications and data storage on Internet-connected computers has greatly increased privacy concerns. The field of cryptography is concerned with coding data to make it difficult (and hopefully—with the most advanced schemes—impossible) for unauthorized users to read. In this exercise you’ll investigate a simple scheme for encrypting and decrypting data. A company that wants to send data over the Internet has asked you to write a program that will encrypt it so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer. Write a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the encryption scheme) to form the original number. [Optional reading project: Research “public key cryptography” in general and the PGP (Pretty Good Privacy) specific public key scheme. You may also want to investigate the RSA scheme, which is widely used in industrial-strength applications.]

          Following is the code block I've written for Encryption Scheme.

          #include

          void main(){
          int iFirst;
          int iSecond;
          int iThird;
          int iFourth;
          int iTemp;

          printf("\\nEnter four digits (with a space in between): ");
          scanf("%d %d %d %d", &iFirst, &iSecond, &iThird, &iFourth);
          
          iFirst = iFirst + 7;
          iSecond = iSecond + 7;
          iThird = iThird + 7;
          iFourth = iFourth + 7;
          
          iFirst = iFirst % 10;
          iSecond = iSecond % 10;
          iThird = iThird % 10;
          iFourth = iFourth % 10;
          
          //Swapping First <--> Third & Second <--> Fourth
          
          iTemp = iFirst;
          iFirst = iThird;
          iThird = iTemp;
          
          iTemp = iSecond;
          iSecond = iFourth;
          iFourth = iTemp;
          
          printf("\\nEncrypted Data - %d%d%d%d", iFirst, iSecond, iThird, iFourth);
          

          }

          Output -

          Quote:

          Enter fou

          A Offline
          A Offline
          Artur Linov IT
          wrote on last edited by
          #4

          This looks complicated, but I know a guy that can help with it. He is a broadcaster on LiveEdu, specialising in C++/Gamedev tutorials - you can contact him in livechat here: How to create an Isometric game in C++ - LiveEdu.tv[^]

          1 Reply Last reply
          0
          • B B Sudhir

            I was praciticing an exercise in the book called C How to program by deitel. In which a question is there about cryptography. Following is the question (Also notice to the phrase mentioned in BOLD letters).

            Quote:

            (Enforcing Privacy with Cryptography) The explosive growth of Internet communications and data storage on Internet-connected computers has greatly increased privacy concerns. The field of cryptography is concerned with coding data to make it difficult (and hopefully—with the most advanced schemes—impossible) for unauthorized users to read. In this exercise you’ll investigate a simple scheme for encrypting and decrypting data. A company that wants to send data over the Internet has asked you to write a program that will encrypt it so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer. Write a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the encryption scheme) to form the original number. [Optional reading project: Research “public key cryptography” in general and the PGP (Pretty Good Privacy) specific public key scheme. You may also want to investigate the RSA scheme, which is widely used in industrial-strength applications.]

            Following is the code block I've written for Encryption Scheme.

            #include

            void main(){
            int iFirst;
            int iSecond;
            int iThird;
            int iFourth;
            int iTemp;

            printf("\\nEnter four digits (with a space in between): ");
            scanf("%d %d %d %d", &iFirst, &iSecond, &iThird, &iFourth);
            
            iFirst = iFirst + 7;
            iSecond = iSecond + 7;
            iThird = iThird + 7;
            iFourth = iFourth + 7;
            
            iFirst = iFirst % 10;
            iSecond = iSecond % 10;
            iThird = iThird % 10;
            iFourth = iFourth % 10;
            
            //Swapping First <--> Third & Second <--> Fourth
            
            iTemp = iFirst;
            iFirst = iThird;
            iThird = iTemp;
            
            iTemp = iSecond;
            iSecond = iFourth;
            iFourth = iTemp;
            
            printf("\\nEncrypted Data - %d%d%d%d", iFirst, iSecond, iThird, iFourth);
            

            }

            Output -

            Quote:

            Enter fou

            M Offline
            M Offline
            Member_14969527
            wrote on last edited by
            #5

            the decryption is wrong 1.encryption #include void main(){ int i[4]; printf("\nEnter four digits (with a space in between): "); for(int j=0;j<4;j++) { scanf("%d", &i[j]); } i[1] = i[1]+7; i[2] = i[2]+7; i[3] = i[3]+7; i[0] = i[0]+7; i[1] = i[1]%10; i[2] = i[2]%10; i[3] = i[3]%10; i[0] = i[0]%10; int k=0; k = i[0]; i[0] = i[2]; i[2] = k; k = i[1]; i[1] = i[3]; i[3] = k; printf("\nEncrypted Data - %d%d%d%d", i[0], i[1], i[2], i[3]); } 2.decryption

            #include
            void main(){
            int i[4];
            printf("\nEnter four digits (with a space in between): ");
            for(int j=0;j<4;j++)
            {
            scanf("%d", &i[j]);
            }
            int k=0;
            k = i[0];
            i[0] = i[2];
            i[2] = k;
            k = i[1];
            i[1] = i[3];
            i[3] = k;
            for(int j=0;j<4;j++)
            {
            if(i[j]<7)
            {
            i[j]=i[j]+3;
            }
            else
            {
            i[j]=i[j]-7;
            }
            printf("%d",i[j]);
            }
            }

            1 Reply Last reply
            0
            • J Jochen Arndt

              To perform a reverse operation you have to not only reverse the operation direction but also the operations itself. So you have to:

              // swap here as already done
              // Can use the same code as the encoding because there is no difference in the result

              // Reverse adding 7
              iFirst -= 7;
              // ...

              // Reverse modulo 10 operation
              if (iFirst < 0)
              iFirst += 10;
              // ...

              [EDIT] Adding 7 followed by modulo 10 must be treated as single operation here (the modulo just ensures that the result is a single digit). Similar for the reverse operation: Subtract 7 and ensure that the result is a positive single digit number. [/EDIT]

              M Offline
              M Offline
              Member_14969527
              wrote on last edited by
              #6

              :thumbsup:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups