Вы находитесь на странице: 1из 4

Meljun cortes ---CSCIL9

ENCRYPTION PROGRAM.....
/// PROBLEM - PAGE 137----2.37 ///----

PROBLEM:
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 C++ PROGRAM THAT ENCRYPTS THEIR DATA SO THAT IT MAY BE TRANSMITTED MORE SECURELY. YOUR PROGRAM SHOULD READ A FOUR-DIGIT AND ENCRYPTS 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 DECRYPTED IT TO FORM THE ORIGINAL NUMBER.

C++ SOURCE CODE:


#include<stdio.h> #include<iostream> #include<conio.h> using namespace std; int main() { //data is transmitted as 4 digit integers //4 digit integer only // //replace each digit by the sum of that digit + 7 mod 10 //swap first digit with third //swap second digit with fourth //print encrypted

//create a decrypter //firstdigit //cout<<(num%10000-num%1000)/1000 <<endl; //seconddigit //cout<<(num%1000-num%100)/100 <<endl; //thirddigit //cout<<(num%100-num%10)/10 <<endl; //fourthdigit //cout<<(num%10-num%1) <<endl; int num=0; int encnum=0; int digit[3]; cout<<"enter 4 digit number to be decrypted\n\n"; do{ cout<<"input :: "; cin>>num; if(num>10000 || num<1000) {cout<<"\n\nPlease Recheck\n\n*First Digit should not start in 0(zero)\n*number should at least contain 4 digits\n and not more than 4 digits\n\n";} }while(num>10000 || num<1000); if(num<10000 || num>1000) {cout<< "\n\n\n4 Digit Number to be decrypted :: '"<<num<<"'\n\n" ; //formula for converting each digit to 1 digit digit[0]=(num%10000-num%1000)/1000; digit[1]=(num%1000-num%100)/100; digit[2]=(num%100-num%10)/10; digit[3]=(num%10-num%1); //using the formula (digit+7)%10 digit[0]=(digit[0]+7)%10; digit[1]=(digit[1]+7)%10;

digit[2]=(digit[2]+7)%10; digit[3]=(digit[3]+7)%10; cout<<"First Digit :: "<< digit[0] << "\nSecond Digit :: "<<digit[1]<<"\nThird Digit :: "<<digit[2]<<"\nFourth Digit :: "<<digit[3]; //Transform to 4 digit form the encrypted number digit[0]=digit[0]*1000; digit[1]=digit[1]*100; digit[2]=digit[2]*10; digit[3]=digit[3]; encnum=digit[0]+digit[1]+digit[2]+digit[3]; cout<< "\n\nThe encrypted number is " <<encnum; cout<<endl <<endl; } //Reverting to original value int yesno= 0; cout<<"\n\ndo you want to revert to original value?(Y=1/N=0) :: "; do{ cin>> yesno; cout<< endl; if(yesno==1) { //reverting to one digit form digit[0]=digit[0]/1000; digit[1]=digit[1]/100; digit[2]=digit[2]/10; digit[3]=digit[3]; for(int i=0;i<=3;i++) { if(digit[i]==7)digit[i]=0; else if(digit[i]==8)digit[i]=1; else if(digit[i]==9)digit[i]=2; else if(digit[i]==0)digit[i]=3; else if(digit[i]==1)digit[i]=4; else if(digit[i]==2)digit[i]=5; else if(digit[i]==3)digit[i]=6; else if(digit[i]==4)digit[i]=7;

else if(digit[i]==5)digit[i]=8; else if(digit[i]==6)digit[i]=9; } cout<<"First Digit :: "<< digit[0] << "\nSecond Digit :: "<<digit[1]<<"\nThird Digit :: "<<digit[2]<<"\nFourth Digit :: "<<digit[3]; //revert to 4 digit form the decrypted number digit[0]=digit[0]*1000; digit[1]=digit[1]*100; digit[2]=digit[2]*10; digit[3]=digit[3]; num=digit[0]+digit[1]+digit[2]+digit[3]; cout<< "\n\nThe decrypted number is " <<num; cout<<endl <<endl; return 0; } if(yesno<1 || yesno>1) { cout<<"\n\ndo you want to revert to original value?(Y/N) :: "; } }while(yesno>1 || yesno<1); return 0; }