Simple Encryption Program in Java

A simple encryption system uses a shifting process to hide a message. The value of the shift can be in the range 1 to 26. For example, a shift of 7 means that A = U, B = V, C = W, etc. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z U V W X Y Z A B C D E F G H I J K L M N O P Q R S T Firstly an extra space is added to the end of the string. To make things little more difficult, spaces within the original text are replaced with QQ before the text is encrypted. Double Q (QQ) was selected because no English word ends in Q or contains QQ. Additionally, the coded message is printed in blocks of six characters separated by spaces. The last block might not contain six characters. Write a program that takes the coded text (less than 100 characters), the shift value and prints the decoded original text. Your program must reject any invalid value for shift and also display a suitable message “INVALID SHIFT VALUE” for the same. Assume all characters are in uppercase. Test your program for the following data as well as your own data: Example 1: INPUT: CODED TEXT: UHINBY LQQQCH HYLQQ SHIFT: 7 OUTPUT: DECODED TEXT: ANOTHER WINNER Example 2: INPUT: CODED TEXT: RUIJQQ EVQQBK SAQQ SHIFT: 11 OUTPUT: DECODED TEXT: BEST OF LUCK Example 3: INPUT: CODED TEXT: DKSQQW NAQQUK QQQ SHIFT: 29 OUTPUT: INVALID SHIFT VALUE
 import java.io.*;
 class Decoder
{
 public static void main(String args[])throws IOException
 {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("CODED TEXT: ");
 String c = br.readLine();
 System.out.print("SHIFT: ");
 int s = Integer.parseInt(br.readLine());
 if(s > 26)
{
 System.out.println("INVALID SHIFT VALUE");
 return;
 }
 c = c.toUpperCase();
 c = c.replace(" ", "");
 c = c.replace("QQ", " ");
 String d = "";
 for(int i = 0; i < c.length(); i++)
{
 char ch = c.charAt(i);
 if(Character.isLetter(ch))
{
 if(ch + s - 1 <= 90)
 d += (char)(ch + s - 1);
 else
{
 char letter = (char)((ch + s - 1) % 90 + 64);
 d += letter;
 }
 }
 else d += ch;
 }
 System.out.println("DECODED TEXT: " + d);
 }
 }

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...