123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- #include <iostream>
- #include <map>
- using namespace std;
- int main() {
-
- int x,y,i,j,k,count=0;
- string keyword,message,new_message,encrypted_text,decrypted_text,row_coordinates,column_coordinates,location_of_x;
-
- cout<<"\nEnter the size of matrix \n";
- cin>>x>>y;
-
- char matrix[x][y];
- map<char,int> flag;
-
- memset(matrix, '0', sizeof(matrix[0][0]) * x * y);
- cout<<"Enter the keyword\n";
- cin>>keyword;
-
- cout<<"Enter the message to be encrypted: \n";
- cin>>message;
-
- for(i=0;message[i]!='\0';i++)
- {
- if((message[i]==message[i-1])&&count%2!=0)
-
- {
-
- new_message+='x';
- location_of_x+=i+'0';
- count++;
- }
- new_message+=message[i];
- count++;
- }
- if(count%2==1)
- {
- new_message+='x';
- location_of_x+=i+'0';
-
-
- }
-
- for(i=0,k=0;keyword[i]!='\0';i++,k++)
- {
-
- matrix[0][k]=keyword[i];
- flag[keyword[i]]=1;
- for(j=0;new_message[j]!='\0';j++)
- if(keyword[i]==new_message[j])
- {
-
- row_coordinates+=new_message[j];
- column_coordinates+=new_message[j];
- row_coordinates+=(k/5)+'0';
- column_coordinates+=(k%5)+'0';
- }
- }
-
-
-
-
- for(i=97;i<=122;i++)
- {
-
- if(flag[(char)i]==0)
- {
- matrix[0][k]=(char)i;
-
- for(j=0;new_message[j]!='\0';j++)
- if(matrix[0][k]==new_message[j])
- {
- row_coordinates+=new_message[j];
- column_coordinates+=message[j];
- row_coordinates+=(k/5)+'0';
- column_coordinates+=(k%5)+'0';
- }
- k++;
-
- }
- }
-
-
-
-
-
- k=0;
- label:for(i=0;i<x&&new_message[k]!='\0';i++)
- for(j=0;j<y;j++)
- if(matrix[i][j]==new_message[k])
- {
-
-
-
- if(k%2==0)
- {
- int row_value=(int)row_coordinates[row_coordinates.find(new_message[k+1])+1]-48;
- int column_value=(int)column_coordinates[column_coordinates.find(new_message[k+1])+1]-48;
- if(row_value==i)
- {
- int column_value=j+1;
- if(column_value>=y)
- column_value=0;
-
- encrypted_text+=matrix[row_value][column_value];
- }
- else if(column_value==j)
- {
-
- int row_value=i+1;
- if(row_value>=x)
- row_value=0;
- encrypted_text+=matrix[row_value][column_value];
- }
-
- else
- encrypted_text+=matrix[row_value][j];
-
- }
-
- if(k%2==1)
- {
- int row_value=(int)row_coordinates[row_coordinates.find(new_message[k-1])+1]-48;
- int column_value=(int)column_coordinates[column_coordinates.find(new_message[k-1])+1]-48;
-
- if(row_value==i)
- {
- int column_value=j+1;
- if(column_value>=y)
- column_value=0;
-
- encrypted_text+=matrix[row_value][column_value];
- }
- else if(column_value==j)
- {
-
- int row_value=i+1;
- if(row_value>=x)
- row_value=0;
- encrypted_text+=matrix[row_value][column_value];
-
-
- }
-
- else
-
- encrypted_text+=matrix[row_value][j];
-
- }
- k++;
- goto label;
- }
-
-
- cout<<"Encrypted text is: "<<encrypted_text<<endl;
-
- new_message=row_coordinates=column_coordinates="";
-
- new_message=encrypted_text;
-
- for(i=0,k=0;keyword[i]!='\0';i++,k++)
- {
-
-
- matrix[0][k]=keyword[i];
- flag[keyword[i]]=1;
- for(j=0;new_message[j]!='\0';j++)
- if(keyword[i]==new_message[j])
- {
-
- row_coordinates+=new_message[j];
- column_coordinates+=new_message[j];
- row_coordinates+=(k/5)+'0';
- column_coordinates+=(k%5)+'0';
- }
-
- }
-
-
-
-
- for(i=97;i<=122;i++)
- {
-
- if(flag[(char)i]==0)
- {
- matrix[0][k]=(char)i;
-
- for(j=0;new_message[j]!='\0';j++)
- if(matrix[0][k]==new_message[j])
- {
-
- row_coordinates+=new_message[j];
- column_coordinates+=new_message[j];
- row_coordinates+=(k/5)+'0';
- column_coordinates+=(k%5)+'0';
- }
- k++;
-
- }
- }
-
- k=count=0;
-
-
- label2:for(i=0;i<x&&encrypted_text[k]!='\0';i++)
- {
- if(k==(location_of_x[count]-48))
- {
- count++;
- k++;
- continue;
- }
-
- for(j=0;j<y;j++)
- if(matrix[i][j]==encrypted_text[k])
- {
-
-
- if(k%2==0)
- {
- int row_value=(int)row_coordinates[row_coordinates.find(encrypted_text[k+1])+1]-48;
- int column_value=(int)column_coordinates[column_coordinates.find(encrypted_text[k+1])+1]-48;
-
- if(row_value==i)
- {
- cout<<"111111"<<endl;
- int column_value=j-1;
- if(column_value<0)
- column_value=y-1;
-
- decrypted_text+=matrix[row_value][column_value];
- }
- else if(column_value==j)
- {
-
- int row_value=i-1;
- if(row_value<0)
- row_value=x-1;
-
-
- decrypted_text+=matrix[row_value][column_value];
-
-
- }
-
- else
- decrypted_text+=matrix[row_value][j];
-
- }
-
- if(k%2==1)
- {
- int row_value=(int)row_coordinates[row_coordinates.find(encrypted_text[k-1])+1]-48;
- int column_value=(int)column_coordinates[column_coordinates.find(encrypted_text[k-1])+1]-48;
-
- if(row_value==i)
- {
- int column_value=j-1;
- if(column_value<0)
- column_value=y-1;
-
- decrypted_text+=matrix[row_value][column_value];
- }
- else if(column_value==j)
- {
- int row_value=i-1;
- if(row_value<0)
- row_value=x-1;
- decrypted_text+=matrix[row_value][column_value];
-
-
- }
-
- else
-
-
- decrypted_text+=matrix[row_value][j];
-
- }
- k++;
- goto label2;
- }
-
- }
-
-
-
- cout<<"Decrypted text is: "<<decrypted_text<<endl;
-
-
- return 0;
- }
- /*
- OUTPUT:-
-
- Enter the size of matrix
- 5 5
- Enter the keyword
- monarchy
- Enter the message to be encrypted:
- vitpune
- Encrypted text is: gwkvovtj
- Decrypted text is: vitpune
-
- Enter the size of matrix
- 5 5
- Enter the keyword
- monarchy
- Enter the message to be encrypted:
- balloon
- Encrypted text is: ibusuhna
- Decrypted text is: balloon
-
- */
|