Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Ayushee
Original file line number Diff line number Diff line change
@@ -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<toEncrypt.length(); i++)
{
char shiftedLetter = (char) (toEncrypt.charAt(i)+positions);
if(shiftedLetter>'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;
}
}