Program No. # 1
//To find whether a number is prime or not
import java.io.*;
class prime1
{
public static void main(String args[]) throws IOException
{
int i;
System.out.print("Enter a number: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num=Integer.parseInt(br.readLine());
int flag=0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
flag=1;
System.out.println("The number is not prime");
break;
}
}
if(flag==0)
System.out.print("The number is prime");
System.out.println("");
}
}
Program No. # 2
//To find prime numbers between 1 to 100
class prime2
{
public static void main(String args[])
{
int i,j;
System.out.println("The prime numbers between 1 to 100 are: ");
for(i=2;i<100;i++)
{
int flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
System.out.print(i+" ");
}
System.out.println("");
}
}
Program No. # 3
//To find factors of a number
import java.io.*;
public class factor
{
public static void main(String[] args) throws IOException
{
System.out.print("Enter the number: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
long n = Long.parseLong(br.readLine());
System.out.print("The prime factorization of " + n + " is: ");
// for each potential factor i
for (long i = 2; i <= n / i; i++)
{
// if i is a factor of N, repeatedly divide it out
while (n % i == 0)
{
System.out.print(i + " , ");
n = n / i;
}
}
// if biggest factor occurs only once, n > 1
if (n > 1) System.out.println(n);
else System.out.println();
}
}
Program No. # 4
//To find gcd of two numbers
import java.io.*;
class gcd1
{
public int gcd(int x, int y)
{
int g=0;
if(x<0)
x=-x;
if(y<0)
y=-y;
while(x>0)
{
g=x;
x=y%x;
y=g;
}
return(g);
}
public static void main(String args[]) throws IOException
{
int i;
System.out.print("Enter first number: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num1=Integer.parseInt(br.readLine());
System.out.print("Enter first number: ");
int num2=Integer.parseInt(br.readLine());
gcd1 a=new gcd1();
int g1=a.gcd(num1,num2);
System.out.println("The GCD of "+num1+" & "+num2+" is "+g1);
}
}
Program No. # 5
//To find gcd of two numbers using recursion
import java.io.*;
class gcd2
{
public int gcd(int x, int y)
{
int g,f;
if(x<0)
x=-x;
if(y<0)
y=-y;
g=x;
f=y%x;
if(f>0)
return gcd(f,g); //recursive call of gcd method
return(g);
}
public static void main(String args[]) throws IOException
{
int i;
System.out.print("Enter first number: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num1=Integer.parseInt(br.readLine());
System.out.print("Enter first number: ");
int num2=Integer.parseInt(br.readLine());
gcd2 a=new gcd2();
int g1=a.gcd(num1,num2);
System.out.println("The GCD of "+num1+" & "+num2+" is "+g1);
}
}
Program No. # 6
//Caesar Cipher of Strings without key
class caesar1
{
public static void main(String args[])
{
String a="abcdefghijklmnopqrstuvwxyz";
String pt="hello";
System.out.println("The plain text is: "+pt.toUpperCase());
int len=pt.length();
System.out.print("The corresponding cipher text is: ");
for(int i=0;i<len;i++)
{
char s1= pt.charAt(i);
int j=a.indexOf(s1);
String ct=a.substring(j+3,j+4);
System.out.print(ct.toUpperCase());
}
System.out.println(" ");
}
}
Program No. # 7
//Caesar Cipher of Strings with key
import java.io.*;
class caesar2
{
public static void main(String args[]) throws IOException
{
String a="abcdefghijklmnopqrstuvwxyz";
String pt="saurabh";
System.out.println("The plain text is: "+pt.toUpperCase()); //plain text
System.out.print("Enter the key: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int key=Integer.parseInt(br.readLine());
int len=pt.length();
System.out.print("The cipher text is: ");
for(int i=0;i<len;i++)
{
char s1= pt.charAt(i);
//System.out.println(s1);
int j=a.indexOf(s1);
int m=(j+key)%26;
String ct=a.substring(m,m+1);
System.out.print(ct.toUpperCase());
}
System.out.println("");
}
}
Program No. # 8
//Columnar Transposition without key
class TransCol
{
public static void main(String a[]) throws ArrayIndexOutOfBoundsException
{
int k=0;
String pt="mynameissarkar";
System.out.println("Plain text: MY NAME IS SARKAR");
String[][] pt1=new String[5][5];
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(k>=pt.length())
{
break;
}
pt1[i][j]=pt.substring(k,k+1);
k++;
}
System.out.println("");
}
//Matrix representation
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(pt1[i][j]!=null)
{
System.out.print(pt1[i][j]+" ");
}
else
{
System.out.print("X"+" ");
}
}
System.out.println("");
System.out.println("");
}
//Cipher text
System.out.print("The cipher text is: ");
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(pt1[j][i]!=null)
{
System.out.print(pt1[j][i].toUpperCase());
}
else
{
System.out.print("X");
}
}
}
System.out.println(" ");
}
}
Program No. # 9
//Columnar Transposition with key
class TransColKey
{
public static void main(String a[]) throws ArrayIndexOutOfBoundsException
{
int k=0;
int[] key={3,1,0,4,2};
String pt="mynameissarkar";
System.out.print("Plain text: MY NAME IS SARKAR");
String[][] pt1=new String[5][5];
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(k>=pt.length())
{
break;
}
pt1[i][j]=pt.substring(k,k+1);
k++;
}
System.out.println("");
}
//Key Used
System.out.print("The Key used is: [ ");
for(int i=0;i<5;i++)
System.out.print(key[i]+" ");
System.out.println("]");
//Matrix representation
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(pt1[i][j]!=null)
{
System.out.print(pt1[i][j]+" ");
}
else
{
System.out.print("X"+" ");
}
}
System.out.println("");
System.out.println("");
}
//Cipher text
System.out.print("The cipher text is: ");
for(int i=0;i<5;i++)
{
int m=key[i];
for(int j=0;j<5;j++)
{
if(pt1[j][m]!=null)
{
System.out.print(pt1[j][m].toUpperCase());
}
else
{
System.out.print("X");
}
}
}
System.out.println(" ");
}
}
Program No. # 10
//PlayFair with key
import java.io.*;
class PlayFair
{
public static void main(String a[]) throws ArrayIndexOutOfBoundsException,IOException
{
String[][] pt1=new String[5][5];
String all="ABCDEFGHIKLMNOPQRSTUVWXYZ";
String key="MONARCHY";
System.out.print("Enter the plain text: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String pt=br.readLine();
pt=pt.toUpperCase();
if((pt.length()%2!=0))
pt=pt+"X";
System.out.println("The plain text is: "+pt);
String ct="";
int len2,k=0;
int len1=key.length();
for(int i=0;i<len1;i++)
{
len2=all.length();
String s=key.substring(i,i+1);
if(s.equals("J"))
{
s="I";
}
int n=all.indexOf(s);
all=all.substring(0,n)+""+all.substring(n+1,len2);
}
all=key+""+all;
//Preparing the playfair matrix
System.out.println("The playfair matrix is:");
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(k>=all.length())
{
break;
}
pt1[i][j]=all.substring(k,k+1);
k++;
}
System.out.println("");
}
//Array representation
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
System.out.print(pt1[i][j]+" ");
}
System.out.println("");
}
for(k=0;k<pt.length();k=k+2)
{
int i,j,a1=0,b=0,c=0,d=0;
String s=pt.substring(k,k+2);
String s1=s.substring(0,1);
String s2=s.substring(1,2);
/*if(s1.equals(s2))
{
if(pt.length()%2==0 && pt.charAt(pt.length()-1)=='X')
pt=pt.substring(0,pt.length()-1);
pt=pt.substring(0,k+1)+"X"+pt.substring(k+1,pt.length()-1);
continue;
}
*/
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(pt1[i][j].equals(s1))
{
a1=i;
b=j;
break;
}
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(pt1[i][j].equals(s2))
{
c=i;
d=j;
break;
}
}
}
if(a1==c)
{
if((b+1)>=5)
b=0;
if((d+1)>=5)
d=0;
s1=pt1[a1][b+1];
s2=pt1[a1][d+1];
}
if(b==d)
{
if((a1+1)>=5)
a1=0;
if((c+1)>=5)
c=0;
s1=pt1[a1+1][b];
s2=pt1[c+1][b];
}
if(a1!=c && b!=d)
{
s1=pt1[a1][d];
s2=pt1[c][b];
}
ct=ct+""+s1+""+s2;
}
System.out.println("The cipher text is: "+ct);
}
}
Program No. # 11
//RSA Public key encryption
import java.io.*;
class check
{
// method to check whether a number is prime or not
void prime(int p,int q)
{
for(int i=2;i<=p/2;i++)
{
if(p%i==0)
{
System.out.println("p is not prime");
System.exit(0);
}
}
for(int i=2;i<=q/2;i++)
{
if(q%i==0)
{
System.out.println("q is not prime");
System.exit(0);
}
}
}
// method to find GCD of two numbers
public int gcd(int x, int y)
{
int g,f;
if(x<0)
x=-x;
if(y<0)
y=-y;
g=x;
f=y%x;
if(f>0)
return gcd(f,g); //recursive call of gcd method
return(g);
}
}
class RSA
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter P: ");
int p=Integer.parseInt(br.readLine());
//System.out.println();
System.out.print("Enter Q: ");
int q=Integer.parseInt(br.readLine());
//check p,q are prime or not
check ob=new check();
ob.prime(p,q);
int n=p*q;
int z=(p-1)*(q-1);
//System.out.println();
System.out.print("Enter Encrytion key(e): ");
int e=Integer.parseInt(br.readLine());
//check whether e and z are relatively prime
int g=ob.gcd(e,z);
if(g!=1)
{
System.out.println("e and z are not relatively prime");
System.exit(0);
}
int d=1;
while(((e*d)%z)!=1)
d++;
System.out.print("Enter the plain text: ");
int m=Integer.parseInt(br.readLine());
int c=(m^e)%n;
System.out.println("The decryption key is(d) : "+d);
System.out.println("The cipher text is : "+c);
}
}
Program No. # 12
//Blum Blum Shub Generator
import java.io.*;
class check
{
// method to check whether a number is prime or not
void prime(int p,int q)
{
for(int i=2;i<=p/2;i++)
{
if(p%i==0)
{
System.out.println("p is not prime");
System.exit(0);
}
}
for(int i=2;i<=q/2;i++)
{
if(q%i==0)
{
System.out.println("q is not prime");
System.exit(0);
}
}
}
// method to find GCD of two numbers
public int gcd(int x, int y)
{
int g,f;
if(x<0)
x=-x;
if(y<0)
y=-y;
g=x;
f=y%x;
if(f>0)
return gcd(f,g); //recursive call of gcd method
return(g);
}
}
class BBS
{
public static void main(String args[]) throws IOException
{
int i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter prime no. P congruent to 3mod4: ");
int p=Integer.parseInt(br.readLine());
//System.out.println();
System.out.print("Enter prime no. Q congruent to 3mod4: ");
int q=Integer.parseInt(br.readLine());
//check p,q are prime or not
check ob=new check();
ob.prime(p,q);
//check whether p,q are congruent to 3mod4 or not
if(p%4!=3)
{
System.out.println("P is not congruent to 3mod4");
System.exit(0);
}
if(q%4!=3)
{
System.out.println("Q is not congruent to 3mod4");
System.exit(0);
}
int n=p*q;
//System.out.println();
System.out.print("Enter a random vaue s: ");
int s=Integer.parseInt(br.readLine());
//check whether s and n are relatively prime
int g=ob.gcd(s,n);
if(g!=1)
{
System.out.println("s and n are not relatively prime");
System.exit(0);
}
int[] x=new int[100];
int[] B=new int[100];
x[0]=(s*s)%n;
System.out.println("");
System.out.print("i"+" "+"Xi"+" "+"Bi");
System.out.println("");
System.out.println("");
for(i=1;i<=20;i++)
{
x[i]=(x[i-1]*x[i-1])%n;
B[i]=x[i]%2;
System.out.print(i+" "+x[i]+" "+B[i]);
System.out.println("");
}
}
}
Program No. # 13
//Linear Congruential Generators
import java.io.*;
class LCG
{
public static void main(String args[]) throws IOException
{
int i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the modulus(m): ");
int m=Integer.parseInt(br.readLine());
System.out.print("Enter the multiplier(a): ");
int a=Integer.parseInt(br.readLine());
System.out.print("Enter the increment value(c): ");
int c=Integer.parseInt(br.readLine());
int[] x=new int[100];
System.out.print("Enter the seed value(Xo): ");
x[0]=Integer.parseInt(br.readLine());
System.out.print("Enter how many random numbers to generate(n): ");
int n=Integer.parseInt(br.readLine());
System.out.println("");
System.out.print("The Sequence generated is:- ");
//Iterative function to generate random numbers
for(i=1;i<=n;i++)
{
x[i]=(a*x[i-1]+c)%m;
System.out.print(x[i]+" , ");
}
System.out.println("");
System.out.println("");
}
}
Program No. # 14
//Middle Square Method to generate random numbers
import java.io.*;
class midsq
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a four digit number(r): ");
int r= Integer.parseInt(br.readLine());
int[] x=new int[100];
String[] s=new String[100];
System.out.print("Enter how many random numbers to generate(n):");
int n= Integer.parseInt(br.readLine());
int i,j,k;
x[0]=r;
System.out.println("The random numbers thus generated are:-");
for(i=1;i<=n;i++)
{
x[i]=x[i-1]*x[i-1];
s[i]=Integer.toString(x[i]); //the number is converted to string
int len=s[i].length();
//Padding 0 in front of a number if its length is less than 8
if(len<8)
{
j=8-len;
for(k=0;k<j;k++)
{
s[i]="0"+s[i];
}
}
//rejecting first 2 & last 2 digits of the number
s[i]=s[i].substring(2,6);
System.out.print(i+". "+s[i]);
System.out.println("");
x[i]=Integer.parseInt(s[i]);
}
}
}
//To find whether a number is prime or not
import java.io.*;
class prime1
{
public static void main(String args[]) throws IOException
{
int i;
System.out.print("Enter a number: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num=Integer.parseInt(br.readLine());
int flag=0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
flag=1;
System.out.println("The number is not prime");
break;
}
}
if(flag==0)
System.out.print("The number is prime");
System.out.println("");
}
}
Program No. # 2
//To find prime numbers between 1 to 100
class prime2
{
public static void main(String args[])
{
int i,j;
System.out.println("The prime numbers between 1 to 100 are: ");
for(i=2;i<100;i++)
{
int flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
System.out.print(i+" ");
}
System.out.println("");
}
}
Program No. # 3
//To find factors of a number
import java.io.*;
public class factor
{
public static void main(String[] args) throws IOException
{
System.out.print("Enter the number: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
long n = Long.parseLong(br.readLine());
System.out.print("The prime factorization of " + n + " is: ");
// for each potential factor i
for (long i = 2; i <= n / i; i++)
{
// if i is a factor of N, repeatedly divide it out
while (n % i == 0)
{
System.out.print(i + " , ");
n = n / i;
}
}
// if biggest factor occurs only once, n > 1
if (n > 1) System.out.println(n);
else System.out.println();
}
}
Program No. # 4
//To find gcd of two numbers
import java.io.*;
class gcd1
{
public int gcd(int x, int y)
{
int g=0;
if(x<0)
x=-x;
if(y<0)
y=-y;
while(x>0)
{
g=x;
x=y%x;
y=g;
}
return(g);
}
public static void main(String args[]) throws IOException
{
int i;
System.out.print("Enter first number: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num1=Integer.parseInt(br.readLine());
System.out.print("Enter first number: ");
int num2=Integer.parseInt(br.readLine());
gcd1 a=new gcd1();
int g1=a.gcd(num1,num2);
System.out.println("The GCD of "+num1+" & "+num2+" is "+g1);
}
}
Program No. # 5
//To find gcd of two numbers using recursion
import java.io.*;
class gcd2
{
public int gcd(int x, int y)
{
int g,f;
if(x<0)
x=-x;
if(y<0)
y=-y;
g=x;
f=y%x;
if(f>0)
return gcd(f,g); //recursive call of gcd method
return(g);
}
public static void main(String args[]) throws IOException
{
int i;
System.out.print("Enter first number: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num1=Integer.parseInt(br.readLine());
System.out.print("Enter first number: ");
int num2=Integer.parseInt(br.readLine());
gcd2 a=new gcd2();
int g1=a.gcd(num1,num2);
System.out.println("The GCD of "+num1+" & "+num2+" is "+g1);
}
}
Program No. # 6
//Caesar Cipher of Strings without key
class caesar1
{
public static void main(String args[])
{
String a="abcdefghijklmnopqrstuvwxyz";
String pt="hello";
System.out.println("The plain text is: "+pt.toUpperCase());
int len=pt.length();
System.out.print("The corresponding cipher text is: ");
for(int i=0;i<len;i++)
{
char s1= pt.charAt(i);
int j=a.indexOf(s1);
String ct=a.substring(j+3,j+4);
System.out.print(ct.toUpperCase());
}
System.out.println(" ");
}
}
Program No. # 7
//Caesar Cipher of Strings with key
import java.io.*;
class caesar2
{
public static void main(String args[]) throws IOException
{
String a="abcdefghijklmnopqrstuvwxyz";
String pt="saurabh";
System.out.println("The plain text is: "+pt.toUpperCase()); //plain text
System.out.print("Enter the key: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int key=Integer.parseInt(br.readLine());
int len=pt.length();
System.out.print("The cipher text is: ");
for(int i=0;i<len;i++)
{
char s1= pt.charAt(i);
//System.out.println(s1);
int j=a.indexOf(s1);
int m=(j+key)%26;
String ct=a.substring(m,m+1);
System.out.print(ct.toUpperCase());
}
System.out.println("");
}
}
Program No. # 8
//Columnar Transposition without key
class TransCol
{
public static void main(String a[]) throws ArrayIndexOutOfBoundsException
{
int k=0;
String pt="mynameissarkar";
System.out.println("Plain text: MY NAME IS SARKAR");
String[][] pt1=new String[5][5];
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(k>=pt.length())
{
break;
}
pt1[i][j]=pt.substring(k,k+1);
k++;
}
System.out.println("");
}
//Matrix representation
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(pt1[i][j]!=null)
{
System.out.print(pt1[i][j]+" ");
}
else
{
System.out.print("X"+" ");
}
}
System.out.println("");
System.out.println("");
}
//Cipher text
System.out.print("The cipher text is: ");
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(pt1[j][i]!=null)
{
System.out.print(pt1[j][i].toUpperCase());
}
else
{
System.out.print("X");
}
}
}
System.out.println(" ");
}
}
Program No. # 9
//Columnar Transposition with key
class TransColKey
{
public static void main(String a[]) throws ArrayIndexOutOfBoundsException
{
int k=0;
int[] key={3,1,0,4,2};
String pt="mynameissarkar";
System.out.print("Plain text: MY NAME IS SARKAR");
String[][] pt1=new String[5][5];
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(k>=pt.length())
{
break;
}
pt1[i][j]=pt.substring(k,k+1);
k++;
}
System.out.println("");
}
//Key Used
System.out.print("The Key used is: [ ");
for(int i=0;i<5;i++)
System.out.print(key[i]+" ");
System.out.println("]");
//Matrix representation
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(pt1[i][j]!=null)
{
System.out.print(pt1[i][j]+" ");
}
else
{
System.out.print("X"+" ");
}
}
System.out.println("");
System.out.println("");
}
//Cipher text
System.out.print("The cipher text is: ");
for(int i=0;i<5;i++)
{
int m=key[i];
for(int j=0;j<5;j++)
{
if(pt1[j][m]!=null)
{
System.out.print(pt1[j][m].toUpperCase());
}
else
{
System.out.print("X");
}
}
}
System.out.println(" ");
}
}
Program No. # 10
//PlayFair with key
import java.io.*;
class PlayFair
{
public static void main(String a[]) throws ArrayIndexOutOfBoundsException,IOException
{
String[][] pt1=new String[5][5];
String all="ABCDEFGHIKLMNOPQRSTUVWXYZ";
String key="MONARCHY";
System.out.print("Enter the plain text: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String pt=br.readLine();
pt=pt.toUpperCase();
if((pt.length()%2!=0))
pt=pt+"X";
System.out.println("The plain text is: "+pt);
String ct="";
int len2,k=0;
int len1=key.length();
for(int i=0;i<len1;i++)
{
len2=all.length();
String s=key.substring(i,i+1);
if(s.equals("J"))
{
s="I";
}
int n=all.indexOf(s);
all=all.substring(0,n)+""+all.substring(n+1,len2);
}
all=key+""+all;
//Preparing the playfair matrix
System.out.println("The playfair matrix is:");
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(k>=all.length())
{
break;
}
pt1[i][j]=all.substring(k,k+1);
k++;
}
System.out.println("");
}
//Array representation
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
System.out.print(pt1[i][j]+" ");
}
System.out.println("");
}
for(k=0;k<pt.length();k=k+2)
{
int i,j,a1=0,b=0,c=0,d=0;
String s=pt.substring(k,k+2);
String s1=s.substring(0,1);
String s2=s.substring(1,2);
/*if(s1.equals(s2))
{
if(pt.length()%2==0 && pt.charAt(pt.length()-1)=='X')
pt=pt.substring(0,pt.length()-1);
pt=pt.substring(0,k+1)+"X"+pt.substring(k+1,pt.length()-1);
continue;
}
*/
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(pt1[i][j].equals(s1))
{
a1=i;
b=j;
break;
}
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(pt1[i][j].equals(s2))
{
c=i;
d=j;
break;
}
}
}
if(a1==c)
{
if((b+1)>=5)
b=0;
if((d+1)>=5)
d=0;
s1=pt1[a1][b+1];
s2=pt1[a1][d+1];
}
if(b==d)
{
if((a1+1)>=5)
a1=0;
if((c+1)>=5)
c=0;
s1=pt1[a1+1][b];
s2=pt1[c+1][b];
}
if(a1!=c && b!=d)
{
s1=pt1[a1][d];
s2=pt1[c][b];
}
ct=ct+""+s1+""+s2;
}
System.out.println("The cipher text is: "+ct);
}
}
Program No. # 11
//RSA Public key encryption
import java.io.*;
class check
{
// method to check whether a number is prime or not
void prime(int p,int q)
{
for(int i=2;i<=p/2;i++)
{
if(p%i==0)
{
System.out.println("p is not prime");
System.exit(0);
}
}
for(int i=2;i<=q/2;i++)
{
if(q%i==0)
{
System.out.println("q is not prime");
System.exit(0);
}
}
}
// method to find GCD of two numbers
public int gcd(int x, int y)
{
int g,f;
if(x<0)
x=-x;
if(y<0)
y=-y;
g=x;
f=y%x;
if(f>0)
return gcd(f,g); //recursive call of gcd method
return(g);
}
}
class RSA
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter P: ");
int p=Integer.parseInt(br.readLine());
//System.out.println();
System.out.print("Enter Q: ");
int q=Integer.parseInt(br.readLine());
//check p,q are prime or not
check ob=new check();
ob.prime(p,q);
int n=p*q;
int z=(p-1)*(q-1);
//System.out.println();
System.out.print("Enter Encrytion key(e): ");
int e=Integer.parseInt(br.readLine());
//check whether e and z are relatively prime
int g=ob.gcd(e,z);
if(g!=1)
{
System.out.println("e and z are not relatively prime");
System.exit(0);
}
int d=1;
while(((e*d)%z)!=1)
d++;
System.out.print("Enter the plain text: ");
int m=Integer.parseInt(br.readLine());
int c=(m^e)%n;
System.out.println("The decryption key is(d) : "+d);
System.out.println("The cipher text is : "+c);
}
}
Program No. # 12
//Blum Blum Shub Generator
import java.io.*;
class check
{
// method to check whether a number is prime or not
void prime(int p,int q)
{
for(int i=2;i<=p/2;i++)
{
if(p%i==0)
{
System.out.println("p is not prime");
System.exit(0);
}
}
for(int i=2;i<=q/2;i++)
{
if(q%i==0)
{
System.out.println("q is not prime");
System.exit(0);
}
}
}
// method to find GCD of two numbers
public int gcd(int x, int y)
{
int g,f;
if(x<0)
x=-x;
if(y<0)
y=-y;
g=x;
f=y%x;
if(f>0)
return gcd(f,g); //recursive call of gcd method
return(g);
}
}
class BBS
{
public static void main(String args[]) throws IOException
{
int i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter prime no. P congruent to 3mod4: ");
int p=Integer.parseInt(br.readLine());
//System.out.println();
System.out.print("Enter prime no. Q congruent to 3mod4: ");
int q=Integer.parseInt(br.readLine());
//check p,q are prime or not
check ob=new check();
ob.prime(p,q);
//check whether p,q are congruent to 3mod4 or not
if(p%4!=3)
{
System.out.println("P is not congruent to 3mod4");
System.exit(0);
}
if(q%4!=3)
{
System.out.println("Q is not congruent to 3mod4");
System.exit(0);
}
int n=p*q;
//System.out.println();
System.out.print("Enter a random vaue s: ");
int s=Integer.parseInt(br.readLine());
//check whether s and n are relatively prime
int g=ob.gcd(s,n);
if(g!=1)
{
System.out.println("s and n are not relatively prime");
System.exit(0);
}
int[] x=new int[100];
int[] B=new int[100];
x[0]=(s*s)%n;
System.out.println("");
System.out.print("i"+" "+"Xi"+" "+"Bi");
System.out.println("");
System.out.println("");
for(i=1;i<=20;i++)
{
x[i]=(x[i-1]*x[i-1])%n;
B[i]=x[i]%2;
System.out.print(i+" "+x[i]+" "+B[i]);
System.out.println("");
}
}
}
Program No. # 13
//Linear Congruential Generators
import java.io.*;
class LCG
{
public static void main(String args[]) throws IOException
{
int i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the modulus(m): ");
int m=Integer.parseInt(br.readLine());
System.out.print("Enter the multiplier(a): ");
int a=Integer.parseInt(br.readLine());
System.out.print("Enter the increment value(c): ");
int c=Integer.parseInt(br.readLine());
int[] x=new int[100];
System.out.print("Enter the seed value(Xo): ");
x[0]=Integer.parseInt(br.readLine());
System.out.print("Enter how many random numbers to generate(n): ");
int n=Integer.parseInt(br.readLine());
System.out.println("");
System.out.print("The Sequence generated is:- ");
//Iterative function to generate random numbers
for(i=1;i<=n;i++)
{
x[i]=(a*x[i-1]+c)%m;
System.out.print(x[i]+" , ");
}
System.out.println("");
System.out.println("");
}
}
Program No. # 14
//Middle Square Method to generate random numbers
import java.io.*;
class midsq
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a four digit number(r): ");
int r= Integer.parseInt(br.readLine());
int[] x=new int[100];
String[] s=new String[100];
System.out.print("Enter how many random numbers to generate(n):");
int n= Integer.parseInt(br.readLine());
int i,j,k;
x[0]=r;
System.out.println("The random numbers thus generated are:-");
for(i=1;i<=n;i++)
{
x[i]=x[i-1]*x[i-1];
s[i]=Integer.toString(x[i]); //the number is converted to string
int len=s[i].length();
//Padding 0 in front of a number if its length is less than 8
if(len<8)
{
j=8-len;
for(k=0;k<j;k++)
{
s[i]="0"+s[i];
}
}
//rejecting first 2 & last 2 digits of the number
s[i]=s[i].substring(2,6);
System.out.print(i+". "+s[i]);
System.out.println("");
x[i]=Integer.parseInt(s[i]);
}
}
}
No comments:
Post a Comment