0% found this document useful (0 votes)
2 views

print code

The document contains code snippets for various encryption and decryption algorithms, including Caesar Cipher, Rail Fence Cipher, Transposition Cipher, and Vernam Cipher. Each algorithm includes methods for encrypting and decrypting text, utilizing user input for parameters like shift values and keys. Additionally, there are examples of hash code generation and verification using SHA-256.

Uploaded by

CISCO NETWORKING
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

print code

The document contains code snippets for various encryption and decryption algorithms, including Caesar Cipher, Rail Fence Cipher, Transposition Cipher, and Vernam Cipher. Each algorithm includes methods for encrypting and decrypting text, utilizing user input for parameters like shift values and keys. Additionally, there are examples of hash code generation and verification using SHA-256.

Uploaded by

CISCO NETWORKING
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Ceasar cipher encryption CaesarCipher Decrypt RailFenceCipherEncrypt RailFenceCipherDecrypt if (row == 0) { TranspositionCipherDecrypt

direction = 1;
import java.util.Scanner; import java.util.Scanner; import java.util.Scanner; } } else if (row == numRails - 1) { for (int row = 0; row < numRows;
import java.util.Scanner; import java.util.Scanner;
public class CaesarCipherEncrypt { public class public class // Print the zigzag pattern public class direction = -1; row++) {
public class
public static String encrypt(String RailFenceCipherEncrypt { System.out.println("Zigzag RailFenceCipherDecrypt { } for (int col = 0; col < key; col++)
CaesarCipherDecrypt { TranspositionCipherDecrypt {
text, int shift) { public static String encrypt(String pattern:"); public static String decrypt(String row += direction; {
public static String for (int i = 0; i < numRails; i++) { } public static String
StringBuilder encryptedText = new text, int numRails) { cipher, int numRails) {
decrypt(String text, int shift) { if (numRails == 1) { for (int j = 0; j < text.length(); // Print the zigzag pattern decrypt(String cipher, int key) {
StringBuilder(); if (numRails == 1) { System.out.print(grid[row][col]);
return return text; j++) { System.out.println("Zigzag int numRows = (int)
for (char character : return cipher; }
} if (rail[i][j] == '\n') { pattern:"); Math.ceil((double) cipher.length()
text.toCharArray()) { CaesarCipherEncrypt.encrypt(te } char[][] rail = new System.out.println();
char[][] rail = new System.out.print(" "); char[numRails][cipher.length()]; for (int i = 0; i < numRails; i++) { / key);
if (Character.isLetter(character)) { xt, 26 - shift); } else { for (int j = 0; j < cipher.length(); char[][] grid = new }
char[numRails][text.length()]; for (int i = 0; i < numRails; i++) {
char base = } return
for (int i = 0; i < numRails; i++) { System.out.print(rail[i][j]); for (int j = 0; j < cipher.length(); j++) { char[numRows][key];
Character.isUpperCase(character) ? } if (rail[i][j] == '\n') { int index = 0; decryptedText.toString().trim();
public static void main(String[] for (int j = 0; j < text.length(); j++) { rail[i][j] = '\n';
'A' : 'a'; j++) { } } System.out.print(" "); for (int col = 0; col < key; }
args) {
encryptedText.append((char) rail[i][j] = '\n'; System.out.println(); } int direction = 1; // 1 for } else { col++) { public static void main(String[]
Scanner scanner = new } System.out.print(rail[i][j]);
((character - base + shift) % 26 + } down, -1 for up for (int row = 0; row < args) {
base)); Scanner(System.in); } return encryptedText.toString(); } Scanner scanner = new
int row = 0; numRows; row++) {
} else { System.out.print("Enter the int direction = 1; // 1 for down, -1 } for (int i = 0; i < cipher.length(); } Scanner(System.in);
if (index < cipher.length()) {
encryptedText.append(character); text to be decrypted: "); for up public static void main(String[] i++) { System.out.println(); System.out.print("Enter the
grid[row][col] =
}}return encryptedText.toString(); int row = 0; args) { rail[row][i] = '*'; }return decryptedText.toString(); text to be decrypted: ");
String text = cipher.charAt(index++);
}public static void main(String[] args) { for (int i = 0; i < text.length(); i++) Scanner scanner = new if (row == 0) { } public static void main(String[] String cipher =
scanner.nextLine(); Scanner(System.in); args) { } else {
Scanner scanner = new { direction = 1; scanner.nextLine();
System.out.print("Enter the rail[row][i] = text.charAt(i); System.out.print("Enter the text Scanner scanner = new grid[row][col] = ' ';
Scanner(System.in); } else if (row == numRails - 1) { System.out.print("Enter the
if (row == 0) { to be encrypted: "); Scanner(System.in); }
System.out.print("Enter the text to be
shift value: "); direction = -1; key (number of columns): ");
direction = 1; String text = scanner.nextLine(); System.out.print("Enter the text }
encrypted: "); int shift = scanner.nextInt(); } row += direction; int key = scanner.nextInt();
} else if (row == numRails - 1) { System.out.print("Enter the } int index = 0; to be decrypted: "); }
String text = scanner.nextLine(); String decrypted = direction = -1; number of rails: "); String cipher = StringBuilder decryptedText = String decrypted =
for (int i = 0; i < numRails; i++) {
System.out.print("Enter the shift decrypt(text, shift); } int numRails = scanner.nextLine(); new StringBuilder(); decrypt(cipher, key);
for (int j = 0; j < cipher.length();
value: "); row += direction; scanner.nextInt(); System.out.print("Enter the for (int row = 0; row <
j++) {
int shift = scanner.nextInt(); String } String encrypted = encrypt(text, if (rail[i][j] == '*' && index < number of rails: "); numRows; row++) { System.out.println("Encrypted
System.out.println("Encrypted
encrypted = encrypt(text, shift); StringBuilder encryptedText = numRails); cipher.length()) { int numRails = for (int col = 0; col < key; text: " + cipher);
text: " + text); System.out.println("Original scanner.nextInt();
System.out.println("Original text: " + new StringBuilder(); rail[i][j] = col++) {
text); for (int i = 0; i < numRails; i++) { text: " + text); cipher.charAt(index++); String decrypted = System.out.println("Decrypted
System.out.println("Decrypted for (int j = 0; j < text.length(); System.out.println("Encrypted } } } decrypt(cipher, numRails); text: " + decrypted);
System.out.println("Encrypted text: " decryptedText.append(grid[row][
text: " + decrypted); j++) { text: " + encrypted); StringBuilder decryptedText = System.out.println("Encrypted scanner.close();
+ encrypted); col]);
if (rail[i][j] != '\n') { new StringBuilder(); text: " + cipher); }
scanner.close(); scanner.close(); scanner.close(); System.out.println("Decrypted
}
} row = 0; } }
} encryptedText.append(rail[i][j]); } text: " + decrypted);
} direction = 1; for (int i = 0; i < // Print the grid
} } } cipher.length(); i++) { scanner.close();
} } System.out.println("Grid:");
decryptedText.append(rail[row][i]);
}

TranspositionCipherEncrypt for (int row = 0; row < VernamCipherDecrypt VernamCipherEncrypt HashCodeExample HashCodeVerifier
numRows; row++) {
import java.util.Scanner; for (int col = 0; col < key; import java.security.MessageDigest; import java.util.Scanner;
import java.util.Scanner; import java.util.Scanner;
public class col++) { public class import
public class
TranspositionCipherEncrypt { java.security.NoSuchAlgorithmExcept public class HashCodeVerifier {
VernamCipherDecrypt { VernamCipherEncrypt { ion;
public static String System.out.print(grid[row][col]); public static String public static String import java.util.Scanner; public static boolean
encrypt(String text, int key) { } decrypt(String encryptedText, encrypt(String text, String key) { public class HashCodeExample { verifyHash(String message, String
int numRows = (int) System.out.println(); String key) { StringBuilder encryptedText = public static String hash) {
Math.ceil((double) text.length() / } createHash(String message) { String newHash =
return new StringBuilder();
key); return try { HashCodeExample.createHash(mess
VernamCipherEncrypt.encrypt(e for (int i = 0; i < text.length();
char[][] grid = new encryptedText.toString(); MessageDigest digest = age);
ncryptedText, key); // Vernam i++) {
char[numRows][key]; } Cipher decryption is the same as
MessageDigest.getInstance("SHA- return newHash.equals(hash);
char encryptedChar = (char) 256"); }
int index = 0; public static void main(String[] encryption (text.charAt(i) ^ key.charAt(i % byte[] hashBytes =
for (int row = 0; row < args) { } key.length())); digest.digest(message.getBytes()); public static void main(String[] args)
numRows; row++) { Scanner scanner = new public static void main(String[] encryptedText.append(encrypted StringBuilder hashString = new {
for (int col = 0; col < key; Scanner(System.in); args) { Char); StringBuilder(); Scanner scanner = new
col++) { System.out.print("Enter the for (byte b : hashBytes) { Scanner(System.in);
Scanner scanner = new } return
if (index < text.length()) { text to be encrypted: "); hashString.append(String.format("%0
Scanner(System.in); encryptedText.toString();
grid[row][col] = String text = 2x", b)); } System.out.print("Enter the
System.out.print("Enter the } return hashString.toString(); original message: ");
text.charAt(index++); scanner.nextLine(); text to be decrypted: "); public static void main(String[] } catch String originalMessage =
} else { System.out.print("Enter the String encryptedText = args) { (NoSuchAlgorithmException e) { scanner.nextLine();
grid[row][col] = ' '; key (number of columns): "); scanner.nextLine(); Scanner scanner = new throw new RuntimeException(e);
} int key = scanner.nextInt(); } } System.out.print("Enter the hash
System.out.print("Enter the Scanner(System.in);
} String encrypted = public static void main(String[] args) code to verify: ");
key: "); System.out.print("Enter the
} encrypt(text, key); { String hash = scanner.nextLine();
String key = text to be encrypted: ");
StringBuilder encryptedText = System.out.println("Original Scanner scanner = new
scanner.nextLine(); String text = Scanner(System.in); boolean isValid =
new StringBuilder(); text: " + text); String decrypted = scanner.nextLine(); verifyHash(originalMessage, hash);
for (int col = 0; col < key; decrypt(encryptedText, key); System.out.print("Enter the System.out.print("Enter the if (isValid) {
col++) { System.out.println("Encrypted key: "); message to hash: "); System.out.println("The hash
for (int row = 0; row < text: " + encrypted); String message = code is valid.");
System.out.println("Encrypted String key =
numRows; row++) { scanner.close(); scanner.nextLine(); } else {
text: " + encryptedText); scanner.nextLine();
} System.out.println("The hash
String encrypted =
encryptedText.append(grid[row][ } System.out.println("Decrypted
String hash = code is not valid.");
encrypt(text, key); createHash(message); }
col]);
text: " + decrypted); System.out.println("Original System.out.println("Hash code: " +
}
scanner.close(); text: " + text); hash); scanner.close();
} } System.out.println("Encrypted }
// Print the grid } scanner.close(); }
text: " + encrypted);
System.out.println("Grid:"); }
scanner.close();
}
}}

You might also like