0

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 :)

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Snow
  • 101
  • 3
  • 16
  • Why do you say it's "supposed to be a space"? The key should be unrelated to the input, so it should have no relation to the plain text string pattern. If you're asking about the `Q`, then the immediate observation should be that the `M` is clearly wrong, too, because it in the same spot as both the `e` and an `l`, and now 4/7th of that key would be wrong. So: what makes you think the key has to mirror the input? – Mike 'Pomax' Kamermans Jul 26 '18 at 22:23
  • From what I knew if in computing it, isn't it supposed to like one liner? like 'H' + "Q" = G. So for an instance "(space)" + Q = so Q here serves the same answer which is a space bar but won't it be the same if removed the Second Q in there or any random letter generated when computed with space bar? Like equalizing the number of characters without the space bar might as well the key? Sorry my english is not that good. I hope you do understand me – Snow Jul 26 '18 at 22:43
  • The key is like a password used to modify the input to produce the output. If your name is "John Snow" is it required that your password be 9 characters with a blank in the 5th position? It is also unclear what you mean by _"the output the second 'Q' of the key isn't supposed to be there because it's supposed to be a space"_. The output _does_ contain a space where there was a space in the input. Finally, you ***do not*** want to have spaces in the output where there were spaces in the input. This gives away a valuable clue (word sizes) to an attacker. The premise of the question is unclear – Jim Garrison Jul 26 '18 at 23:03
  • "Finally, you do not want to have spaces in the output where there were spaces in the input. This gives away a valuable clue (word sizes) to an attacker. " - you've got a point. Anyways I understand. Thank you :) – Snow Jul 26 '18 at 23:16

0 Answers0