Skip to main content
Home
Live to Learn!

Main navigation

  • Home
  • Learn
    • FoxPro
    • MS-DOS
    • C PRG
    • Java
    • ASP
    • Ruby on Rails
    • ASP.NET
    • E-Books
    • Exam Preparation
    • Tools
  • Blog
  • Forums
  • Contact
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Learn by Example - Java Sample Programs

Poly-alphabetic Cipher Method Encryption - Java

Book navigation

  • Armstrong Number
  • Palindrome Checking
  • Playing Audio Clip using Applet
  • Applet Form
  • Exception Handling
  • Multiple Inheritance and Packages
  • Shopping List
  • Simple Multithread program
  • JDBC Program Select, Insert, Update, Delete records
  • RMI - Example AddServer
  • Simple Bank Account Process
  • TCP Server and Client in Java
  • UDP Server and Client in Java
  • FTP Server and Client in Java
  • Chat Server and Client in Java
  • Echo Server and Client in Java
  • Address Resolution Protocol in Java
  • Ping server and Client in Java
  • Multicast Server and Client in Java
  • Transposition Cipher Method
  • Poly-alphabetic Cipher Method Encryption - Java
  • DES - Using Data Encryption Standard in Java
  • AES - Using Advanced Encryption Standard in Java
  • Bit Stuffing
By Karthikeyan , 14 November, 2012

Java program that demonstrates Poly-alphabetic Cipher Method of Encryption Technique

Program : OneTimePad.java


import java.io.*;
import java.util.*;
public class OneTimePad 
{
 char vignereTable[][]= new char[26][26];
public void GenereatePad() {

char alpharray[] = new char[26];
char c = 'a';
for(int x=0;x<26;x++){
 alpharray[x] = c;
 c++;
 }

int i,j,k;
i = 0;
while(i < 26)
{
    k = i;
    for (j=0;j < 26;j++)
    {
        if (k >= 26)
            k = 0;
        vignereTable[i][j] = alpharray[k++]; 
    }
    i++;
}
           
} 

private String key;
public OneTimePad(String k)
{
    key = k;
}
public String encrypt(String plainText)
{
    char[] plainTextArr = plainText.toCharArray();
    while(key.length() < plainText.length())
    {
        key += key;
    }
    key = key.substring(0,plainText.length());
    System.out.println(key);
    char [] keyArray = key.toCharArray();
    String cipherText = "";
    for(int i=0; i < plainText.length();i++)
    {
        int rowpos = keyArray[i]-'a';
        int colpos = plainTextArr[i]-'a';
        cipherText += vignereTable[rowpos][colpos];
    }
    return cipherText;
    
}
public String decrypt(String cipherText)
{
    String plainText = "";
    char[] cipherTextArr = cipherText.toCharArray();
    char [] keyArray = key.toCharArray();
    char [] plainTextArr = new char[keyArray.length];
    for(int i=0; i < keyArray.length; i++)
    {
        int rowpos = keyArray[i] - 'a';
        int cipherpos = new String(vignereTable[rowpos]).indexOf(cipherTextArr[i]);
        plainTextArr[i] = vignereTable[0][cipherpos];
    }
    plainText = new String(plainTextArr);
    return plainText;
}

public static void main(String args[])
{
    Scanner console = new Scanner(System.in);
    System.out.println("Enter a text key in lower case");
    String keyText = console.nextLine();
    OneTimePad algo = new OneTimePad(keyText);
    algo.GenereatePad();
    System.out.println("Enter the plain text");
    String plainText = console.nextLine();
    String cipherText = algo.encrypt(plainText);
    System.out.println("The encrypted text is "+cipherText);
    plainText = algo.decrypt(cipherText);
    System.out.println("The decrypted text is "+plainText);
}
      
}

Output

Sample output

Enter a text key in lower case
olympic
Enter the plain text
hellowolrd
olympicoly
The encrypted text is vpjxdeqzcb
The decrypted text is hellowolrd
  • Add new comment

Comments

Featured Blog Posts

Convert Currency in Number to Words (Indian Rupees) - MS Excel
Foxpro Tutorial and Programs
Convert Currency in Number to Words in MS Word
Convert Currency in Number to Words (Indian Rupees) - Version 2
Best way to Use Rupee Symbol in Windows – Easy steps
Convert Currency in Number to Words - MS Access
Creating All in One Windows XP DVD with all Important Applications
RSS feed

© 2009-2025 Live to Learn.In

Terms of Use | Privacy Policy