How to remove letter in space for One Time Pad cipher?

105 views Asked by At

I have found this code somewhere on the web for reference yet this bothers me. The result is correct but somehow there is something wrong with the process.

import java.io.*;
import java.util.*;
import java.util.Scanner;

public class onetime{
   public static void main(String[] args){
      System.out.println("Type Message: " );
      Scanner sc = new Scanner(System.in);
      String text = sc.nextLine();
      String key = RandomAlpha(text.length());

      String enc = OTPEncryption(text,key);

      System.out.println("Plaintext : " +text);
      System.out.println("Key:        "+key);
      System.out.println("Encrypted : "+enc);
      System.out.println("Decrypted : "+OTPDecryption(enc,key));
   }

   public static String RandomAlpha(int len){
      Random r = new Random();
      String key = "";
      for(int x=0;x<len;x++)
         key = key + (char) (r.nextInt(26) + 'A');
      return key;
   }

   public static String OTPEncryption(String text,String key){
      String alphaU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      String alphaL = "abcdefghijklmnopqrstuvwxyz";

      int len = text.length();

      String sb = "";
      for(int x=0;x<len;x++){
         char get = text.charAt(x);
         char keyget = key.charAt(x);
         if(Character.isUpperCase(get)){
            int index = alphaU.indexOf(get);
            int keydex = alphaU.indexOf(Character.toUpperCase(keyget));

            int total = (index + keydex) % 26;

            sb = sb+ alphaU.charAt(total);
         }
         else if(Character.isLowerCase(get)){
            int index = alphaL.indexOf(get);
            int keydex = alphaU.indexOf(Character.toLowerCase(keyget));

            int total = (index + keydex) % 26;

            sb = sb+ alphaL.charAt(total);
         }
         else{
            sb = sb + get;
         }
      }

      return sb;
   }
   public static String OTPDecryption(String text,String key){
      String alphaU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      String alphaL = "abcdefghijklmnopqrstuvwxyz";

      int len = text.length();

      String sb = "";
      for(int x=0;x<len;x++){
         char get = text.charAt(x);
         char keyget = key.charAt(x);
         if(Character.isUpperCase(get)){
            int index = alphaU.indexOf(get);
            int keydex = alphaU.indexOf(Character.toUpperCase(keyget));

            int total = (index - keydex) % 26;
            total = (total<0)? total + 26 : total;

            sb = sb+ alphaU.charAt(total);
         }
         else if(Character.isLowerCase(get)){
            int index = alphaL.indexOf(get);
            int keydex = alphaU.indexOf(Character.toLowerCase(keyget));

            int total = (index - keydex) % 26;
            total = (total<0)? total + 26 : total;

            sb = sb+ alphaL.charAt(total);
         }
         else{
            sb = sb + get;
         }
      }

      return sb;
   }

}

Output

  • Plain Text: help me
  • Key: QMMNQAB
  • Encrypted: gdko ld
  • Decrypt: help me

so as you can here in the output the second 'Q' of the key isn't supposed to be there because it's supposed to be a space. How can I remove it? Your answer is highly appreciated thank you :)

0

There are 0 answers