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());