0% found this document useful (0 votes)
24 views6 pages

NS Exp 1

This document contains the code for a cipher encryption and decryption program in Java. The program allows the user to choose between encryption, decryption, brute force decryption using different keys, and frequency analysis decryption. For each option selected, the program prompts the user for inputs like plain text, cipher text and keys as needed and performs the chosen operation, displaying the results.

Uploaded by

mahatce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

NS Exp 1

This document contains the code for a cipher encryption and decryption program in Java. The program allows the user to choose between encryption, decryption, brute force decryption using different keys, and frequency analysis decryption. For each option selected, the program prompts the user for inputs like plain text, cipher text and keys as needed and performs the chosen operation, displaying the results.

Uploaded by

mahatce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

13LIT11

Coding:
Cipher.java
import java.io.*;
class cipher
{
public static void main(String ar[])throws Exception
{
int c;
int co[]=new int[26];
char a[]={'a','b','c','d','e','f','g','h','i','o','p','q','r','s','t','u','v','w','x','y','z'};
DataInputStream in=new DataInputStream(System.in);
for(int i=0;i<26;i++)
{
co[i]=0;
}
int key;
System.out.println("Enter Your Key:");
key=Integer.parseInt(in.readLine());
String pt="",en="",de="",de1="",de2="";
int t=0;
while(t!=3)
{
System.out.println("\nEnter Your choice :");
System.out.println("\n 1.Encryption");
System.out.println("\n 2.Decryption");
System.out.println(" \n 3.Brute force algorithm");
System.out.println(" \n 4.Frequency analysis");
System.out.println(" \n 5.Exit\n");
13LIT11

c=Integer.parseInt(in.readLine());
switch(c)
{
case 1:
System.out.println("Enter Plain Text:");
pt=in.readLine();
for(int i=0;i<pt.length();i++)
{
for(int j=0;j<26;j++)
{
if(a[j]==(pt.charAt(i)))
{
int y=(j+key)%26;
en=en+a[y];
}
}
}
System.out.println("The Cipher Text is :"+en);
break;
case 2:
for(int i=0;i<en.length();i++)
{
for(int j=0;j<26;j++)
{
if(a[j]==(en.charAt(i)))
{
int r=j-key;
if(r<0)
13LIT11

{
int z=r+26;
de=de+a[z];
}
else
{
int z=(j-key)%26;
de=de+a[z];
}
}
}
}
System.out.println("The Plain Text:"+de);
break;
case 3:
for(int k=0;k<26;k++)
{
for(int i=0;i<en.length();i++)
{
for(int j=0;j<26;j++)
{
if(a[j]==(en.charAt(i)))
{
int r=j-k;
if(r<0)
{
int z=r+26;
de1=de1+a[z];
13LIT11

}
else
{
int z=(j-k)%26;
de1=de1+a[z];
}
}
}
}
System.out.println("\n");
System.out.print("Key:"+k);
System.out.print("Plain Text:"+de1);
if(de1.equals(pt))
break;
de1="";
}
break;
case 4:
for(int i=0;i<en.length();i++)
{
for(int j=1;j<26;j++)
{
if(a[j]==(en.charAt(i)))
{
co[j]=co[j]+1;
}
}
}
13LIT11

int max,po1=0;
max=co[0];
for(int i=0;i<26;i++)
{
if(co[i]>max)
{
max=co[i];
po1=i;
}
}
char re=a[po1];
System.out.println("Po1"+po1);
System.out.println("Enter Char to be Replace");
String v=in.readLine();
char v1=v.charAt(0);
int po2=0;
for(int j=1;j<26;j++)
{
if(v1==a[j])
{
po2=j;
}
}
System.out.println("Pos2"+po2);
int ke=po1-po2;
System.out.println("Key:"+ke);
for(int i=0;i<en.length();i++)
{
13LIT11

for(int j=0;j<26;j++)
{
if(a[j]==(en.charAt(i)))
{
int r1=j-ke;
if(r1<0)
{
int z1=r1+26;
de2=de2+a[z1];
}
else
{
int z1=(j-ke)%26;
de2=de2+a[z1];
}
}
}
}
System.out.println("Plain Text:"+de2);
break;
case 5:
t=3;
break;
}
}
}
}

You might also like