How to code the Caesar Cipher: an introduction to basic encryption

We have not yet established a modified alphabet variable, so we should do that now.Step 4: Build a new sentence using the new characters in place of the original characters.Once we have found the value in the modified alphabet, we should set it to the same location in the StringBuilder we created.public String Encryption(String input, int key){ StringBuilder encrypted = new StringBuilder(input); String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String alphabet2 = alphabet.toLowerCase(); String keyedalphabet = alphabet.substring(key) + alphabet.substring(0, key);for (int q = 0; q < encrypted.length(); q++) { char currChar = encrypted.charAt(q); int index = alphabet.indexOf(currChar); if (index != -1) { char newChar = keyedalphabet.charAt(index); encrypted.setCharAt(q, newChar); }Step 5: repeat until sentence length is reached..(For loop)Now, we have checked if the character is upper-case, but we also need to check if the character is lower-case..To do this, we need to access alphabet2 that we established earlier on.index = alphabet2.indexOf(currChar); if (index != -1) { String keyedalphabet2 = keyedalphabet.toLowerCase(); char newChar = keyedalphabet2.charAt(index); encrypted.setCharAt(q, newChar); }Step 6: return result.Now, we have completed the For loop..All we have left is to exit it and return the String.public String Encryption(String input, int key){ StringBuilder encrypted = new StringBuilder(input); String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String alphabet2 = alphabet.toLowerCase(); String keyedalphabet = alphabet.substring(key) + alphabet.substring(0, key); for (int q = 0; q < encrypted.length(); q++) { char currChar = encrypted.charAt(q); int index = alphabet.indexOf(currChar); if (index != -1) { char newChar = keyedalphabet.charAt(index); encrypted.setCharAt(q, newChar); } index = alphabet2.indexOf(currChar); if (index != -1) { String keyedalphabet2 = keyedalphabet.toLowerCase(); char newChar = keyedalphabet2.charAt(index); encrypted.setCharAt(q, newChar); } } return encrypted }Step 7: Debug.But wait!.That won’t work!.encrypted is not a String, it is a StringBuilder and this function specifically requires a String to be returned!Luckily, there is a very simple function to remedy this oversight.public String Encryption(String input, int key){ StringBuilder encrypted = new StringBuilder(input); String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String alphabet2 = alphabet.toLowerCase(); String keyedalphabet = alphabet.substring(key) + alphabet.substring(0, key); for (int q = 0; q < encrypted.length(); q++) { char currChar = encrypted.charAt(q); int index = alphabet.indexOf(currChar); if (index != -1) { char newChar = keyedalphabet.charAt(index); encrypted.setCharAt(q, newChar); } index = alphabet2.indexOf(currChar); if (index != -1) { String keyedalphabet2 = keyedalphabet.toLowerCase(); char newChar = keyedalphabet2.charAt(index); encrypted.setCharAt(q, newChar); } } return encrypted.toString(); }That is how you get the encrypted version of your original sentence..Try it for yourself!Thank you for reading!. More details

Leave a Reply