Java Program to Implement the One Time Pad Algorithm

«
»
This is a java program to implement one time pad algorithm. In cryptography, a one-time pad (OTP) is an encryption technique that cannot be cracked if used correctly. In this technique, a plaintext is paired with random, secret key (or pad). Then, each bit or character of the plaintext is encrypted by combining it with the corresponding bit or character from the pad using modular addition. If the key is truly random, and at least as long as the plaintext, and never reused in whole or in part, and kept completely secret, then the resulting ciphertext will be impossible to decrypt or break. It has also been proven that any cipher with the perfect secrecy property must use keys with effectively the same requirements as OTP keys. However, practical problems have prevented one-time pads from being widely used.

Here is the source code of the Java Program to Implement the One Time Pad Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.setandstring;
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class OneTimePadCipher
  7. {
  8.     public static String encryptionMessage(String s)
  9.     {
  10.         int i, j;
  11.         int randomBitPattern[] = new int[8];
  12.         for (i = 0; i < 7; i++)
  13.         {
  14.             randomBitPattern[i] = (i % 2 == 0) ? 1 : 0;
  15.         }
  16.         char asc[] = new char[s.length()];
  17.         for (i = 0; i < s.length(); i++)
  18.         {
  19.             asc[i] = (char) ((int) s.charAt(i));
  20.         }
  21.         BasicOperation b1 = new BasicOperation();
  22.         String cipherText = new String("");
  23.         for (i = 0; i < asc.length; i++)
  24.         {
  25.             int temp = (int) (asc[i]);
  26.             int len = b1.decimalToBinary(temp);
  27.             int bintemp[] = new int[7];
  28.             int xorlen;
  29.             if (len == 7)
  30.             {
  31.                 for (j = 1; j <= len; j++)
  32.                 {
  33.                     bintemp[j - 1] = b1.binaryArrayAtPosition(j);
  34.                 }
  35.                 // XOR Operation
  36.                 xorlen = b1.xorop(bintemp, randomBitPattern, len);
  37.             }
  38.             else
  39.             {
  40.                 // System.out.println("\n less than 7 :"+len);
  41.                 bintemp[0] = 0;
  42.                 for (j = 1; j <= len; j++)
  43.                 {
  44.                     bintemp[j] = b1.binaryArrayAtPosition(j);
  45.                 }
  46.                 // XOR Operation
  47.                 xorlen = b1.xorop(bintemp, randomBitPattern, len + 1);
  48.             }
  49.             int xor[] = new int[xorlen];
  50.             for (j = 0; j < xorlen; j++)
  51.             {
  52.                 xor[j] = b1.xorinArrayAt(j);
  53.                 cipherText = cipherText + xor[j];
  54.             }
  55.             cipherText += " ";
  56.         }
  57.         return (cipherText);
  58.     }
  59.  
  60.     public static String decryptionMessage(String s)
  61.     {
  62.         int i, j;
  63.         // char cipherChar[]=new char[(s.length()/2)];
  64.         char cipherChar[] = new char[(s.length())];
  65.         int cnt = -1;
  66.         for (i = 0; i < s.length(); i++)
  67.         {
  68.             // we receive only Ascii of it is allow 0 and 1, do not accept white
  69.             // space
  70.             // int ascii=(int)s.charAt(i);
  71.             if ((int) s.charAt(i) == 48 || (int) s.charAt(i) == 49
  72.                     || (int) s.charAt(i) == 32)
  73.             {
  74.                 cnt++;
  75.                 cipherChar[cnt] = s.charAt(i);
  76.             }
  77.         }
  78.         String s1 = new String(cipherChar);
  79.         String s2[] = s1.split(" ");
  80.         int data[] = new int[s2.length];
  81.         for (i = 0; i < s2.length; i++)
  82.         {
  83.             data[i] = Integer.parseInt(s2[i]);
  84.         }
  85.         char randomBitPattern[] = new char[7];
  86.         for (i = 0; i < 7; i++)
  87.         {
  88.             randomBitPattern[i] = (i % 2 == 0) ? '1' : '0';
  89.         }
  90.         BasicOperation b1 = new BasicOperation();
  91.         String plain = new String("");
  92.         // do the XOR Operation
  93.         for (i = 0; i < s2.length; i++)
  94.         {
  95.             int xorlen = b1.xorop(s2[i], randomBitPattern);
  96.             int xor[] = new int[xorlen];
  97.             for (j = 0; j < xorlen; j++)
  98.             {
  99.                 xor[j] = b1.xorinArrayAt(j);
  100.                 plain += xor[j];
  101.             }
  102.             plain += " ";
  103.         }
  104.         String p[] = plain.split(" ");
  105.         BasicOperation ob = new BasicOperation();
  106.         int decryptedChar[] = new int[p.length];
  107.         char plainTextChar[] = new char[p.length];
  108.         for (i = 0; i < p.length; i++)
  109.         {
  110.             decryptedChar[i] = ob.binaryToDecimal(Integer.parseInt(p[i]));
  111.             plainTextChar[i] = (char) decryptedChar[i];
  112.         }
  113.         return (new String(plainTextChar));
  114.     }
  115.  
  116.     public static void main(String[] args)
  117.     {
  118.         Scanner sc = new Scanner(System.in);
  119.         System.out.println("Enter the message: ");
  120.         String message = sc.next();
  121.         System.out.println("'" + message + "' in encrypted message : "
  122.                 + encryptionMessage(message));
  123.         System.out.println("'" + encryptionMessage(message)
  124.                 + "' in decrypted message : "
  125.                 + decryptionMessage(encryptionMessage(message)));
  126.         sc.close();
  127.     }
  128. }
  129.  
  130. class BasicOperation
  131. {
  132.     int bin[]   = new int[100];
  133.     int xor[]   = new int[100];
  134.     int temp1[] = new int[100];
  135.     int temp2[] = new int[100];
  136.     int len;
  137.     int xorlen;
  138.  
  139.     // convert binary number to decimal number
  140.     public int binaryToDecimal(int myNum)
  141.     {
  142.         int dec = 0, no, i, n = 0;
  143.         no = myNum;
  144.         // Find total digit of no of inupted number
  145.         while (no > 0)
  146.         {
  147.             n++;
  148.             no = no / 10;
  149.         }
  150.         // Convert inputed number into decimal
  151.         no = myNum;
  152.         for (i = 0; i < n; i++)
  153.         {
  154.             int temp = no % 10;
  155.             dec = dec + temp * ((int) Math.pow(2, i));
  156.             no = no / 10;
  157.         }
  158.         return dec;
  159.     }
  160.  
  161.     public int decimalToBinary(int myNum)
  162.     {
  163.         int j, i = -1, no, temp = 0;
  164.         no = myNum;
  165.         int t[] = new int[100];
  166.         while (no > 0)
  167.         {
  168.             i++;
  169.             temp = no % 2;
  170.             t[i] = temp;
  171.             no = no / 2;
  172.         }
  173.         len = (i + 1);
  174.         j = -1;
  175.         for (i = len; i >= 0; i--)
  176.         {
  177.             j++;
  178.             bin[j] = t[i];
  179.         }
  180.         return len;
  181.     }
  182.  
  183.     // find the specific bit value of binary number at given position
  184.     public int binaryArrayAtPosition(int pos)
  185.     {
  186.         return bin[pos];
  187.     }
  188.  
  189.     public int xorinArrayAt(int pos)
  190.     {
  191.         return xor[pos];
  192.     }
  193.  
  194.     // perform the binary X-OR operation
  195.     public int xorop(int a[], int b[], int arrlen)
  196.     {
  197.         int i;
  198.         for (i = 0; i < arrlen; i++)
  199.         {
  200.             xor[i] = (a[i] == b[i]) ? 0 : 1;
  201.         }
  202.         xorlen = i;
  203.         return xorlen;
  204.     }
  205.  
  206.     // perform the binary X-OR operation
  207.     public int xorop(String s, char c[])
  208.     {
  209.         int i = -1;
  210.         for (i = 0; i < s.length(); i++)
  211.         {
  212.             xor[i] = (s.charAt(i) == c[i]) ? 0 : 1;
  213.         }
  214.         xorlen = i;
  215.         return xorlen;
  216.     }
  217.  
  218.     public int getLen()
  219.     {
  220.         return len + 1;
  221.     }
  222.  
  223.     // display binary bit pattern or the array
  224.     public void displayBinaryArray()
  225.     {
  226.         for (int i = 0; i <= len; i++)
  227.         {
  228.             System.out.println("\n Binary Array :" + bin[i]);
  229.         }
  230.     }
  231. }

Output:

$ javac OneTimePadCipher.java
$ java OneTimePadCipher
 
Enter the message: 
Sanfoundry
'Sanfoundry' in encrypted message : 0000110 0110100 0111011 0110011 0111010 0100000 0111011 0110001 0100111 0101100 
'0000110 0110100 0111011 0110011 0111010 0100000 0111011 0110001 0100111 0101100 ' in decrypted message : Sanfoundry

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.