#include< stdio.h>
#include< conio.h>
int phi,M,n,e,d,C,FLAG;
int check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return;
}
FLAG = 0;
}
void encrypt()
{
int i;
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}
void decrypt()
{
int i;
M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}
void main()
{
int p,q,s;
clrscr();
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);
do
{
printf("\n\nEnter e\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&M);
encrypt();
printf("\n\nEnter the Cipher text\t: ");
scanf("%d",&C);
decrypt();
getch();
}
/*************** OUTPUT *****************
Enter Two Relatively Prime Numbers : 7 17
F(n) = 96
Enter e : 5
Public Key : {5,119}
Private Key : {77,119}
Enter The Plain Text : 19
Encrypted keyword : 66
Enter the Cipher text : 66
Decrypted keyword : 19 */
All programs for FE,SE,TE & BE in Java,C++,prolog,TASM.
Labels
- Books (2)
- CN and ACN (4)
- COA (5)
- Data Structure (7)
- Earn Online (2)
- Encrption and Decryption (2)
- TASM/Microprocessor (13)
Showing posts with label Encrption and Decryption. Show all posts
Showing posts with label Encrption and Decryption. Show all posts
Tuesday, September 21, 2010
Friday, September 3, 2010
Columnar Cipher Encryption And Decryption
import java.io.*;
public class Play
{
static String s1,st,d;
static StringBuffer s;
static int m,n,c,choice,p,q,k;
static int z[]=new int[10];
static char a[][];
public static void dis()
{
System.out.println();
System.out.println("Matrix :");
System.out.println();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]!='$')
System.out.print(a[i][j]+" ");
else
System.out.print(" ");
}
System.out.println();
}
System.out.println();
}
public static void enc(DataInputStream dis)throws Exception
{
while(true)
{
c=0;
s1="";
System.out.println("------------------------------------------------------------------------");
System.out.print("Enter columns Sequence between 1 to "+n+" : ");
st=dis.readLine();
d=st+d;
System.out.println();
for(int i=0;i<n;i++)
{
c=(int)st.charAt(i)-49;
for(int j=0;j<m;j++)
{
if(a[j][c]!='$')
s1=s1+a[j][c];
}
}
s1.trim();
c=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(c<s1.length())
a[i][j]=s1.charAt(c++);
else
a[i][j]='$';
dis();
System.out.println();
System.out.println();
System.out.print("Do You want to continue(yes(1)/no(0)) : ");
choice=Integer.parseInt(dis.readLine());
if(choice==0)
{
System.out.println("****************************************************************");
System.out.println();
System.out.println("Ecryption results in the ciphertext : "+s1);
System.out.println();
//System.exit(0);
return;
}
}
}
public static void dec()
{
k=0;
p=s1.length()/n;
q=s1.length()%n;
//System.out.println("p = "+p+" q = "+q+" d = "+d);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i][j]='$';
for(int i=0;i<d.length();i++)
{
c=(int)d.charAt(i)-49;
//System.out.println("c = "+c);
if(c>=q)
{
for(int j=0;j<p;j++)
{
a[j][c]=s1.charAt(k++);
}
}
else
{
for(int j=0;j<p+1;j++)
{
a[j][c]=s1.charAt(k++);
}
}
dis();
if(k==s1.length())
{
s1="";
k=0;
for(int x=0;x<m;x++)
for(int j=0;j<n;j++)
if(a[x][j]!='$')
{
s1=s1+a[x][j];
a[x][j]='$';
}
}
}
System.out.println("Decryption results in the plaintext : "+s1);
}
public static void main(String[] args)
{
try
{
DataInputStream dis=new DataInputStream(System.in);
System.out.print("Enter Plain text : ");
s1=dis.readLine();
s=new StringBuffer(s1);
//REMOVING WIDE-SPACES
for(int i=0;i<s.length();i++)
if(s.charAt(i)==' ')
s.deleteCharAt(i);
s1=new String(s);
d="";
System.out.println("Enter size of the array ");
System.out.print("Enter no of rows = ");
m=Integer.parseInt(dis.readLine());
System.out.print("Enter no of columns = ");
n=Integer.parseInt(dis.readLine());
a=new char[m][n];
c=0;
//ENTERING IN THE ARRAY
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(c<s1.length())
a[i][j]=s1.charAt(c++);
else
a[i][j]='$';
dis();
System.out.println("------------------------------------------------------------------------");
enc(dis);
System.out.println("-------------------------------Decryption-------------------------------");
dec();
}
catch(Exception e)
{}
}
}
/*
*/
public class Play
{
static String s1,st,d;
static StringBuffer s;
static int m,n,c,choice,p,q,k;
static int z[]=new int[10];
static char a[][];
public static void dis()
{
System.out.println();
System.out.println("Matrix :");
System.out.println();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]!='$')
System.out.print(a[i][j]+" ");
else
System.out.print(" ");
}
System.out.println();
}
System.out.println();
}
public static void enc(DataInputStream dis)throws Exception
{
while(true)
{
c=0;
s1="";
System.out.println("------------------------------------------------------------------------");
System.out.print("Enter columns Sequence between 1 to "+n+" : ");
st=dis.readLine();
d=st+d;
System.out.println();
for(int i=0;i<n;i++)
{
c=(int)st.charAt(i)-49;
for(int j=0;j<m;j++)
{
if(a[j][c]!='$')
s1=s1+a[j][c];
}
}
s1.trim();
c=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(c<s1.length())
a[i][j]=s1.charAt(c++);
else
a[i][j]='$';
dis();
System.out.println();
System.out.println();
System.out.print("Do You want to continue(yes(1)/no(0)) : ");
choice=Integer.parseInt(dis.readLine());
if(choice==0)
{
System.out.println("****************************************************************");
System.out.println();
System.out.println("Ecryption results in the ciphertext : "+s1);
System.out.println();
//System.exit(0);
return;
}
}
}
public static void dec()
{
k=0;
p=s1.length()/n;
q=s1.length()%n;
//System.out.println("p = "+p+" q = "+q+" d = "+d);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i][j]='$';
for(int i=0;i<d.length();i++)
{
c=(int)d.charAt(i)-49;
//System.out.println("c = "+c);
if(c>=q)
{
for(int j=0;j<p;j++)
{
a[j][c]=s1.charAt(k++);
}
}
else
{
for(int j=0;j<p+1;j++)
{
a[j][c]=s1.charAt(k++);
}
}
dis();
if(k==s1.length())
{
s1="";
k=0;
for(int x=0;x<m;x++)
for(int j=0;j<n;j++)
if(a[x][j]!='$')
{
s1=s1+a[x][j];
a[x][j]='$';
}
}
}
System.out.println("Decryption results in the plaintext : "+s1);
}
public static void main(String[] args)
{
try
{
DataInputStream dis=new DataInputStream(System.in);
System.out.print("Enter Plain text : ");
s1=dis.readLine();
s=new StringBuffer(s1);
//REMOVING WIDE-SPACES
for(int i=0;i<s.length();i++)
if(s.charAt(i)==' ')
s.deleteCharAt(i);
s1=new String(s);
d="";
System.out.println("Enter size of the array ");
System.out.print("Enter no of rows = ");
m=Integer.parseInt(dis.readLine());
System.out.print("Enter no of columns = ");
n=Integer.parseInt(dis.readLine());
a=new char[m][n];
c=0;
//ENTERING IN THE ARRAY
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(c<s1.length())
a[i][j]=s1.charAt(c++);
else
a[i][j]='$';
dis();
System.out.println("------------------------------------------------------------------------");
enc(dis);
System.out.println("-------------------------------Decryption-------------------------------");
dec();
}
catch(Exception e)
{}
}
}
/*
Enter Plain text : computer Engg.
Enter size of the array
Enter no of rows = 4
Enter no of columns = 5
Matrix :
c o m p u
t e r E n
g g .
------------------------------------------------------------------------
------------------------------------------------------------------------
Enter columns Sequence between 1 to 5 : 45213
Matrix :
p E u n o
e g c t g
m r .
Do You want to continue(yes(1)/no(0)) : 1
------------------------------------------------------------------------
Enter columns Sequence between 1 to 5 : 23541
Matrix :
E g r u c
. o g n t
p e m
Do You want to continue(yes(1)/no(0)) : 0
****************************************************************
Ecryption results in the ciphertext : Egruc.ogntpem
-------------------------------Decryption-------------------------------
Matrix :
E
g
r
Matrix :
E u
g c
r .
Matrix :
E u o
g c g
r .
Matrix :
E u n o
g c t g
r .
Matrix :
p E u n o
e g c t g
m r .
Matrix :
p
E
Matrix :
p u
E n
Matrix :
o p u
e E n
g
Matrix :
c o p u
t e E n
g g
Matrix :
c o m p u
t e r E n
g g .
Decryption results in the plaintext : computerEngg.
*/
Labels:
Encrption and Decryption
Subscribe to:
Posts (Atom)