0

I'm working on encrypted text using ascii value and I have this code:

import java.util.*;
public class OneTimePad{
   public static void main(String[] args){

        Scanner _user_ = new Scanner(System.in);  //Scanner Obj
    
        System.out.print(" Enter your Message : ");
        String Message = _user_.next();
        System.out.print(" Enter your Key : ");
        String Key = _user_.next();
        
        char Fnal;
        int Total;

         for(int l = 0 , m = 0; (l < Message.length() && m < Key.length()); l++,m++){   
            Total = (int)Message.charAt(l) + (int)Key.charAt(m);
           
            if((Total >=65 && Total <= 90) || (Total >= 97 && Total <=122 )){
                    System.out.print("Case 1");
                    Fnal = (char)Total;
                    System.out.print(Fnal);
            }else{
                System.out.print("Case 2");
                int a    = Total % 26;
                Fnal = (char)a;
                System.out.print(Fnal);
            }
        }
    }
}

Here is my output:

Enter your Message : hello

Enter your Key : hihih

Why only this two lines are printed on console? I don't see any error.


UPDATE

Here's my new code:

for(int l = 0 , m = 0; (l < Message.length() && m < Key.length()); l++,m++) {   
  Total = (int)Message.charAt(l) + (int)Key.charAt(m);
  //char s = Message.charAt(l);
  char dos = Message.charAt(l);
  String comp = String.valueOf(dos);

  if (comp.contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ")){
    if(Total >=65 && Total <= 90){
      Fnal = (char)Total;
      System.out.print(Fnal);
    }else{
       int a    = Total % 26 + 65 ;
       Fnal = (char)a;
       System.out.print(Fnal);
    }
  }
}

Why it didn't Working?

Calaf
  • 1,133
  • 2
  • 9
  • 22
  • It is because the results of addition is out-of-range of `(Total >=65 && Total <= 90) || (Total >= 97 && Total <=122 )` and therefore only control characters (character code less than 32) are printed. What is your desired behavior? – MikeCAT Jul 30 '20 at 16:57
  • I am printing encrypted text using ascii value , i can resolve it. – sketch hacker Jul 30 '20 at 17:06

2 Answers2

0

Not every integer has a visible character associated with it. You can use this simple method to print a list of integer and the relative char:

public static void printChars(){
    for (int i=0; i<1000; i++){
        System.out.println(i+" -> "+(char)i);
    }//for
}//printChars

So, why you don't see any output? Lets try with an example.

Enter your Message: a

Enter your Key: s

  • (int)Message.charAt(l) will be 97;
  • (int)Key.charAt(m) will be 115;
  • Finally, Total = (int)Message.charAt(l) + (int)Key.charAt(m) will be 212.

The two conditions Total >=65 && Total <= 90 and Total >= 97 && Total <=122 are not verified, so we have to see what happens in the "else" block.

  • int a = Total % 26 will be 4

As you can see by using the previous method printChars(), no visible character is associated with the number 4, therefore nothing is shown as output.


Tip: try to use some numeric input, for example

Enter your Message: 0

Enter your Key: 3

  • Total will be 48+51=99

The conditions are verified, so we have to go in the "if" block. The output will be (int)99, so we will see:

c


UPDATE

In your new code, first of all you have to define Total and Fnal as int. For example:

int Total = (int)Message.charAt(l) + (int)Key.charAt(m);

The main error is the use of contains(...).

If you write comp.contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), you say that the string must have ABCDEFGHIJKLMNOPQRSTUVWXYZ as a substring (and not "A or B or C ...").

Probably you need a Pattern, and you can read a similar question here: Check if String contains only letters

Calaf
  • 1,133
  • 2
  • 9
  • 22
0
import java.util.Scanner;
public class Onetimepad{
    public static void main(String[] args){
    
    Scanner _user_ = new Scanner(System.in);    // A Scanner Obj for to take input from user.
                            // System.in is to writing something on console
    
       // Take Message and key from user
    
    System.out.print(" Enter Message : ");
    String Message = _user_.next();

    System.out.print(" Enter Key ");
    String Key = _user_.next();

    // Converting each String latter into Character . And finding that latter's ascii value and 
    // And add that value to key's ascii value , and if it is greater than max than % 26.

    if (Message.length() == Key.length())
      { 
    for(int i = 0 , j = 0 ; i < Message.length() && j < Key.length() ; i++,j++)
    {
        char chmsg = Message.charAt(i);      // "V" = 'V'    
        char chkey = Key.charAt(j);    // "D" = 'D' 
        
        int chpr = (int)chmsg + (int)chkey; 
        
        if(chpr >= 65 && chpr <= 90)
        {
                 System.out.print((char)chpr);
        }
        else
        {
             chpr = chpr % 26+ 65;
             System.out.print((char)chpr);
        }
    } 
    
      }
    else
    {
      System.out.print(" Length of Message and Key is not same : ");
    }

}
} 


// What it is write code and the logic is right.
//Please Tell, It's a OneTimePad Encryption.