0% found this document useful (0 votes)
51 views

Blowfish Pake Java Netbean

This document contains Java code that implements symmetric key encryption and decryption of text messages using the Blowfish algorithm. It generates a random symmetric key, encrypts user-input text with that key, then decrypts and displays the original text. The key generation, encryption, and decryption methods are in a BEnkripsi class, and a main method instantiates that class to test the encryption/decryption workflow.

Uploaded by

me men
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Blowfish Pake Java Netbean

This document contains Java code that implements symmetric key encryption and decryption of text messages using the Blowfish algorithm. It generates a random symmetric key, encrypts user-input text with that key, then decrypts and displays the original text. The key generation, encryption, and decryption methods are in a BEnkripsi class, and a main method instantiates that class to test the encryption/decryption workflow.

Uploaded by

me men
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package enkripsi;
import javax.swing.*;
import javax.crypto.*;
import java.security.SecureRandom;
import java.util.Random;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author Deham
*/
class BEnkripsi{
byte[] skey = new byte [1000];
String skeyString;
static byte [] raw;
String inputPesan, enkripsiData, Deskripsipesan;

public BEnkripsi() {
try {
generateSymmetricKey();
inputPesan = JOptionPane.showInputDialog(null, "Masukan text untuk di
Enskripsi");
byte[] ibyte = inputPesan.getBytes();
byte[] ebyte = encrypt(raw, ibyte);
String enkripsiData = new String (ebyte);
System.out.println("Pesan Ter-Enskripsi"+enkripsiData);
JOptionPane.showMessageDialog(null, "Data Ter-Enkripsi" +
"\n"+enkripsiData);

byte[] dbyte = decrypt(raw, ebyte);


String Deskripsipesan = new String(dbyte);
System.out.println("Pesan Ter-Deskripsi"+Deskripsipesan);

JOptionPane.showMessageDialog(null, "Data di
Deskripsi"+"\n"+Deskripsipesan);
} catch (Exception e) {
System.out.println(e);
}
}
void generateSymmetricKey(){
try {
Random r = new Random();
int num = r.nextInt(10000);
String knum = String.valueOf(num);
byte[] knumb = knum.getBytes();
skey = getrawkey(knumb);
skeyString = new String(skey);
System.out.println("Blowfish Symmetric key = "+skeyString);
} catch (Exception e) {
System.out.println(e);
}
}
private static byte[] getrawkey(byte[] seed ) throws Exception{
KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(120, sr);
SecretKey skey = kgen.generateKey();
raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear ) throws Exception{
SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted ) throws Exception{
SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}

}
public class Enkripsi {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
BEnkripsi bf = new BEnkripsi();
}

You might also like