You are on page 1of 6

NAMA : HAFID AKBAR FIKRI

NIM : 18.01.53.0028
Klp : B1
TUGAS KRIPTOGRAFI B1 PROGRAM JAVA
1. Tentukan chipherteks dari plainteks: UNIVERSITAS STIKUBANK SEMARANG
dengan menggunakan Subtitusi Caesar Chiper dengan kunci = 4
Jawab :

import java.util.*;
public class filename {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Masukkan Teks Java: ");
String plaintext = sc.nextLine();
System.out.print("Masukkan Shift: ");
int shift = sc.nextInt();
String ciphertext = "";
char alphabet;
for(int i=0; i < plaintext.length();i++)
{
alphabet = plaintext.charAt(i);

if(alphabet >= 'A' && alphabet <= 'Z') {

alphabet = (char) (alphabet + shift);

if(alphabet > 'Z') {

alphabet = (char) (alphabet-26);

ciphertext = ciphertext + alphabet;


}

else {

ciphertext = ciphertext + alphabet;

}
}
System.out.println("Ciphertext: " + ciphertext);
}
}
2. Tentukan chipherteks dari plainteks : HAFID AKBAR FIKRI dengan menggunakan
Cipher Transposisi dengan kunci = 4
Jawab :

import java.util.*;
class filename2 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Plainteks : ");
String pl = sc.nextLine();
String demo = "";
String s = "";
int start = 0;
for (int i = 0; i < pl.length(); i++) {
if (pl.charAt(i) == ' ') {
s = s + pl.substring(start, i);
start = i + 1;
}
}
s = s + pl.substring(start);

int k = s.length();
int l = 0;
int col = 4; //Key
int row = s.length() / col;
char ch[][] = new char[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (l < k) {
ch[i][j] = s.charAt(l);
l++;
} else {
ch[i][j] = '#';
}
}
}

char trans[][] = new char[col][row];


for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
trans[j][i] = ch[i][j];
}
}
System.out.print("Cipherteks : ");

for (int i = 0; i < col; i++) {


for (int j = 0; j < row; j++) {
System.out.print(trans[i][j]);
}
}
//display
System.out.println();
}
}
3. Buatlah proses enksripsi dan deskripsi plainteks berikut ini dengan menggunakan Cipher
Vigenere (kunci bebas):

Plainteks : Semarang Kota Tercintaku

Kunci : HAFID

Jawab :

public class filename3


{
public 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;
}

public 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;
}

public static void main(String[] args)


{
String key = "HAFID";
String message = "SEMARANG KOTA TERCINTAKU";
String encryptedMsg = encrypt(message, key);
System.out.println("String: " + message);
System.out.println("Key: " + key);
System.out.println("Encrypted message: " + encryptedMsg);
System.out.println("Decrypted message: " + decrypt(encryptedMsg, key));
}
}

You might also like