0% found this document useful (0 votes)
251 views3 pages

Source Code Aplikasi Kriptografi

This Java code defines a class called FromEnkripsi that provides an interface for encrypting, decrypting, and saving text files. It contains methods for encrypting text by shifting each character by a key, decrypting encrypted text by reversing the shift, saving encrypted text to a file, opening an encrypted file, and resetting the text fields. Swing components like buttons and text areas are used to build the graphical user interface.

Uploaded by

Imba Uday
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
251 views3 pages

Source Code Aplikasi Kriptografi

This Java code defines a class called FromEnkripsi that provides an interface for encrypting, decrypting, and saving text files. It contains methods for encrypting text by shifting each character by a key, decrypting encrypted text by reversing the shift, saving encrypted text to a file, opening an encrypted file, and resetting the text fields. Swing components like buttons and text areas are used to build the graphical user interface.

Uploaded by

Imba Uday
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package progenkripsi;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class FromEnkripsi extends javax.swing.JFrame {


private JFileChooser dialog = new JFileChooser();

public FromEnkripsi() {
initComponents();
}

@SuppressWarnings("unchecked")

private void btEnkripActionPerformed(java.awt.event.ActionEvent evt) {

String key = txtKey.getText();


String isitext = (key.length())+key+txtPlainText.getText();
String out = "";
for (int i=0; i<isitext.length(); i++){
int index = isitext.charAt(i);
char s = (char)(index+1);
out = out+String.valueOf(s);
}

if (txtKey.getText().equals("")){
JOptionPane.showMessageDialog(null,"Masukkan Key!");
}
else { txtChiperText.setText(out);
txtPlainText.setText("");
txtKey.setText("");
}

private void btSimpanActionPerformed(java.awt.event.ActionEvent evt) {

int isi = dialog.showSaveDialog(this);


if (isi == JFileChooser.APPROVE_OPTION){
File file = dialog.getSelectedFile();
try{
file.createNewFile();
PrintWriter out = new PrintWriter (new BufferedWriter(new
FileWriter(file)));
out.print(txtChiperText.getText());
out.flush();
}
catch (IOException ex){
System.err.println("Error:"+ex.getMessage());
}
}
}

private void btDekripActionPerformed(java.awt.event.ActionEvent evt) {

String isitext = txtChiperText.getText();


char k = (char)(isitext.charAt(0));
String key = "", out = "";
int jumKey = Integer.parseInt(String.valueOf(k))-1;
for (int i = 1; i<=jumKey; i++){
int index = isitext.charAt(i);
char s = (char)(index-1);
key = key + String.valueOf(s);
}
if (key.equals(txtKey.getText())){
for (int i = (jumKey+1); i<isitext.length(); i++){
int index = isitext.charAt(i);
char s = (char)(index-1);
out = out + String.valueOf(s);
}
txtPlainText.setText(out);
}
else{
JOptionPane.showMessageDialog(null,"Key Tidak Cocok atau Belum
Diisi!\nSilahkan coba lagi");
}
}

private void btBukaActionPerformed(java.awt.event.ActionEvent evt) {

txtChiperText.setText(null);
int isi = dialog.showOpenDialog(this);
if (isi == JFileChooser.APPROVE_OPTION){
File file = dialog.getSelectedFile();
try{
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine())!=null){
StringTokenizer st = new StringTokenizer(strLine, ",");
txtChiperText.setText(txtChiperText.getText()+st.nextToken()
+"\n");
}
in.close();

}
catch (Exception e){
System.err.println("Error:" + e.getMessage());
}
}
}

private void reset(){


txtPlainText.setText("");
txtChiperText.setText("");
txtKey.setText("");
}
private void btKeluarActionPerformed(java.awt.event.ActionEvent evt) {

System.exit(0);
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

reset();
}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FromEnkripsi().setVisible(true);
}
});
}

private javax.swing.JButton btBuka;


private javax.swing.JButton btDekrip;
private javax.swing.JButton btEnkrip;
private javax.swing.JButton btKeluar;
private javax.swing.JButton btSimpan;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private java.awt.Label label1;
private javax.swing.JTextArea txtChiperText;
private javax.swing.JTextField txtKey;
private javax.swing.JTextArea txtPlainText;

You might also like