PDA

View Full Version : Something about programming!


peter1988
Jan 13, 2009, 08:26 PM
Can anyone help me about this question>>>?? (Microsoft Visual C++)

A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped. All of their data is transmitted as four-digit integers. They have asked you to write a program that will encrypt their data so that it may be transmitted more securely. Your program should read a four-digit integer and encrypt it as follows: Replace each digit by the remainder after the sum of that digit plus 7 is divided by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer. (7 marks)

Sample output:

Enter a four-digit number to be encrypted: 5678
Encrypted number is 4523

simonjarvis
May 1, 2009, 10:13 PM
// Crude answer not very good as relies on transmitting
// only 4 digit integers but can be up to 32767 and cryptography is no good
// very easily broken but this is more of a simple exam question I guess as in
// real life this is not a practicle solution. You would want to encrypt the connection via
// SSH, CRYPTO RPC or Encrypted Winsock not the actual data unless it is a concern
// with capturing data on the internal network.

#define REV(x)((x + 3<10)?(x + 3):(x+3-10))
#define DECRYPTHOUSANDS(x)(REV((x / 1000)))
#define DECRYPHUNDREDS(x)(REV((x % 1000) / 100))
#define DECRYPTENS(x)(REV(((x % 1000) % 100) / 10))
#define DECRYPONES(x)(REV(((x % 1000) % 10)))
#define CRYPTHOUSANDS(x)( ((x / 1000) +7 ) % 10 )
#define CRYPHUNDREDS(x)( (((x % 1000) / 100) +7 ) % 10 )
#define CRYPTENS(x)( ((((x % 1000) % 100) / 10) +7 ) % 10 )
#define CRYPONES(x)( ((((x % 1000) % 100) % 10) +7 ) % 10 )
#define SWAP(v,x,y,z)( (v * 10) + ( x ) + ( y * 1000 ) + ( z * 100 ) )

int Cryp(int _val)
{
int _thousand(CRYPTHOUSANDS(_val));
int _hundred(CRYPHUNDREDS(_val));
int _tens(CRYPTENS(_val));
int _ones(CRYPONES(_val));
return SWAP (_thousand,_hundred,_tens,_ones);;
}
int DeCryp(int _val)

{
int _thousand(DECRYPTHOUSANDS(_val));
int _hundred(DECRYPHUNDREDS(_val));
int _tens(DECRYPTENS(_val));
int _ones(DECRYPONES(_val));
return SWAP (_thousand,_hundred,_tens,_ones);

}

shihouzhuge
Oct 6, 2009, 09:49 AM
Can anyone help me about this question>>>???(Microsoft Visual C++)

A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped. All of their data is transmitted as four-digit integers. They have asked you to write a program that will encrypt their data so that it may be transmitted more securely. Your program should read a four-digit integer and encrypt it as follows: Replace each digit by the remainder after the sum of that digit plus 7 is divided by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer. (7 marks)

Sample output:

Enter a four-digit number to be encrypted: 5678
Encrypted number is 4523


#include<iostream.h>
void main()
{
cout<<"Enter a four-digit number to be encrypted:";
char a[4];// input the number,it is char
cin>>a;
int b[4];
b[0]=(int(a[2])-int('0')+7)%10;// change the char to int;
b[1]=(int(a[3])-int('0')+7)%10;
b[2]=(int(a[0])-int('0')+7)%10;
b[3]=(int(a[1])-int('0')+7)%10;
cout<<"Encrypted number is:";
for(int I=0;i<4;i++){ //output the answer
cout<<b[i];
}
}

shihouzhuge
Oct 6, 2009, 09:51 AM
I used Microsoft visual c++6.0,and the answer is compiled without faults.
Good luck!

Dalia_2011
Dec 29, 2010, 08:55 AM
I want answer this question in java
Q1. A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped. All of their data is transmitted as four-digit integers. They have asked you to write a program that will encrypt their data so that it may 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 and the second with the fourth. Then print the encrypted integer.
Write a program that inputs four-digit integer and prints an encrypted integer number and decrypts it to form the original number. (Use a method to decrypt and another to encrypt)