0
Hello. I’m doing a course work where I have to take a PDF file or the TXT that the teacher went through, and encrypt it by Cesar’s cipher algorithms, playfrai and transposition. I would like to know how to make my algorithm read this file or PDF or TXT and encrypt in the requested format, because every time I pass too much large text it breaks.
It follows one of the algorithms I intend to use to carry out the activities. I am able to suggestions.
public static class CaesarCipher
{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static String encrypt(String plainText, int shiftKey)
{
plainText = plainText.toLowerCase();
String cipherText = "";
for (int i = 0; i < plainText.length(); i++)
{
int charPosition = ALPHABET.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = ALPHABET.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText;
}
public static String decrypt(String cipherText, int shiftKey)
{
cipherText = cipherText.toLowerCase();
String plainText = "";
for (int i = 0; i < cipherText.length(); i++)
{
int charPosition = ALPHABET.indexOf(cipherText.charAt(i));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0)
{
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
plainText += replaceVal;
}
return plainText;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String for Encryption: ");
String message = new String();
message = sc.next();
System.out.println(encrypt(message, 3));
System.out.println(decrypt(encrypt(message, 3), 3));
sc.close();
}
}
edited and put , friend it works more when selecting a large part of the text it empaca would like to read the file and perform operations.
– Renan Alexandre
Yes friend ! I would like to know how to implement this in the code . If there is a way to read the file and practice what the algorithm does . If you have another suggestion accepted . Thank you for your attention.
– Renan Alexandre