0% found this document useful (0 votes)
21 views5 pages

Public Class CaesarCipher

The document contains implementations of three different encryption algorithms: Caesar Cipher, Hill Cipher, and Vigenere Cipher. Each algorithm includes methods for encrypting and decrypting messages, with examples provided in the main method. The Caesar Cipher uses a shift value, the Hill Cipher utilizes a key matrix, and the Vigenere Cipher employs a repeating key for encryption and decryption.

Uploaded by

Anvita Manne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Public Class CaesarCipher

The document contains implementations of three different encryption algorithms: Caesar Cipher, Hill Cipher, and Vigenere Cipher. Each algorithm includes methods for encrypting and decrypting messages, with examples provided in the main method. The Caesar Cipher uses a shift value, the Hill Cipher utilizes a key matrix, and the Vigenere Cipher employs a repeating key for encryption and decryption.

Uploaded by

Anvita Manne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

public class CaesarCipher {

public static void main(String[] args) {

String message = "CRYPTOGRAPHY AND NETWORK SECURITY";

int shift = 4;

// Encrypt the message

String encrypted = encrypt(message, shift);

System.out.println("Encrypted Message: " + encrypted);

// Decrypt the message

String decrypted = decrypt(encrypted, shift);

System.out.println("Decrypted Message: " + decrypted);

public static String encrypt(String message, int shift) {

StringBuilder result = new StringBuilder();

for (char c : message.toCharArray()) {

if (Character.isLetter(c)) {

char base = Character.isUpperCase(c) ? 'A' : 'a';

result.append((char) ((c - base + shift) % 26 + base));

} else {

result.append(c);

return result.toString();

public static String decrypt(String encrypted, int shift) {

return encrypt(encrypted, 26 - shift);


}

public class HillCipher {

public static void main(String[] args) {

String message = "HELP";

int[][] keyMatrix = {

{2, 3},

{1, 4}

};

// Encrypt the message

String encrypted = encrypt(message, keyMatrix);

System.out.println("Encrypted Message: " + encrypted);

public static String encrypt(String message, int[][] keyMatrix) {

int n = keyMatrix.length;

int[] messageVector = new int[n];

StringBuilder encryptedMessage = new StringBuilder();

for (int i = 0; i < message.length(); i += n) {

for (int j = 0; j < n; j++) {

messageVector[j] = message.charAt(i + j) - 'A';


}

for (int j = 0; j < n; j++) {

int sum = 0;

for (int k = 0; k < n; k++) {

sum += keyMatrix[j][k] * messageVector[k];

encryptedMessage.append((char) ('A' + (sum % 26)));

return encryptedMessage.toString();

public class VigenereCipher {

public static void main(String[] args) {

String message = "HELLOCRYPTOGRAPHY";

String key = "KEY";

// Encrypt the message

String encrypted = encrypt(message, key);

System.out.println("Encrypted Message: " + encrypted);

// Decrypt the message


String decrypted = decrypt(encrypted, key);

System.out.println("Decrypted Message: " + decrypted);

public static String encrypt(String message, String key) {

StringBuilder result = new StringBuilder();

key = generateKey(message, key);

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

char c = (char) (((message.charAt(i) - 'A') + (key.charAt(i) - 'A')) %


26 + 'A');

result.append(c);

return result.toString();

public static String decrypt(String encrypted, String key) {

StringBuilder result = new StringBuilder();

key = generateKey(encrypted, key);

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

char c = (char) (((encrypted.charAt(i) - 'A') - (key.charAt(i) - 'A') +


26) % 26 + 'A');

result.append(c);

return result.toString();

public static String generateKey(String message, String key) {

StringBuilder newKey = new StringBuilder(key);

while (newKey.length() < message.length()) {

newKey.append(key);
}

return newKey.substring(0, message.length());

You might also like