I want to create a java program that can be encrypt-decrypt alphabet from characters . Which is the shifting of the alphabet is starting from the the first character to increase 4 and 5 increased in the second character, increasing 6 at the third character, and so on..
Then when one of the characters increasement is more than the ascii code of 'z', that character will decrease from the variable i from the loop. (Sorry my english isn't good enough)
This is the output of the program that i want:
Encryption:
Plain text: "abcz"
Cipher text: "egiz"
Decryption:
Cipher text: "egiz"
Plain text: "abcz"
This is my code for now:
package -; public class - { public static void main(String[] args) { String enkripsi = "abcz".toLowerCase(); String dekripsi = "egiz".toLowerCase(); System.out.println("============================"); System.out.println("\tENKRIPSI"); System.out.println("============================"); System.out.println("Plain text: "); System.out.println(enkripsi); System.out.println(""); System.out.println("Cipher text:"); enkrip(enkripsi); System.out.println(""); System.out.println("============================"); System.out.println(""); System.out.println("============================"); System.out.println("\t DEKRIPSI"); System.out.println("============================"); System.out.println("Cipher text:"); System.out.println(dekripsi); System.out.println(""); System.out.println("Plain text:"); dekrip(dekripsi); System.out.println(""); System.out.println("============================"); System.out.println(""); System.out.println("============================"); } public static void enkrip(String enkripsi) { int a[] = new int[enkripsi.length()]; for (int i = 0; i < a.length; i++) { a[i] = enkripsi.charAt(i) + 4 + i; if (a[i] >= 0 && a[i] <= 96 || a[i] > 122 && a[i] <= 255 ) { a[i] = enkripsi.charAt(i) - 1; a[i] = enkripsi.charAt(i) - (i % 26); if (a[i] < (byte) 'a') { a[i] = enkripsi.charAt(i) + (i % 26); } else if (a[i] > (byte) 'z') { a[i] = enkripsi.charAt(i) + (i % 26); } } System.out.print((char) a[i]); } } public static void dekrip(String dekripsi) { int b[] = new int[dekripsi.length()]; for (int i = 0; i < b.length; i++) { b[i] = dekripsi.charAt(i) - 4 - i; if (b[i] >= 0 && b[i] <= 96 || b[i] > 122 && b[i] <= 255) { b[i]= dekripsi.charAt(i)-1; } if (b[i] < (byte) 'z') { b[i] = dekripsi.charAt(i) + (i%26) ; if (b[i] < (byte) 'a') { b[i] = (i % 26) - dekripsi.charAt(i); } else if (b[i] > (byte) 'z') { b[i] = (i % 26) - dekripsi.charAt(i); } } System.out.print((char) b[i]); } } }
What's wrong with my code? It won't work. Specially in the decryption . Please help me...