Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2003 SOLVED QUESTION 1: DECRYPTION

Sunday, March 17, 2019

ISC COMPUTER SCIENCE PRACTICAL 2003 SOLVED QUESTION 1: DECRYPTION

 A simple encryption system uses a shifting process to hide a message. The value of the shift can be in the range of 1 to 26. For example a shift of 7 means that A=U, B=V, C-W, etc. i.e.,
Text :     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
CODE : 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

First an extra space is added to the end of the string. To make things a little more difficult, space 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. Tour program must reject any non valid value for shift and display an error message “INVALID SHIFT VALUE”. Assume all characters are upper-case. Test your program for the following data and some data that you have coded, using rules given above:

INPUT :
CODED TEXT :  UHINBY LKKQCH HYLKK
SHIFT : 7
OUTPUT   DECODED TEXT : ANOTHER WINNER 

INPUT :  CODED TEST: RUIJGG EVGGBK SAGG
SHIFT : 11
OUTPUT   DECODED TEXT : BEST OF LUCK 

The idea behind the program is as follows
1. Take the input message and shift key(check the validity)
2. Now remove the space between CODED TEXT
    for example  UHINBY LKKQCH HYLKK should be
    coverted to  UHINBYLKKQCHHYLKK, since in the
    original message the space is incoded with "QQ".
3. Now seperate the words by findig "QQ" in the CODED TEXT
4. The seperated words are now shifted to position determined by the shift key
5. Display the shifted words

To achieve this we at first discuss the method
private char shiftPlaces(char c,int key) {
       c=(char)(c+key > 90 ? (65+(c+key)%90-2):c+key-1);
       return c;
    }

