Java-KriptografiKlasik
Berbagai Program Kriptografi Klasik
Caesar Cipher
Program : CaesarCipher.java
import java.util.Scanner;
/**
* Program CaesarCipher
*/
public class CaesarCipher {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
String key;
int keyLength;
System.out.println("Enter message: ");
str = sc.nextLine();
System.out.println("Enter encryption key: ");
key = sc.next();
keyLength = Integer.parseInt(key);
// Pengulangan terus-menerus dari pilihan 'Enrypt' and 'Decrypt'
for (;;) {
System.out.println("1.Encrypt\n2.Decrypt\n3.Exit...");
int choice = sc.nextInt();
switch (choice) {
case 1:
/*
* send input string keyLength to encrypt() method to encrypt it returns
* 'Encrypted' string
*/
System.out.println("Encrypted message..." + encrypt(str, keyLength));
break;
case 2:
// send retrived string from encrypt() method and keyLength to decrypt() method
// it returns 'Decrypted' string
System.out.println("Decryptedmessage..." + decrypt(encrypt(str, keyLength),
keyLength));
break;
case 3:
// exit from the program
System.exit(0);
sc.close();
break;
default:
System.out.println("Invalid option..");
}
}
}
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;
}
public static String decrypt(String str, int keyLength) {
String decrypted = "";
for (int i = 0; i < str.length(); i++) {
// stores ascii value of character in the string at index 'i'
int c = str.charAt(i);
// decryption logic for uppercase letters
if (Character.isUpperCase(c)) {
c = c - (keyLength % 26);
// if c value deceed the ascii value of 'A' increase it by adding 26(no.of
// alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if (c < 'A')
c = c + 26;
}
// decryption logic for uppercase letters
else if (Character.isLowerCase(c)) {
c = c - (keyLength % 26);
// if c value deceed the ascii value of 'A' increase it by adding 26(no.of
// alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if (c < 'a')
c = c + 26;
}
// concatinate the decrypted characters/strings
decrypted = decrypted + (char) c;
}
return decrypted;
}
}
Program CaesarCipherProgram.java
import java.util.Scanner; Page 2 of 7
Java-KriptografiKlasik
import java.util.Scanner;
/**
* CaesarCipherProgram
*
*/
public class CaesarCipherProgram {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("1.Encrypt\n2.Decrypt\n3.Exit...");
int choice = input.nextInt();
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..");
}
}
}
// Fungsi untuk melakukan enkripsi metode Caesar
public static void Eknripsi() {
Scanner sc = new Scanner(System.in);
System.out.println("Masukkan pesan plaintext : ");
String plaintext = sc.nextLine();
System.out.println("Masukkan nilai dimana karakter plaintext akan di-shifted : ");
int shift = sc.nextInt();
String ciphertext = "";
char alphabet;
for (int i = 0; i < plaintext.length(); i++) {
// Shift one character at a time
alphabet = plaintext.charAt(i);
// if alphabet lies between a and z
if (alphabet >= 'a' && alphabet <= 'z') {
// shift alphabet
alphabet = (char) (alphabet + shift);
// if shift alphabet greater than 'z'
if (alphabet > 'z') {
// reshift to starting position
alphabet = (char) (alphabet + 'a' - 'z' - 1);
}
ciphertext = ciphertext + alphabet;
}
Page 3 of 7
Java-KriptografiKlasik
// if alphabet lies between 'A'and 'Z'
else if (alphabet >= 'A' && alphabet <= 'Z') {
// shift alphabet
alphabet = (char) (alphabet + shift);
// if shift alphabet greater than 'Z'
if (alphabet > 'Z') {
// reshift to starting position
alphabet = (char) (alphabet + 'A' - 'Z' - 1);
}
ciphertext = ciphertext + alphabet;
} else {
ciphertext = ciphertext + alphabet;
}
}
System.out.println("ciphertext : " + ciphertext);
}
// Fungsi untuk melakukan Dekripsi metode Caesar
public static void Dekripsi() {
Scanner sc = new Scanner(System.in);
System.out.println("Masukkan pesan ciphertext : ");
String ciphertext = sc.nextLine();
System.out.println("Masukkan nilai shift : ");
int shift = sc.nextInt();
String decryptMessage = "";
for (int i = 0; i < ciphertext.length(); i++) {
// Shift one character at a time
char alphabet = ciphertext.charAt(i);
// if alphabet lies between a and z
if (alphabet >= 'a' && alphabet <= 'z') {
// shift alphabet
alphabet = (char) (alphabet - shift);
// shift alphabet lesser than 'a'
if (alphabet < 'a') {
// reshift to starting position
alphabet = (char) (alphabet - 'a' + 'z' + 1);
}
decryptMessage = decryptMessage + alphabet;
}
// if alphabet lies between A and Z
else if (alphabet >= 'A' && alphabet <= 'Z') {
// shift alphabet
alphabet = (char) (alphabet - shift);
// shift alphabet lesser than 'A'
if (alphabet < 'A') {
// reshift to starting position
alphabet = (char) (alphabet - 'A' + 'Z' + 1);
}
decryptMessage = decryptMessage + alphabet;
} else {
decryptMessage = decryptMessage + alphabet;
}
}
Page 4 of 7
Java-KriptografiKlasik
System.out.println("decrypt message : " + decryptMessage);
}
}
Rot13
Program Rot13.java
import java.io.*;
/**
* Rot13
*
*/
public class Rot13 {
public static void main(String[] args) throws IOException {
if (args.length >= 1) {
for (String file : args) {
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
rot13(in, System.out);
}
}
} else {
rot13(System.in, System.out);
}
}
private static void rot13(InputStream in, OutputStream out) throws IOException {
int ch;
while ((ch = in.read()) != -1) {
out.write(rot13((char) ch));
}
}
private static char rot13(char ch) {
if (ch >= 'A' && ch <= 'Z') {
return (char) (((ch - 'A') + 13) % 26 + 'A');
}
if (ch >= 'a' && ch <= 'z') {
return (char) (((ch - 'a') + 13) % 26 + 'a');
}
return ch;
}
}
Subtitution Cipher
Program SubtitutionCipher.java
Page 5 of 7
Java-KriptografiKlasik
/**
* SubtitutionCipher
*/
public class SubtitutionCipher {
final static String key = "]kYV}(!7P$n5_0i R:?jOWtF/=-pe'AD&@r6%ZXs\"v*N"
+ "[#wSl9zq2^+g;LoB`aGh{3.HIu4fbK)mU8|dMET><,Qc\\C1yxJ";
static String text = "Here we have to do is there will be a input/source "
+ "file in which we are going to Encrypt the file by replacing every "
+ "upper/lower case alphabets of the source file with another "
+ "predetermined upper/lower case alphabets or symbols and save "
+ "it into another output/encrypted file and then again convert "
+ "that output/encrypted file into original/decrypted file. This "
+ "type of Encryption/Decryption scheme is often called a "
+ "Substitution Cipher.";
public static void main(String[] args) {
String enc = encode(text);
System.out.println("Encoded: " + enc);
System.out.println("\nDecoded: " + decode(enc));
}
static String encode(String s) {
StringBuilder sb = new StringBuilder(s.length());
for (char c : s.toCharArray())
sb.append(key.charAt((int) c - 32));
return sb.toString();
}
static String decode(String s) {
StringBuilder sb = new StringBuilder(s.length());
for (char c : s.toCharArray())
sb.append((char) (key.indexOf((int) c) + 32));
return sb.toString();
}
}
Vigenere Cipher
Program VigenereCipher.java
Page 6 of 7
Java-KriptografiKlasik
/**
* VigenereCipher
*/
public class VigenereCipher {
public static void main(String[] args) {
String key = "VIGENERECIPHER";
String ori = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
String enc = encrypt(ori, key);
System.out.println(enc);
System.out.println(decrypt(enc, key));
}
static String encrypt(String text, final String key) {
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < 'A' || c > 'Z')
continue;
res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
j = ++j % key.length();
}
return res;
}
static String decrypt(String text, final String key) {
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < 'A' || c > 'Z')
continue;
res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
j = ++j % key.length();
}
return res;
}
}
Page 7 of 7