Dalam kriptografi, cipher transposisi adalah metode
enkripsi dimana posisi yang dimiliki oleh unit plaintext (yang umumnya karakter
atau kelompok karakter) dialihkan sesuai dengan sistem yang teratur, sehingga
ciphertext merupakan permutasi dari plaintext. Artinya, urutan unit berubah
(plaintext yang diatur ulang / kembali). Secara matematis fungsi bijektif
digunakan pada posisi karakter untuk mengenkripsi dan fungsi invers untuk
mendekripsi.

Dalam columnar transposisi cipher biasa, spasi cadang
diisi dengan nulls, artinya, spasi dihilangkan sehingga kalimat menjadi sedikit
susah dibaca. Akhirnya, pesan dibaca sesuai urutan kolom, dalam urutan yang
ditentukan oleh kata kunci. Misalnya, kita menggunakan ZEBRAS kata kunci dan
pesan WE ARE DISCOVERED. FLEE AT ONCE. Kata tersebut menjadi : WEAREDISCOVEREDFLEEATONCEQKJEU.
Kaliamat ini yang akan dimasukan dalam code java. Kalimat ini merupakan hasil
dari kalimat asli , dikurangi spasi dan ditambah QKJEU supaya jumlahnya
kelipatan dari 6. QKJEU bisa diganti apa saja sesuai keinginan pembuat kalimat.
Contoh dalam java, seperti dalam code berikut ini :
import java.awt.event.*;
import java.util.*;
import java.util.*;
public class transpositionCipher
{
public static void main(String args[])
{
String key;
String message;
String encryptedMessage;
// Letters in the x-axis
int x=0;
// Letters in the y-axis
int y=0;
key = "ZEBRAS";
message = "WEAREDISCOVEREDFLEEATONCEQKJEU";
encryptedMessage = "";
{
public static void main(String args[])
{
String key;
String message;
String encryptedMessage;
// Letters in the x-axis
int x=0;
// Letters in the y-axis
int y=0;
key = "ZEBRAS";
message = "WEAREDISCOVEREDFLEEATONCEQKJEU";
encryptedMessage = "";
// To set the temp as [x][y]
char temp[][]=new char [key.length()][message.length()];
char msg[] = message.toCharArray();
// To populate the array
x=0;
y=0;
// To convert the message into an array of char
for (int i=0; i< msg.length;i++)
{
temp[x][y]=msg[i];
if (x==(key.length()-1))
{
x=0;
y=y+1;
} // Close if
else
{
x++;
}
} // Close for loop
char temp[][]=new char [key.length()][message.length()];
char msg[] = message.toCharArray();
// To populate the array
x=0;
y=0;
// To convert the message into an array of char
for (int i=0; i< msg.length;i++)
{
temp[x][y]=msg[i];
if (x==(key.length()-1))
{
x=0;
y=y+1;
} // Close if
else
{
x++;
}
} // Close for loop
// To sort the key
char t[]=new char [key.length()];
t=key.toCharArray();
Arrays.sort(t);
for (int j=0;j<y;j++)
{
for (int i=0;i<key.length();i++)
{
System.out.print(temp[i][j]);
}
System.out.println();
}
char t[]=new char [key.length()];
t=key.toCharArray();
Arrays.sort(t);
for (int j=0;j<y;j++)
{
for (int i=0;i<key.length();i++)
{
System.out.print(temp[i][j]);
}
System.out.println();
}
System.out.println();
// To print out row by row (i.e. y)
for (int j=0;j<y;j++){
// To compare the the sorted Key with the key
// For char in the key
for (int i=0;i<key.length();i++){
int pos=0;
// To get the position of key.charAt(i) from sorted key
for (pos=0;pos<t.length;pos++){
if (key.charAt(i)==t[pos]){
// To break the for loop once the key is found
break;
}
}
System.out.print(temp[pos][j]);
encryptedMessage+=temp[pos][j];
}
System.out.println();
}
for (int j=0;j<y;j++){
// To compare the the sorted Key with the key
// For char in the key
for (int i=0;i<key.length();i++){
int pos=0;
// To get the position of key.charAt(i) from sorted key
for (pos=0;pos<t.length;pos++){
if (key.charAt(i)==t[pos]){
// To break the for loop once the key is found
break;
}
}
System.out.print(temp[pos][j]);
encryptedMessage+=temp[pos][j];
}
System.out.println();
}
System.out.println(encryptedMessage);
System.exit(0);
}
}
System.exit(0);
}
}
Sehingga setelah dikenai fungsi
cipher tersebut, kalimat menjadi “EVLNA CDTES EAROF ODEEC WIREE
No comments:
Post a Comment
Silakan berkomentar ....