RC4 Encryption


Berikut ini adalah source code sederhana untuk enkripsi RC4. Source code ini dibuat menggunakan bahasa pemrograman Java.


import java.io.*;
//import java.lang.Character;
/**
*
* @author zlich
*/
public class RC4 {
public static void main (String [] args) throws IOException {
int [] key = null;
int keylen = 0;
int k = 0;
int [] SBox = new int [256];

DataInputStream input = new DataInputStream(System.in);

//inisialisasi S-Box
for (int i = 0; i 0) {
key = new int [keylen];
}
for (k=0; k<keylen; k++) {
key[k] = kunci.charAt(k);

System.out.print(key[k] + " ");
}

//permutasi S-Box
int i, j = 0;
for (i=0; i<256; i++) {
j = (j + SBox[i] + key[i % key.length]) % 256;

//swap
int temp = SBox[i];
SBox[i] = SBox[j];
SBox[j] = temp;
}

//Input Plaintext
System.out.println("\nPlaintext: ");
String plaintext = input.readLine();

//PRGA
int a = 0;
int b = 0;
for (i=0; i<plaintext.length(); i++) {
a = (a + 1) % 256;
b = (b + SBox[a]) % 256;

//swap
int temp = SBox[a];
SBox[a] = SBox[b];
SBox[b] = temp;

//pseudorandom generation
int pseudo = SBox[(SBox[a] + SBox[b]) % 256];
int chartext = plaintext.charAt(i);

//XOR
int chiper = pseudo ^ chartext;
System.out.print(Integer.toHexString(chiper).toUpperCase()); //chipertext
//System.out.print(Character.toChars(chiper));
}
}
}

Source codenya masih menggunakan prosedural, untuk pendekatan OOP sedang diusahakan.

Kalau dicompile terus dieksekusi outputnya kayak gini:
Key:
pwd12
112 119 100 49 50
Plaintext:
Math 310 Proves!
6CA86FE3CBC33C162595C3E78B9C97BC

Selamat Mencoba!!

5 thoughts on “RC4 Encryption

  1. kk, kok dari code diatas saya ada error di bag. for paling atas…
    sama yg nama var nya “kunci ” di fungsi for yg kedua,,
    itu kira2 knapa ya??

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s