The Poly-Alphabetic cipher is also an example of substitution cipher which uses multiple one-character keys.

In the program we are implementing Mono-Alphabetic cipher which is an example of substitution cipher. Program consists of two methods encryption and decryption. The encryption method has two parameter one the plain text and second is key. To facilitate the encryption process all the alphabets are usually written out in a large table known as Vigenere tableau. The size of tableau is 26×26 so that 26 full cipher text alphabets are available. Each key encrypts one plain text character. The decryption method also has two parameters one encrypted message and key. It does opposite process of encryption.

Program

importjavax.swing.JOptionPane;

public class PolyAlphabetic {

public static void main(String[] args)

{

// TODO code application logic here

String plain_text,cipher1,cipher2;

plain_text=JOptionPane.showInputDialog(“Input the string to encrypt:”);

String k=JOptionPane.showInputDialog(“Enter the key:”);

cipher1=encryption(plain_text,k);

cipher2=decryption(cipher1,k);

JOptionPane.showMessageDialog(null, cipher1);

JOptionPane.showMessageDialog(null, cipher2);

}

public static String encryption(String p1,String k1)

{

String result=””;

intoffset,j=0,shift;

for(int i=0;i<p1.length();i++)

{

if(p1.charAt(i)>=’a’ && p1.charAt(i)<=’z’)

{

shift=((int)k1.charAt(j))-97;

j++;

j%=k1.length();

offset=((int)p1.charAt(i))-97;

offset=(offset+shift)%26;

result+=(char)(offset+97);

}

else if(p1.charAt(i)>=’A’ && p1.charAt(i)<=’Z’)

{

shift=((int)k1.charAt(j))-65;

j++;

j%=k1.length();

offset=((int)p1.charAt(i))-65;

offset=(offset+shift)%26;

result+=(char)(offset+65);

}

else

{

result=result+p1.charAt(i);

}

}

return result;

 

}

public static String decryption(String p1,String k1)

{

String result=””;

intoffset,j=0,shift;

for(int i=0;i<p1.length();i++)

{

if(p1.charAt(i)!=’ ‘)

{

shift=((int)k1.charAt(j))-97;

j++;

j%=k1.length();

offset=((int)p1.charAt(i))-97;

offset=(offset-shift)%26;

if(offset<=0)

{

offset+=26;

}

result+=(char)(offset+97);

}

else if(p1.charAt(i)>=’A’ && p1.charAt(i)<=’Z’)

{

shift=((int)k1.charAt(j))-65;

j++;

j%=k1.length();

offset=((int)p1.charAt(i))-65;

offset=(offset-shift)%26;

if(offset<=0)

{

offset+=26;

}

result+=(char)(offset+65);

}

else

{

result=result+p1.charAt(i);

}

}

return result;

}

}

Output:

Pract 1-4

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

©2023 MYBSCIT.com. An initiative by some failures to make student scholars.

or

Log in with your credentials

Forgot your details?