Created
October 27, 2019 00:54
-
-
Save intelliapps-io/d8c8337e47af6f508b5e0332e042905c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
//Define the class decryption. | |
public class decryption | |
{ | |
//Define the main() method. | |
public static void main(String[] args) | |
{ | |
//Declare the required variables. | |
String encryptedText; | |
String decryptedText=""; | |
Scanner s = new Scanner(System.in); | |
//Prompt the user for the input. | |
System.out.println("Enter the message"+ | |
" to be decrypted"); | |
//Store the encrypted message in | |
//the string object encryptedText. | |
encryptedText = s.nextLine(); | |
//Print the encrypted string on the screen. | |
System.out.println("Encrypted Message " | |
+encryptedText); | |
//Run the loop till the value of the variable | |
//key is greater than 99. | |
for (int key = 1; key < 100; key++) | |
{ | |
//Traverse the encrypted string character | |
//by character. | |
for (int index = 0; index < | |
encryptedText.length();index++) | |
{ | |
//Get the character at the current | |
//position of the encrypted message. | |
char encryptedchar = | |
encryptedText.charAt(index); | |
char decryptedChar; | |
//Check if encrypted key is greater | |
//than 126. | |
if(((int)encryptedchar+127-32)>126) | |
{ | |
//Get the decrypted character. | |
decryptedChar=(char) | |
((encryptedchar-key+127)-32); | |
} | |
//Else if if (original character+key) | |
//is less than 126. | |
else | |
{ | |
//Get the decrypted character. | |
decryptedChar=(char) | |
(encryptedchar-key); | |
} | |
//Append the current decrypted | |
//character to the decryptedText | |
//string object. | |
decryptedText+=decryptedChar; | |
} | |
//Print the decrypted message | |
//with the current key. | |
System.out.println | |
("Decrypted Message at key "+key+" is " | |
+decryptedText); | |
//Reset the string decryptedText | |
//object. | |
decryptedText=""; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment