Cały kod programu (używam NetBeans):
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Scram extends javax.swing.JFrame {
JFileChooser chooser = new JFileChooser();
File plik;
public Scram() {
initComponents();
ImageIcon iconImage = new ImageIcon(this.getClass().getResource("Lock.png"));
this.setIconImage(iconImage.getImage());
getContentPane().setBackground(new Color(30, 144, 255));
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
sciezka_pliku = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Scrambler");
setBounds(new java.awt.Rectangle(490, 200, 200, 200));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setMaximumSize(new java.awt.Dimension(430, 175));
setMinimumSize(new java.awt.Dimension(430, 175));
setResizable(false);
jButton1.setText("Wybierz");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Zakoduj");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Odkoduj");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jLabel1.setText("Nazwa pliku:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(20, 20, 20)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(sciezka_pliku, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(26, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(45, 45, 45)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addComponent(sciezka_pliku)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(42, Short.MAX_VALUE))
);
setSize(new java.awt.Dimension(446, 213));
setLocationRelativeTo(null);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
chooser.showOpenDialog(null);
plik = chooser.getSelectedFile();
sciezka_pliku.setText(plik.getAbsolutePath());
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try{
FileInputStream file = new FileInputStream(sciezka_pliku.getText());
byte k[] = "Pa$$word".getBytes();
SecretKeySpec key = new SecretKeySpec(k, "DES");
Cipher enc = Cipher.getInstance("DES");
enc.init(Cipher.ENCRYPT_MODE, key);
chooser.showSaveDialog(null);
plik = chooser.getSelectedFile();
FileOutputStream outStream = new FileOutputStream(plik.getAbsolutePath());
CipherOutputStream cos = new CipherOutputStream(outStream, enc);
byte[] bufor = new byte[1024];
int read;
while((read=file.read(bufor)) != -1){
cos.write(bufor, 0, read);
sciezka_pliku.setText(plik.getAbsolutePath());
}
file.close();
outStream.flush();
cos.close();
JOptionPane.showMessageDialog(null, "Plik został zakodowany");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
try{
FileInputStream file = new FileInputStream(sciezka_pliku.getText());
byte k[] = "Pa$$word".getBytes();
SecretKeySpec key = new SecretKeySpec(k, "DES");
Cipher enc = Cipher.getInstance("DES");
enc.init(Cipher.DECRYPT_MODE, key);
chooser.showSaveDialog(null);
plik = chooser.getSelectedFile();
FileOutputStream outStream = new FileOutputStream(plik.getAbsolutePath());
CipherOutputStream cos = new CipherOutputStream(outStream, enc);
byte[] bufor = new byte[1024];
int read;
while((read=file.read(bufor)) != -1){
cos.write(bufor, 0, read);
}
file.close();
outStream.flush();
cos.close();
JOptionPane.showMessageDialog(null, "Plik został odkodowany");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
sciezka_pliku.setText(plik.getAbsolutePath());
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Scram().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField sciezka_pliku;
// End of variables declaration
private void delete(FileInputStream file) {
throw new UnsupportedOperationException("Not supported yet.");
}
}