publicclassShiftAlphabetEncrypt{ publicstatic Scanner scan = new Scanner(System.in); publicstatic String alph = "abcdefghijklmnopqrstuvwxyz"; //Method that will shift the alphabet with keys what you entered in main method. static String[] shiftAlphabet (int[] key){ String[] alphShif = {alph.substring(key[0])+alph.substring(0,key[0]), alph.substring(key[1])+alph.substring(0,key[1])}; return alphShif; } //Method that encrypt/decrypt the text that you entered with keys in main method. publicstatic String encrypt(String text, String[] alphShif, int[] key){ StringBuilder t = new StringBuilder(""); int count = 0; boolean oddEven = true; for (int i=0, n=text.length(); i<n; ++i){ //If character not letter, nothing change. if (!(Character.isLetter(text.charAt(i)))) { t.append(text.charAt(i)); continue; } ++count; //If character is not "not letter". for (int m=0, o=alph.length(); m<o; ++m){ //See is this letter in odd or even position (count only letters). if (count%2==1) oddEven=true; else oddEven=false; //Find the original position of this letter. if ((Character.toLowerCase(text.charAt(i))) == alph.charAt(m)){ //If this letter is in upper case or not in upper case. if (Character.isUpperCase(text.charAt(i))) //Change this letter to the number in same position of new alphabet. t.append(Character.toUpperCase(alphShif[oddEven?0:1].charAt(m))); else t.append(alphShif[oddEven?0:1].charAt(m)); } } } //Return encrypted/decrypted text back. return t.toString(); } //Main method. publicstaticvoidmain(String[] args){ System.out.println("\n\n\nPlease enter the text that you want to encrypt:"); String text = scan.nextLine(); int[] key = newint[2]; System.out.println("\nPlease enter the key of odd letters\n(positive integer):"); key[0] = scan.nextInt(); System.out.println("\nPlease enter the key of even letters\n(positive integer):"); key[1] = scan.nextInt(); scan.close(); System.out.println(encrypt(text, shiftAlphabet(key), key)); } }
publicclassShiftAlphabetDecrypt{ privatestatic Scanner scan = new Scanner(System.in); privatestatic String alph = "abcdefghijklmnopqrstuvwxyz"; //Method that will shift the encrypted alphabet back //with keys what you entered in main method. static String[] shiftAlphabet (int[] key){ String[] alphShif = {alph.substring(alph.length()-key[0])+ alph.substring(0, alph.length()-key[0]), alph.substring(alph.length()-key[1])+ alph.substring(0, alph.length()-key[1])}; return alphShif; } //Main method. publicstaticvoidmain(String[] args){ System.out.println("\n\n\nPlease enter the encrypted text:"); String text = scan.nextLine(); int[] key = newint[2]; System.out.println("\nPlease enter the key of odd letters\n(positive integer):"); key[0] = scan.nextInt(); System.out.println("\nPlease enter the key of even letters\n(positive integer):"); key[1] = scan.nextInt(); scan.close(); System.out.println(ShiftAlphabetEncrypt.encrypt(text, shiftAlphabet(key), key)); } }
输出:
加密:
Please enter the text that you want to encrypt: Je m’appelle alittlebear!!
Please enter the key of odd letters (positive integer): 6
Please enter the key of even letters (positive integer): 16 Pu s’qvfkbru gbojzbkrkqx!!
解密:
Please enter the encrypted text: Pu s’qvfkbru gbojzbkrkqx!!
Please enter the key of odd letters (positive integer): 6
Please enter the key of even letters (positive integer): 16 Je m’appelle alittlebear!!
// Method that will shift the alphabet with keys what you entered in main method. static String[] shiftAlphabetEncrypt(int[] keys) { String[] alphaShifted = { alphabet.substring(keys[0]) + alphabet.substring(0, keys[0]), alphabet.substring(keys[1]) + alphabet.substring(0, keys[1])};
return alphaShifted; }
// Method that encrypt/decrypt the text that you entered with keys in main method. publicstatic String encryptAndDecrypt(String text, String[] alphaShifted, int[] keys){ StringBuilder textSB = new StringBuilder(""); int count = 0; boolean oddEven = true; char textCA;
for (int i = 0, a = text.length(); i < a; ++i) { textCA = text.charAt(i);
// If character not letter, nothing change. if (!(Character.isLetter(textCA))) { textSB.append(textCA); continue; }
++count; // See is this letter in odd or even position (count only letters). if (count % 2 == 1) oddEven = true; else oddEven = false;
// If this character is not "not letter" (is letter). for (int b = 0, c = alphabet.length(); b < c; ++b) {
// Find the original position of this letter. if ((Character.toLowerCase(textCA)) == alphabet.charAt(b))
// Change this letter to the number in same position of new alphabet. textSB.append(Character.isUpperCase(textCA) ? //Ask is this letter in upper case? (Character.toUpperCase(alphaShifted[oddEven ? 0 : 1].charAt(b))) : //Append an upper case letter. (alphaShifted[oddEven ? 0 : 1].charAt(b))); //Append a lower case letter (no change). } }
// Return encrypted/decrypted text back. return textSB.toString(); }
// Main method. publicstaticvoidmain(String[] args){ scan.reset(); System.out.println("\n\n\nPlease enter the text that you want to encrypt:"); String text = scan.nextLine(); int[] keys = newint[2]; System.out.println("\nPlease enter the keys of odd letters\n(positive integer):"); keys[0] = scan.nextInt(); System.out.println("\nPlease enter the keys of even letters\n(positive integer):"); keys[1] = scan.nextInt(); scan.close(); System.out.println(encryptAndDecrypt(text, shiftAlphabetEncrypt(keys), keys)); } }
publicclassShiftAlphabetDecrypt{ privatefinalstatic Scanner scan = ShiftAlphabetEncrypt.scan; privatefinalstatic String alph = ShiftAlphabetEncrypt.alphabet; //Method that will shift the encrypted alphabet back //with keys what you entered in main method. static String[] shiftAlphabetDecrypt (int[] keys){ String[] alphabetShifted = {alph.substring(alph.length()-keys[0])+ alph.substring(0, alph.length()-keys[0]), alph.substring(alph.length()-keys[1])+ alph.substring(0, alph.length()-keys[1])}; return alphabetShifted; } //Main method. publicstaticvoidmain(String[] args){ scan.reset(); System.out.println("\n\n\nPlease enter the encrypted text:"); String text = scan.nextLine(); int[] keys = newint[2]; System.out.println("\nPlease enter the keys of odd letters\n(positive integer):"); keys[0] = scan.nextInt(); System.out.println("\nPlease enter the keys of even letters\n(positive integer):"); keys[1] = scan.nextInt(); scan.close(); System.out.println(ShiftAlphabetEncrypt.encryptAndDecrypt(text, shiftAlphabetDecrypt(keys), keys)); } }
输出:
加密:
Please enter the text that you want to encrypt: WO SHI ALITTLEBEAR LOL!!
Please enter the keys of odd letters (positive integer): 3
Please enter the keys of even letters (positive integer): 7 ZV VOL HOPWAOLELDY OVO!!
解密:
Please enter the encrypted text: ZV VOL HOPWAOLELDY OVO!!
Please enter the keys of odd letters (positive integer): 3
Please enter the keys of even letters (positive integer): 7 WO SHI ALITTLEBEAR LOL!!
static String encryptKeys(int[] key){ StringBuilder keysLetters = new StringBuilder(""); for (int n=0; n<2; ++n){ keysLetters.append(alphabet.charAt(key[n])); } return keysLetters.toString(); }
// Method that will shift the alphabet with keys what you entered in main method. static String[] shiftAlphabetEncrypt(int[] keys) { String[] alphaShifted = { alphabet.substring(keys[0]) + alphabet.substring(0, keys[0]), alphabet.substring(keys[1]) + alphabet.substring(0, keys[1])};
return alphaShifted; }
// Method that encrypt/decrypt the text that you entered with keys in main method. publicstatic String encryptAndDecrypt(String text, String[] alphaShifted, int[] keys){ StringBuilder textSB = new StringBuilder(""); int count = 0; boolean oddEven = true; char textCA;
for (int i = 0, a = text.length(); i < a; ++i) { textCA = text.charAt(i);
// If character not letter, nothing change. if (!(Character.isLetter(textCA))) { textSB.append(textCA); continue; }
++count; // See is this letter in odd or even position (count only letters). if (count % 2 == 1) oddEven = true; else oddEven = false;
// If this character is not "not letter" (is letter). for (int b = 0, c = alphabet.length(); b < c; ++b) {
// Find the original position of this letter. if ((Character.toLowerCase(textCA)) == alphabet.charAt(b))
// Change this letter to the number in same position of new alphabet. textSB.append(Character.isUpperCase(textCA) ? //Ask is this letter in upper case? (Character.toUpperCase(alphaShifted[oddEven ? 0 : 1].charAt(b))) : //Append an upper case letter. (alphaShifted[oddEven ? 0 : 1].charAt(b))); //Append a lower case letter (no change). } }
// Return encrypted/decrypted text back. return textSB.toString(); }
// Main method. publicstaticvoidmain(String[] args){ scan.reset(); System.out.println("\n\n\nPlease enter the text that you want to encrypt:"); String text = scan.nextLine(); int[] keys = newint[2]; System.out.println("\nPlease enter the key of odd letters\n(positive integer):"); keys[0] = scan.nextInt(); System.out.println("\nPlease enter the key of even letters\n(positive integer):"); keys[1] = scan.nextInt(); scan.close(); System.out.println(encryptKeys(keys)+encryptAndDecrypt(text, shiftAlphabetEncrypt(keys), keys)); } }
publicclassEncrypt{ publicfinalstatic Random rand = new Random(); publicfinalstatic Scanner scan = new Scanner(System.in); publicfinalstatic String alphabet = "abcdefghijklmnopqrstuvwxyz";
//Encrypte keys to the start of the encrypted code. static String encryptKeys(int[] key){ String keysLetters = ""; for (int i=0; i<3; ++i) keysLetters += alphabet.charAt(key[i]); return keysLetters; }
// Method that will shift the alphabet with keys what you entered in main method. static String[] shiftAlphabetEncrypt(int[] keys) { String[] alphaShifted = { alphabet.substring(keys[0]) + alphabet.substring(0, keys[0]), alphabet.substring(keys[1]) + alphabet.substring(0, keys[1])};
return alphaShifted; }
// Method that encrypt/decrypt the text that you entered with keys in main method. publicstatic String encryptAndDecrypt(String text, String[] alphaShifted, int[] keys){ StringBuilder textSB = new StringBuilder(""); int count = 0; boolean oddEven = true; char textCA;
for (int i = 0, a = text.length(); i < a; ++i) { textCA = text.charAt(i);
// If character not letter, nothing change. if (!(Character.isLetter(textCA))) { textSB.append(textCA); continue; }
++count; // See is this letter in odd or even position (count only letters). if (count % 2 == 1) oddEven = true; else oddEven = false;
// If this character is not "not letter" (is letter). for (int b = 0, c = alphabet.length(); b < c; ++b) {
// Find the original position of this letter. if ((Character.toLowerCase(textCA)) == alphabet.charAt(b))
// Change this letter to the number in same position of new alphabet. textSB.append(Character.isUpperCase(textCA) ? //Ask is this letter in upper case? (Character.toUpperCase(alphaShifted[oddEven ? 0 : 1].charAt(b))) : //Append an upper case letter. (alphaShifted[oddEven ? 0 : 1].charAt(b))); //Append a lower case letter (no change). } }
// Return encrypted/decrypted text back. return textSB.toString(); }
// Main method. publicstaticvoidmain(String[] args){ //Get text. scan.reset(); System.out.println("\n\n\nPlease enter the text that you want to encrypt:"); String text = scan.nextLine(); scan.close(); //Get random keys. int[] keys = newint[3]; for(int i=0; i<3; ++i) keys[i] = rand.nextInt(alphabet.length()); //Output encrypted code. System.out.println(encryptKeys(keys)+encryptAndDecrypt(text, shiftAlphabetEncrypt(keys), keys)); } }