Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hill Chrome:
- #include <iostream>
- #include <string>
- #include <math.h>
- using namespace std;
- int main()
- {
- char plaintext[3];
- int key[3][3];
- cout<<"Enter the elements for the key(integers only):"<<endl;
- for(int i=0;i<3;i++)
- for(int j=0;j<3;j++)
- cin>>key[i][j];
- cout<<"The entered key is :"<<endl;
- for(int i=0;i<3;i++)
- {
- cout<<"\n";
- for(int j=0;j<3;j++)
- cout<<key[i][j]<<" ";
- }
- cout<<"\n";
- cout<<"Enter a three letter word:"<<endl;
- cin>>plaintext;
- int message[3][1];
- for(int k=0;k<3;k++)
- for(int j=0;j<1;j++)
- message[k][j]=((int)plaintext[k]-97);
- cout<<"The message matrix is:"<<endl;
- for(int i=0;i<3;i++)
- {
- cout<<"\n";
- for(int j=0;j<1;j++)
- cout<<message[i][j];
- }
- cout<<"\n";
- cout<<"The multiplied matrix is:"<<endl;
- int k,i,j;
- int c[3][1];
- for(i=0;i<3;++i)
- {
- for(j=0;j<1;++j)
- {
- c[i][j]=0;
- for(k=0;k<3;++k)
- c[i][j]=c[i][j]+(key[i][k]*message[k][j]);
- cout<<c[i][j]<<" ";
- }
- cout<<"\n";
- }
- cout<<"The encrypted data is:"<<endl;
- for(int i=0;i<1;i++)
- {
- cout<<"\n";
- for(int j=0;j<3;j++)
- cout<<(char)((fmod(c[i][j],26)+97));
- }
- cout<<"\n";
- float det=0;
- float ikey[3][3];
- for(i = 0; i < 3; i++)
- det=det + (key[0][i] * (key[1][(i+1)%3] * key[2][(i+2)%3] - key[1][(i+2)%3] * key[2][(i+1)%3]));
- cout<<"Inverse of matrix is:"<<endl;
- for(i = 0; i < 3; i++){
- for(j = 0; j < 3; j++)
- {
- cout<<((key[(j+1)%3][(i+1)%3] * key[(j+2)%3][(i+2)%3]) - (key[(j+1)%3][(i+2)%3] * key[(j+2)%3][(i+1)%3]))/det<<"\t";
- ikey[i][j]=((key[(j+1)%3][(i+1)%3] * key[(j+2)%3][(i+2)%3]) - (key[(j+1)%3][(i+2)%3] * key[(j+2)%3][(i+1)%3]))/det;
- }
- cout<<"\n";
- }
- float decrypt[3][1];
- for(i=0;i<3;i++)
- {
- for(j=0;j<1;j++)
- {
- decrypt[i][j]=0;
- for(k=0;k<3;k++)
- decrypt[i][j]=decrypt[i][j]+(ikey[i][k]*c[k][j]);
- cout<<decrypt[i][j]<<" ";
- }
- cout<<"\n";
- }
- cout<<"The decrypted data is:"<<endl;
- for(i=0;i<3;i++)
- cout<<(char)(fmod(decrypt[i][0],26)+97);
- }
- -----------------------------------------------------------------------------------
- Affine:
- #include <iostream>
- #include <string>
- #include <math.h>
- using namespace std;
- int main()
- {
- string plaintext,encrypt,decrypt;
- int inv;
- cout<<"Enter the message to be encrypted:"<<endl;
- getline(cin,plaintext);
- float a,b;
- cout<<"Enter the values for a and b:"<<endl;
- cin>>a>>b;
- int length=(int)plaintext.length();
- for(int i=0;i<length;i++)
- {
- encrypt[i]=(char)(fmod((a*(plaintext[i]-97)+b),26)+97);
- }
- for(int i=0;i<length;i++)
- {
- if(encrypt[i]=='V')
- {
- cout<<" ";
- }
- else
- {
- cout<<encrypt[i];
- }
- }
- cout<<"\n";
- cout<<"The deciphered text is:"<<endl;
- int f=0;
- inv=0;
- for(int i=0;i<26;i++)
- {
- f=fmod((a*i),26);
- if(f==1)
- {
- inv=i;
- }
- }
- for(int i=0;i<length;i++)
- {
- decrypt[i]=(char)(fmod(inv*((encrypt[i]-97)-b),26)+97);
- }
- for(int i=0;i<length;i++)
- {
- if(decrypt[i]=='T')
- {
- cout<<" ";
- }
- else
- {
- cout<<decrypt[i];
- }
- }
- }
- ---------------------------------------------------------------------------------------------------------
- Play Fair Cipher:
- #include <stdio.h>
- #define siz 5
- void encrypt(int *i, int *j)
- {
- (*i)++,(*j)++;
- if((*i)==siz) *i=0;
- else if((*j)==siz) *j=0;
- }
- void playfair(char ch1,char ch2, char mat[siz][siz])
- {
- int j,m,n,p,q,c,k;
- for(j=0,c=0;(c<2)||(j<siz);j++)
- for(k=0;k<siz;k++)
- if(mat[j][k] == ch1)
- m=j,n=k,c++;
- else if(mat[j][k] == ch2)
- p=j,q=k,c++;
- if(m==p)
- encrypt(&n,&q);
- else if(n==q)
- encrypt(&m,&p);
- else
- n+=q,q=n-q,n-=q;
- printf("%c%c",mat[m][n],mat[p][q]);
- }
- void main()
- {
- char mat[siz][siz],key[10],str[25]={0};
- int m,n,i,j;
- char temp;
- printf("Enter Key:");
- gets(key);
- m=n=0;
- for(i=0;key[i]!='\0';i++)
- {
- for(j=0;j<i;j++)
- if(key[j] == key[i]) break;
- if(key[i]=='j') key[i]='i';
- if(j>=i)
- {
- mat[m][n++] = key[i];
- if(n==siz)
- n=0,m++;
- }
- }
- for(i=97;i<=122;i++)
- {
- for(j=0;key[j]!='\0';j++)
- if(key[j] == i)
- break;
- else if(i=='j')
- break;
- if(key[j]=='\0')
- {
- mat[m][n++] = i;
- if(n==siz) n=0,m++;
- }
- }
- printf("Enter input String:");
- gets(str);
- printf("\n\nMatrix :\n");
- for(i=0;i<siz;i++)
- {
- for(j=0;j<siz;j++)
- printf("%c\t",mat[i][j]);
- printf("\n");
- }
- printf("\n\nEntered text :%s\nCipher Text :",str);
- for(i=0;str[i]!='\0';i++)
- {
- temp = str[i++];
- if(temp == 'j') temp='i';
- if(str[i]=='\0')
- playfair(temp,'x',mat);
- else
- {
- if(str[i]=='j') str[i]='i';
- if(temp == str[i])
- {
- playfair(temp,'x',mat);
- playfair('x',str[i],mat);
- }
- else
- playfair(temp,str[i],mat);
- }
- }
- }
- ---------------------------------------------------------------------------
- Ceaser Cipher:
- #include <iostream>
- #include <string>
- using namespace std;
- int key;
- char caesar(char);
- int main()
- {
- string input;
- cout<<"Enter the key:";
- cin>>key;
- cout << "Enter the text: " << endl;
- cin>>input;
- string output = "";
- for (int x = 0; x < input.length(); x++)
- {
- output += caesar(input[x]);
- }
- cout << output << endl;
- }
- char caesar(char c)
- {
- if (isalpha(c))
- {
- c = toupper(c);
- c = (((c - 65) + key) % 26) + 65;
- }
- return c;
- }
- ------------------------------------------------------------------------------
- RSA:
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- #include<math.h>
- #include<string.h>
- long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;
- char msg[100];
- int prime(long int);
- void ce();
- long int cd(long int);
- void encrypt();
- void decrypt();
- void main()
- {
- clrscr();
- printf("\nENTER FIRST PRIME NUMBER\n");
- scanf("%d",&p);
- flag=prime(p);
- if(flag==0)
- {
- printf("\nWRONG INPUT\n");
- getch();
- exit(1);
- }
- printf("\nENTER ANOTHER PRIME NUMBER\n");
- scanf("%d",&q);
- flag=prime(q);
- if(flag==0||p==q)
- {
- printf("\nWRONG INPUT\n");
- getch();
- exit(1);
- }
- printf("\nENTER MESSAGE\n");
- fflush(stdin);
- scanf("%s",msg);
- for(i=0;msg[i]!=NULL;i++)
- m[i]=msg[i];
- n=p*q;
- t=(p-1)*(q-1);
- ce();
- printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
- for(i=0;i<j-1;i++)
- printf("\n%ld\t%ld",e[i],d[i]);
- encrypt();
- decrypt();
- getch();
- }
- int prime(long int pr)
- {
- int i;
- j=sqrt(pr);
- for(i=2;i<=j;i++)
- {
- if(pr%i==0)
- return 0;
- }
- return 1;
- }
- void ce()
- {
- int k;
- k=0;
- for(i=2;i<t;i++)
- {
- if(t%i==0)
- continue;
- flag=prime(i);
- if(flag==1&&i!=p&&i!=q)
- {
- e[k]=i;
- flag=cd(e[k]);
- if(flag>0)
- {
- d[k]=flag;
- k++;
- }
- if(k==99)
- break;
- }
- }
- }
- long int cd(long int x)
- {
- long int k=1;
- while(1)
- {
- k=k+t;
- if(k%x==0)
- return(k/x);
- }
- }
- void encrypt()
- {
- long int pt,ct,key=e[0],k,len;
- i=0;
- len=strlen(msg);
- while(i!=len)
- {
- pt=m[i];
- pt=pt-96;
- k=1;
- for(j=0;j<key;j++)
- {
- k=k*pt;
- k=k%n;
- }
- temp[i]=k;
- ct=k+96;
- en[i]=ct;
- i++;
- }
- en[i]=-1;
- printf("\nTHE ENCRYPTED MESSAGE IS\n");
- for(i=0;en[i]!=-1;i++)
- printf("%c",en[i]);
- }
- void decrypt()
- {
- long int pt,ct,key=d[0],k;
- i=0;
- while(en[i]!=-1)
- {
- ct=temp[i];
- k=1;
- for(j=0;j<key;j++)
- {
- k=k*ct;
- k=k%n;
- }
- pt=k+96;
- m[i]=pt;
- i++;
- }
- m[i]=-1;
- printf("\nTHE DECRYPTED MESSAGE IS\n");
- for(i=0;m[i]!=-1;i++)
- printf("%c",m[i]);
- }
- ----------------------------------------------------------------------
- Vigenere
- #include <iostream>
- #include <string>
- #include <math.h>
- using namespace std;
- int main()
- {
- char plaintext[3];
- int key[3][3];
- cout<<"Enter the elements for the key(integers only):"<<endl;
- for(int i=0;i<3;i++)
- for(int j=0;j<3;j++)
- cin>>key[i][j];
- cout<<"The entered key is :"<<endl;
- for(int i=0;i<3;i++)
- {
- cout<<"\n";
- for(int j=0;j<3;j++)
- cout<<key[i][j]<<" ";
- }
- cout<<"\n";
- cout<<"Enter a three letter word:"<<endl;
- cin>>plaintext;
- int message[3][1];
- for(int k=0;k<3;k++)
- for(int j=0;j<1;j++)
- message[k][j]=((int)plaintext[k]-97);
- cout<<"The message matrix is:"<<endl;
- for(int i=0;i<3;i++)
- {
- cout<<"\n";
- for(int j=0;j<1;j++)
- cout<<message[i][j];
- }
- cout<<"\n";
- cout<<"The multiplied matrix is:"<<endl;
- int k,i,j;
- int c[3][1];
- for(i=0;i<3;++i)
- {
- for(j=0;j<1;++j)
- {
- c[i][j]=0;
- for(k=0;k<3;++k)
- c[i][j]=c[i][j]+(key[i][k]*message[k][j]);
- cout<<c[i][j]<<" ";
- }
- cout<<"\n";
- }
- cout<<"The encrypted data is:"<<endl;
- for(int i=0;i<1;i++)
- {
- cout<<"\n";
- for(int j=0;j<3;j++)
- cout<<(char)((fmod(c[i][j],26)+97));
- }
- cout<<"\n";
- float det=0;
- float ikey[3][3];
- for(i = 0; i < 3; i++)
- det=det + (key[0][i] * (key[1][(i+1)%3] * key[2][(i+2)%3] - key[1][(i+2)%3] * key[2][(i+1)%3]));
- cout<<"Inverse of matrix is:"<<endl;
- for(i = 0; i < 3; i++){
- for(j = 0; j < 3; j++)
- {
- cout<<((key[(j+1)%3][(i+1)%3] * key[(j+2)%3][(i+2)%3]) - (key[(j+1)%3][(i+2)%3] * key[(j+2)%3][(i+1)%3]))/det<<"\t";
- ikey[i][j]=((key[(j+1)%3][(i+1)%3] * key[(j+2)%3][(i+2)%3]) - (key[(j+1)%3][(i+2)%3] * key[(j+2)%3][(i+1)%3]))/det;
- }
- cout<<"\n";
- }
- float decrypt[3][1];
- for(i=0;i<3;i++)
- {
- for(j=0;j<1;j++)
- {
- decrypt[i][j]=0;
- for(k=0;k<3;k++)
- decrypt[i][j]=decrypt[i][j]+(ikey[i][k]*c[k][j]);
- cout<<decrypt[i][j]<<" ";
- }
- cout<<"\n";
- }
- cout<<"The decrypted data is:"<<endl;
- for(i=0;i<3;i++)
- cout<<(char)(fmod(decrypt[i][0],26)+97);
- }
- ---------------------------------------------------------------------------------
- Vignere Cipher:
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <process.h>
- void vigenereCipher(char *,char *);
- void encipher();
- void decipher();
- int main()
- {
- int choice;
- while(1)
- {
- printf("1. Encrypt Text \n");
- printf("2. Decrypt Text \n");
- printf("3. Exit \n");
- printf("Enter Your Choice : ");
- scanf("%d",&choice);
- fflush(stdin);
- if(choice == 3)
- exit(0);
- else if(choice == 1)
- encipher();
- else if(choice == 2)
- decipher();
- else
- printf("Please Enter Valid Option.");
- }
- return 0;
- }
- void encipher()
- {
- unsigned int i,j;
- char input[257],key[33];
- printf("Enter Text to be Encrypted [Max. 256 characters/ only alphabets]:n ");
- gets(input);
- printf("Enter Encryption Key [Max. 32 Characters/ only alphabets]: ");
- gets(key);
- for(i=0,j=0;i<strlen(input);i++,j++)
- {
- if(j>=strlen(key))
- {
- j=0;
- }
- printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-65))%26));
- }
- }
- void decipher()
- {
- unsigned int i,j;
- char input[257],key[33];
- int value;
- printf("Enter Text to be Decrypted [Max. 256 characters/ only alphabets]:n ");
- gets(input);
- printf("Enter Decryption Key [Max. 32 Characters/ only aphabets]: ");
- gets(key);
- for(i=0,j=0;i<strlen(input);i++,j++)
- {
- if(j>=strlen(key))
- {
- j=0;
- }
- value = (toupper(input[i])-64)-(toupper(key[j])-64);
- if( value < 0)
- {
- value = value * -1;
- }
- printf("%c",65 + (value % 26));
- }
- }
Add Comment
Please, Sign In to add comment