0% found this document useful (0 votes)
7 views2 pages

Caesar Cipher

The document presents a Java program that implements the Caesar Cipher technique for text encryption. It defines a method to encrypt a given text by shifting characters based on a specified integer value. The example provided encrypts the text 'ATTACKATONCE' with a shift of 4, resulting in the ciphered text 'EXXEGOEXSRGI'.
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)
7 views2 pages

Caesar Cipher

The document presents a Java program that implements the Caesar Cipher technique for text encryption. It defines a method to encrypt a given text by shifting characters based on a specified integer value. The example provided encrypts the text 'ATTACKATONCE' with a shift of 4, resulting in the ciphered text 'EXXEGOEXSRGI'.
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/ 2

PRACTICAL 01

CAESAR CIPHER
//A Java Program to illustrate Caesar Cipher Technique
class CaesarCipher
{
// Encrypts text using a shift of s
public static StringBuffer encrypt(String text, int s)
{
StringBuffer result= new StringBuffer();

for (int i=0; i<text.length(); i++)


{
if (Character.isUpperCase(text.charAt(i)))
{
char ch = (char)(((int)text.charAt(i) +
s - 65) % 26 + 65);
result.append(ch);
}
else
{
char ch = (char)(((int)text.charAt(i) +
s - 97) % 26 + 97);
result.append(ch);
}
}
return result;
}

// Driver code
public static void main(String[] args)
{
String text = "ATTACKATONCE";
int s = 4;
System.out.println("Text : " + text);
System.out.println("Shift : " + s);
System.out.println("Cipher: " + encrypt(text, s));
}
}

OUTPUT:

Text : ATTACKATONCE
Shift: 4
Cipher: EXXEGOEXSRGI

You might also like