Open Immanuel-Anthony opened 1 month ago
Ok, I assign this issue to you!
Assigned to you, @Immanuel-Anthony
Happy contribution!
I want to contribute on this issue please assign me .
I want to contribute on this issue please assign me .
Sure, I will assign this issue to you @jayendranar02
Issue has been assigned to you @jayendranar02!
Happy contribution!
import java.util.Scanner;
public class VigenereCipher {
// Method to encrypt plaintext using Vigenère Cipher
public static String encrypt(String plaintext, String keyword) {
StringBuilder encryptedText = new StringBuilder();
keyword = generateKeyword(plaintext, keyword);
for (int i = 0; i < plaintext.length(); i++) {
char p = plaintext.charAt(i);
char k = keyword.charAt(i);
// Encrypt only alphabetic characters
if (Character.isLetter(p)) {
char base = Character.isUpperCase(p) ? 'A' : 'a';
int encryptedChar = (p + k - 2 * base) % 26 + base;
encryptedText.append((char) encryptedChar);
} else {
encryptedText.append(p); // Non-alphabetic characters remain unchanged
}
}
return encryptedText.toString();
}
// Method to decrypt ciphertext using Vigenère Cipher
public static String decrypt(String ciphertext, String keyword) {
StringBuilder decryptedText = new StringBuilder();
keyword = generateKeyword(ciphertext, keyword);
for (int i = 0; i < ciphertext.length(); i++) {
char c = ciphertext.charAt(i);
char k = keyword.charAt(i);
// Decrypt only alphabetic characters
if (Character.isLetter(c)) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
int decryptedChar = (c - k + 26) % 26 + base;
decryptedText.append((char) decryptedChar);
} else {
decryptedText.append(c); // Non-alphabetic characters remain unchanged
}
}
return decryptedText.toString();
}
// Generate the repeated keyword based on the length of the text
private static String generateKeyword(String text, String keyword) {
StringBuilder result = new StringBuilder();
int j = 0; // Index for keyword
for (int i = 0; i < text.length(); i++) {
char currentChar = text.charAt(i);
if (Character.isLetter(currentChar)) {
result.append(keyword.charAt(j % keyword.length()));
j++;
} else {
result.append(currentChar); // Non-alphabetic characters remain unchanged
}
}
return result.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the plaintext:");
String plaintext = scanner.nextLine();
System.out.println("Enter the keyword:");
String keyword = scanner.nextLine();
String encrypted = encrypt(plaintext, keyword);
System.out.println("Encrypted Text: " + encrypted);
String decrypted = decrypt(encrypted, keyword);
System.out.println("Decrypted Text: " + decrypted);
scanner.close();
}
}
Explanation of the Code--->
Encrypt Method:- Encrypts the given plaintext using the specified keyword. It generates a repeated keyword to match the length of the plaintext and performs the encryption for each letter.
Decrypt Method:- Decrypts the given ciphertext using the same keyword. It similarly generates a matching keyword and applies the decryption logic.
Generate Keyword:- This method constructs a keyword of the same length as the input text, skipping non-alphabetic characters. User Interaction:
The main method handles user input for plaintext and keyword, performs the encryption and decryption, and displays the results.
Conclusion---> This proposal outlines a complete Vigenère Cipher program in Java that can encrypt and decrypt text. The provided code is ready for integration and testing. By following this plan, we can successfully add a functional and educational cryptographic tool to our project.
Proposal :
I would like to add a Vigenère Cipher program in Java to =>
Encrypt Data Decrypt Data Thank you