Berbagai Program Kriptografi Klasik: Caesar Cipher
Berbagai Program Kriptografi Klasik: Caesar Cipher
Caesar Cipher
Program : CaesarCipher.java
import java.util.Scanner;
/**
* Program CaesarCipher
*/
Page 1 of 7
Java-KriptografiKlasik
public static String encrypt(String str, int keyLength) {
String encrypted = "";
for (int i = 0; i < str.length(); i++) {
// stores ascii value of character in the string at index 'i'
int c = str.charAt(i);
// encryption logic for uppercase letters
if (Character.isUpperCase(c)) {
c = c + (keyLength % 26);
// if c value exceeds the ascii value of 'Z' reduce it by subtracting 26(no.of
// alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if (c > 'Z')
c = c - 26;
}
// encryption logic for lowercase letters
else if (Character.isLowerCase(c)) {
c = c + (keyLength % 26);
// if c value exceeds the ascii value of 'z' reduce it by subtracting 26(no.of
// alphabets) to keep in boundaries of ascii values of 'a' and 'z'
if (c > 'z')
c = c - 26;
}
// concatinate the encrypted characters/strings
encrypted = encrypted + (char) c;
}
return encrypted;
}
Program CaesarCipherProgram.java
/**
* CaesarCipherProgram
*
*/
public class CaesarCipherProgram {
switch (choice) {
case 1:
// Masuk ke fungsi Enkripsi
Eknripsi();
break;
case 2:
// Masuk ke fungsi Dekripsi
Dekripsi();
break;
case 3:
// exit from the program
System.exit(0);
break;
default:
System.out.println("Invalid option..");
}
}
}
}
System.out.println("ciphertext : " + ciphertext);
}
Rot13
Program Rot13.java
import java.io.*;
/**
* Rot13
*
*/
public class Rot13 {
Subtitution Cipher
Program SubtitutionCipher.java
Page 5 of 7
Java-KriptografiKlasik
/**
* SubtitutionCipher
*/
public class SubtitutionCipher {
return sb.toString();
}
return sb.toString();
}
}
Vigenere Cipher
Program VigenereCipher.java
Page 6 of 7
Java-KriptografiKlasik
/**
* VigenereCipher
*/
public class VigenereCipher {
Page 7 of 7