encryption - java caesar cipher code -
i did caesar cipher code java runs doesnt encrypt after user enter key !
here code
public class caesarcipher { public static final string alphabet = "abcdefghijklmnopqrstuvwxyz"; public static string encrypt(string plaintext, int shiftkey) { plaintext = plaintext.tolowercase(); string ciphertext = ""; (int = 0; < 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 = ""; (int = 0; < 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 string encryption: "); string message = new string(); message = sc.next(); system.out.println(encrypt(message, 3)); system.out.println(decrypt(encrypt(message, 3), 3)); sc.close(); } }
run:
enter plain text: reem la enter key: 2 cipher text
using indexof
not efficient... can integer arithmetic on char
values indices.
i included comments in code explain more, came with.
public class caesarcipher { // rotate character k-positions public static char cipher(char c, int k) { // declare helping constants final int alphalength = 26; final char asciishift = character.isuppercase(c) ? 'a' : 'a'; final int ciphershift = k % alphalength; // shift down 0..25 a..z char shifted = (char) (c - asciishift); // rotate letter , handle "wrap-around" negatives , value >= 26 shifted = (char) ((shifted + ciphershift + alphalength) % alphalength); // shift english characters return (char) (shifted + asciishift); } // rotate string k-positions public static string cipher(string s, int k) { stringbuilder sb = new stringbuilder(); (int = 0; < s.length(); i++) { sb.append(cipher(s.charat(i), k)); } return sb.tostring(); } public static void main(string[] args) { scanner keyboard = new scanner(system.in); string password; int key; system.out.print("please enter password: "); password = keyboard.nextline(); { system.out.print("please enter key between 1-25: "); key = keyboard.nextint(); if (key < 1 || key > 25) { system.out.printf(" key must between 1 , 25, entered %d.\n", key); } } while (key < 1 || key > 25); system.out.println("password:\t" + password); string encryption = cipher(password, key); system.out.println("encrypted:\t" + encryption); system.out.println("decrypted:\t" + cipher(encryption, -key)); } }
the output should
please enter password: abcdefghijklmnopqrstuvwxyz please enter key between 1-25: 1 password: abcdefghijklmnopqrstuvwxyz encrypted: bcdefghijklmnopqrstuvwxyza decrypted: abcdefghijklmnopqrstuvwxyz
Comments
Post a Comment