It simply move the character to 7 places using the ascii values:
To understand this, take for example when shift key is 7.
In this the code 'H'(ascii 72) shifts to 'N'(ascii 78)
and the code  'U'(ascii 85) shifts to 'A'(ascii 65)
Simple see that 72+7-1 = 79-1 = 78
but for 'U' if we add 7-1 to the ascii of it, the value becomes
85+7-1=91 which exceeds the ascii of 'Z', therefore
we are adding 65 (ascii of 'A' ) to ascii of ('U'+key)%ascii of 'Z' and subtracting 2
to get at the right position.
Now for step 3. we are using the code below  to extract words
seperated by "QQ"

 for(i=0;i<text_to_decrypt.length()-2;i++) {
             s="";
             for(j=i;j<text_to_decrypt.length()-2;j++) {
                    if(shiftPlaces(text_to_decrypt.charAt(j+1),key)!='Q' && shiftPlaces(text_to_decrypt.charAt(j+2),key)!='Q') {
                          s+=text_to_decrypt.charAt(j);
                          }
                    else {
                          s+=text_to_decrypt.charAt(j);
                          s +=text_to_decrypt.charAt(j+1);
                          j=j+3;
                          i=j;
                          break;
                          }
                    }.....

Once int the enrypted message spaces are remvode by calling 
private void removeSpacesInCodedText()
the mesage will look like, "UHINBYLKKQCHHYLKK"
then the two for loops extracts UHINBYL and QCHHYL and then individual characters are shifted 
to the rquired position and printed.
This process is bit complicated, I urge you to do this independently:
Given a string, for example
THISQQISQQAQQTESTQQ (since it is given that a space is added at end 
while encrypting) extract the words THIS, IS, A, TEST
It will be a good exercise! str
Test your code with this data:
THEQQPROGRAMMINGQQPROJECT
OUTPUT: THE, PROGRAMMING, PROJECT
Alternately we can use,

String s = str.replace("QQ"," ");  the method will look like
public void decryptionProcess() {
       //removing spaces in encrypted message, since original word seperator is QQ
       removeSpacesInCodedText();
       String s="";
       System.out.println(s);
       for(int i=0;i<text_to_decrypt.length();i++) { // shiftin each character
             s = s+shiftPlaces(text_to_decrypt.charAt(i),key);
             }
       s = s.replace("QQ"," "); // replacing
       System.out.println(s);
       }

that will help in easier extraction.
Both the codes are given below. The choice is yours! Using standard library function or to
code that library function yourself!!

CODE-1

import java.util.*;
public class ISC2015Q1 {
       public static void main(String[] args) {
             String msg;
             int shift;
             Scanner in = new Scanner(System.in);
             System.out.println("INPUT CODED TEXT:");
             msg = in.nextLine();
             do {
             System.out.println("Enter the shift key:");
             shift = in.nextInt();
             if(shift <1 || shift > 26)
                   System.out.println("Invalid shift key, input again:");
             }while(shift <1 || shift > 26);
             decryption obj = new decryption(msg.toUpperCase(),shift);
             System.out.println("OUTPUT:");
             obj.decryptionProcess();
             in.close();
             }
}

class decryption {
public void decryptionProcess() {
       //removing spaces in encrypted message, since original word seperator is QQ
       removeSpacesInCodedText();
       //extract word separated by QQ
       String s="";
       System.out.println(s);
       for(int i=0;i<text_to_decrypt.length();i++) { // shifting individual characters
             s = s+shiftPlaces(text_to_decrypt.charAt(i),key);
             }
       s = s.replace("QQ"," "); // replacing using library function
       System.out.println(s);
       }
      
private void removeSpacesInCodedText() {
             String temp="";
             for(int i=0;i<text_to_decrypt.length();i++) {
                    if(text_to_decrypt.charAt(i)==' ')
                          continue;
                    else
                          temp = temp+text_to_decrypt.charAt(i);
                    }
             text_to_decrypt = temp;
             }
private char shiftPlaces(char c,int key) {
       c=(char)(c+key > 90 ? (65+(c+key)%90-2):c+key-1);
       return c;
    }

decryption(String msg,int shift){
       text_to_decrypt = msg;
       key = shift;
       }
private int key;
private String text_to_decrypt;
}
_____________________________________________________________________________
CODE -2


import java.util.*;
public class ISC2003Q1 {
       public static void main(String[] args) {
             String msg;
             int shift;
             Scanner in = new Scanner(System.in);
             System.out.println("INPUT CODED TEXT:");
             msg = in.nextLine();
             do {
             System.out.println("Enter the shift key:");
             shift = in.nextInt();
             if(shift <1 || shift > 26)
                   System.out.println("Invalid shift key, input again:");
             }while(shift <1 || shift > 26);
             decryption obj = new decryption(msg.toUpperCase(),shift);
             System.out.println("OUTPUT:");
             obj.decryptionProcess();
             in.close();
             }
}

class decryption {
public void decryptionProcess() {
       //removing spaces in encrypted message, since original word seperator is QQ
       removeSpacesInCodedText();
       //extract word separated by QQ
       int i,j;
       String s;
       for(i=0;i<text_to_decrypt.length()-2;i++) {
             s="";
             for(j=i;j<text_to_decrypt.length()-2;j++) {
                    if(shiftPlaces(text_to_decrypt.charAt(j+1),key)!='Q' && shiftPlaces(text_to_decrypt.charAt(j+2),key)!='Q') {
                          s+=text_to_decrypt.charAt(j);
                          }
                    else {
                          s+=text_to_decrypt.charAt(j);
                          s +=text_to_decrypt.charAt(j+1);
                          j=j+3;
                          i=j;
                          break;
                          }
                    }
             //decoding the message
             for(int l=0;l<s.length();l++)
                    System.out.print(shiftPlaces(s.charAt(l),key));
             System.out.print(" ");
             }
 }
private void removeSpacesInCodedText() {
             String temp="";
             for(int i=0;i<text_to_decrypt.length();i++) {
                    if(text_to_decrypt.charAt(i)==' ')
                          continue;
                    else
                          temp = temp+text_to_decrypt.charAt(i);
                    }
             text_to_decrypt = temp;
             }
private char shiftPlaces(char c,int key) {
       c=(char)(c+key > 90 ? (65+(c+key)%90-2):c+key-1);
       return c;
    }

decryption(String msg,int shift){
       text_to_decrypt = msg;
       key = shift;
       }
private int key;
private String text_to_decrypt;
}

No comments:

Post a Comment