RSA Encryption Dengan JAVA (Private dan Public Key)

Bismillah,

Pada kesempatan kali ini penulis akan membuat tutorial pembuatan metode enkripsi dengan JAVA yang menggunakan keys (untuk RSA menggunakan sertifikat : RSA Sertifikat ).

Buat terlebih dahulu folder : C:/RSA_Dengan_JAVA (silahkan dengan nama apapun dan di direktori manapun)

Buat sebuah kelas : Silahkan namakan sendiri, dikarenakan penulis menggunakan metode static yang tidak perlu adanya inisialisai kelas. Dan hanya dikerjakan di satu kelas.

Buat inisialisasi final string untuk menyimpan file dan pemanggilan RSA

/**
* Nama Algoritma.
*/
private static final String ALGORITHM = "RSA";
/**
* Path private keys.
*/
private static final String PRIVATE_KEY_FILE = "C:/RSA_Dengan_JAVA/private.key";
/**
* Path publoic keys.
*/
private static final String PUBLIC_KEY_FILE = "C:/RSA_Dengan_JAVA/public.key";


Buat method untuk generate keys

private static void membuatKey() {
try {
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
keyGen.initialize(1024);
final KeyPair key = keyGen.generateKeyPair();

File privateKeyFile = new File(PRIVATE_KEY_FILE);
File publicKeyFile = new File(PUBLIC_KEY_FILE);

// Membuat direktori file public and private key
if (privateKeyFile.getParentFile() != null) {
privateKeyFile.getParentFile().mkdirs();
}
privateKeyFile.createNewFile();

if (publicKeyFile.getParentFile() != null) {
publicKeyFile.getParentFile().mkdirs();
}
publicKeyFile.createNewFile();

// SMenyimpan Public key in a file
ObjectOutputStream publicKeyOS = new ObjectOutputStream(
new FileOutputStream(publicKeyFile));
publicKeyOS.writeObject(key.getPublic());
publicKeyOS.close();

// SMenyimpan Private key in a file
ObjectOutputStream privateKeyOS = new ObjectOutputStream(
new FileOutputStream(privateKeyFile));
privateKeyOS.writeObject(key.getPrivate());
privateKeyOS.close();
} catch (Exception e) {
e.printStackTrace();
}
}

Buat method tambahan untuk pengecekan keys

private static boolean ketersediaanKey() {

File privateKey = new File(PRIVATE_KEY_FILE);
File publicKey = new File(PUBLIC_KEY_FILE);

if (privateKey.exists() && publicKey.exists()) {
return true;
}
return false;
}

Method Encrypt

private static byte[] encrypt(String text, PublicKey key) {
byte[] cipherText = null;
try {
// Inisialisasi RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// encrypt object menggunakan public key
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(text.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return cipherText;
}

Method Decrypt

private static String decrypt(byte[] text, PrivateKey key) {
byte[] dectyptedText = null;
try {
// Inisialisasi RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);

// decrypt object menggunakan private key
cipher.init(Cipher.DECRYPT_MODE, key);
dectyptedText = cipher.doFinal(text);
} catch (Exception ex) {
ex.printStackTrace();
}

return new String(dectyptedText);
}

Tinggal di jalankan

public static void main(String[] args) {

try {
// Mengecek ketersediaan key
if (ketersediaanKey()) {
membuatKey();
}

final String originalText = "Harry Kurniawan - IK 342";
ObjectInputStream ois = null;

// Encrypt object menggunakan public key
ois = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
final PublicKey publicKey = (PublicKey) ois.readObject();
final byte[] cipherText = encrypt(originalText, publicKey);

// Decrypt object menggunakan private key.
ois = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
final PrivateKey privateKey = (PrivateKey) ois.readObject();
final String plainText = decrypt(cipherText, privateKey);

// Printing the Original, Encrypted and Decrypted Text
System.out.println("Object Asli: " + originalText);
System.out.println("Encrypted Object: " + new String(cipherText));
System.out.println("Decrypted Object: " + plainText);

} catch (Exception e) {
e.printStackTrace();
}
}

Sekian, semoga bermanfaat🙂

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