From 130693023a303b55e2e81f90bef9cc988494f829 Mon Sep 17 00:00:00 2001 From: Ayushee Gupta <146634687+ayusheeg@users.noreply.github.com> Date: Sat, 28 Oct 2023 12:13:37 +0530 Subject: [PATCH] Create Ayushee --- Ayushee | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Ayushee diff --git a/Ayushee b/Ayushee new file mode 100644 index 0000000..84de3d9 --- /dev/null +++ b/Ayushee @@ -0,0 +1,73 @@ +//Ayushee Gupta + +import java.util.Scanner; + +public class CaesarCipher { + public static void main(String[] args) + { + Scanner scanner = new Scanner(System.in); + + System.out.println("----------Caesar Cipher-----------"); + System.out.print("Enter the Message: "); + String msg = scanner.nextLine(); + System.out.print("Enter the Position: "); + int position = scanner.nextInt(); + String encryptedMsg = Encrypt(msg, position); + String decryptMsg= Decrypt(encryptedMsg, position); + System.out.println("Encrypted Message: " + encryptedMsg); + System.out.println("Decrypted Message: " + decryptMsg ); + } + + public static String Encrypt(String textToEncrypt, int positions) + { + String toEncrypt = "", + result = ""; + + for(int i=0; i< textToEncrypt.length(); i++) + { + + if(textToEncrypt.charAt(i) == ' ' || textToEncrypt.charAt(i) == '.') + { + continue; + } + else + { + if(Character.isLowerCase(textToEncrypt.charAt(i))) + { + toEncrypt += Character.toUpperCase(textToEncrypt.charAt(i)); + } + else + { + toEncrypt += textToEncrypt.charAt(i); + } + } + } + + for(int i=0; i'Z') + { + shiftedLetter = (char) (shiftedLetter - 'Z' + 'A' - 1); + } + result += shiftedLetter; + } + return result; + } + + public static String Decrypt(String textToDecrypt, int positions) + { + String result = ""; + + for(int i=0; i< textToDecrypt.length(); i++) + { + char shiftedLetter = (char) (textToDecrypt.charAt(i)-positions); + if(shiftedLetter<'A') + { + shiftedLetter = (char) (shiftedLetter + 'Z' - 'A' + 1); + } + result += shiftedLetter; + } + return result; + } +}