[go: up one dir, main page]

0% found this document useful (0 votes)
13 views1 page

ROT13 Cipher Java

The document presents a Java program that implements the ROT13 cipher for text encryption. It prompts the user for input, checks the length constraints, and then encrypts the text by shifting letters 13 places in the alphabet. The resulting cipher text is displayed to the user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

ROT13 Cipher Java

The document presents a Java program that implements the ROT13 cipher for text encryption. It prompts the user for input, checks the length constraints, and then encrypts the text by shifting letters 13 places in the alphabet. The resulting cipher text is displayed to the user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

ROT13 Cipher in Java

import java.util.Scanner;

public class ROT13Cipher {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Taking input
System.out.print("Enter a text: ");
String input = scanner.nextLine();

int length = input.length();

// Checking length constraints


if (length <= 3 || length >= 100) {
System.out.println("Invalid input. Length must be greater than 3 and less
than 100.");
} else {
String cipherText = "";

// Encrypting the text using ROT13


for (int i = 0; i < length; i++) {
char c = input.charAt(i);

if (c >= 'a' && c <= 'z') {


c = (char) ((c - 'a' + 13) % 26 + 'a');
} else if (c >= 'A' && c <= 'Z') {
c = (char) ((c - 'A' + 13) % 26 + 'A');
}

cipherText += c; // Append the encrypted character


}

System.out.println("The cipher text is: " + cipherText);


}

scanner.close();
}
}

You might also like