Stuck with one program.
-
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
-
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
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]
-
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]
-
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
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[^]
-
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
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]);
}
} -
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]
:thumbsup: