0% found this document useful (0 votes)
11 views15 pages

DES

Uploaded by

Quý Thiên
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views15 pages

DES

Uploaded by

Quý Thiên
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license

* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template

*/

package com.java.mvc.mahoa;

import java.awt.Dimension;

import java.awt.Toolkit;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.CipherOutputStream;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

import javax.swing.JOptionPane;

/**

*/

public class DESCS extends javax.swing.JFrame {

private int mode;


private static void doCopy(InputStream is, OutputStream os) throws IOException{

byte[] bytes = new byte[64];

int numBytes;

while((numBytes = is.read(bytes)) != -1){

os.write(bytes, 0, numBytes);

os.flush();

os.close();

is.close();

private static void encrypt(String key, InputStream is, OutputStream os) throws Throwable{

encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);

private static void decrypt(String key, InputStream is, OutputStream os) throws Throwable{

encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);

private static void encryptOrDecrypt(String key,int mode, InputStream is, OutputStream os) throws Throwable{

DESKeySpec dks = new DESKeySpec(key.getBytes());

SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");

SecretKey desKey = skf.generateSecret(dks);

Cipher cipher = Cipher.getInstance("DES");

if(mode==Cipher.ENCRYPT_MODE){

cipher.init(Cipher.ENCRYPT_MODE, desKey);

CipherInputStream cis = new CipherInputStream(is, cipher);

doCopy(cis, os);

}else if(mode==Cipher.DECRYPT_MODE){

cipher.init(Cipher.DECRYPT_MODE, desKey);

CipherOutputStream cos = new CipherOutputStream(os, cipher);

doCopy(is, cos);
}

/**

* Creates new form DESCS

*/

public DESCS() {

initComponents();

Dimension screenSize,frameSize;

int x,y;

screenSize=Toolkit.getDefaultToolkit().getScreenSize();

frameSize=getSize();

x=(screenSize.width-frameSize.width)/2;

y=(screenSize.height-frameSize.height)/2;

setLocation(x, y);

/**

* This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jScrollPane2 = new javax.swing.JScrollPane();

txtaPlain = new javax.swing.JTextArea();

jLabel3 = new javax.swing.JLabel();

txtKey = new javax.swing.JTextField();


jScrollPane1 = new javax.swing.JScrollPane();

txtaCipher = new javax.swing.JTextArea();

jLabel4 = new javax.swing.JLabel();

btnEn = new javax.swing.JButton();

btnDe = new javax.swing.JButton();

btnWri = new javax.swing.JButton();

btnOKA = new javax.swing.JButton();

btnShow = new javax.swing.JButton();

btnOKB = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Segoe UI", 1, 24)); // NOI18N

jLabel1.setForeground(new java.awt.Color(0, 102, 102));

jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

jLabel1.setText("Mã hóa/Giải mã");

jLabel2.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N

jLabel2.setText("Plain Text:");

txtaPlain.setColumns(20);

txtaPlain.setRows(5);

jScrollPane2.setViewportView(txtaPlain);

jLabel3.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N

jLabel3.setText("Input Key:");

txtaCipher.setColumns(20);

txtaCipher.setRows(5);

jScrollPane1.setViewportView(txtaCipher);

jLabel4.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N

jLabel4.setText("Cirpher Text:");
btnEn.setBackground(new java.awt.Color(102, 102, 102));

btnEn.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N

btnEn.setForeground(new java.awt.Color(255, 255, 255));

btnEn.setText("Mã hóa");

btnEn.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnEnActionPerformed(evt);

});

btnDe.setBackground(new java.awt.Color(102, 102, 102));

btnDe.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N

btnDe.setForeground(new java.awt.Color(255, 255, 255));

btnDe.setText("Giải mã");

btnDe.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnDeActionPerformed(evt);

});

btnWri.setBackground(new java.awt.Color(102, 102, 102));

btnWri.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N

btnWri.setForeground(new java.awt.Color(255, 255, 255));

btnWri.setText("Ghi File");

btnWri.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnWriActionPerformed(evt);

});

btnOKA.setBackground(new java.awt.Color(102, 102, 102));

btnOKA.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N

btnOKA.setForeground(new java.awt.Color(255, 255, 255));


btnOKA.setText("Mở khóa A");

btnOKA.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnOKAActionPerformed(evt);

});

btnShow.setBackground(new java.awt.Color(102, 102, 102));

btnShow.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N

btnShow.setForeground(new java.awt.Color(255, 255, 255));

btnShow.setText("All Show");

btnShow.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnShowActionPerformed(evt);

});

btnOKB.setBackground(new java.awt.Color(102, 102, 102));

btnOKB.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N

btnOKB.setForeground(new java.awt.Color(255, 255, 255));

btnOKB.setText("Mở khóa B");

btnOKB.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnOKBActionPerformed(evt);

});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jLabel1)
.addGap(215, 215, 215))

.addGroup(layout.createSequentialGroup()

.addGap(42, 42, 42)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(btnEn, javax.swing.GroupLayout.PREFERRED_SIZE, 120,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(btnOKA, javax.swing.GroupLayout.PREFERRED_SIZE, 138,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(btnOKB, javax.swing.GroupLayout.PREFERRED_SIZE, 124,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(btnWri, javax.swing.GroupLayout.PREFERRED_SIZE, 121,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(0, 0, Short.MAX_VALUE))

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel3)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(txtKey, javax.swing.GroupLayout.PREFERRED_SIZE, 440,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel2)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 440,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addComponent(jLabel4)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(btnShow, javax.swing.GroupLayout.PREFERRED_SIZE, 140,


javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(160, 160, 160)

.addComponent(btnDe, javax.swing.GroupLayout.PREFERRED_SIZE, 140,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 440,


javax.swing.GroupLayout.PREFERRED_SIZE))))

.addGap(51, 51, 51))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(46, 46, 46)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(txtKey, javax.swing.GroupLayout.PREFERRED_SIZE, 38,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel3))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(btnOKA, javax.swing.GroupLayout.PREFERRED_SIZE, 32,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(btnEn, javax.swing.GroupLayout.PREFERRED_SIZE, 32,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(btnWri, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(btnOKB, javax.swing.GroupLayout.PREFERRED_SIZE, 32,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel2)

.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(16, 16, 16)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel4)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(btnShow, javax.swing.GroupLayout.PREFERRED_SIZE, 32,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(btnDe, javax.swing.GroupLayout.PREFERRED_SIZE, 33,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(32, 32, 32))

);

pack();

}// </editor-fold>
private void btnEnActionPerformed(java.awt.event.ActionEvent evt) {

try{

String key = txtKey.getText();

FileInputStream fis = new FileInputStream("D:\\Des.txt");

FileOutputStream fos = new FileOutputStream("D:\\EnDes.txt");

encrypt(key, fis, fos);

JOptionPane.showMessageDialog(null, "Đã mã hóa văn bản");

BufferedReader br = null;

String fileName ="D:\\EnDes.txt";

br = new BufferedReader(new FileReader(fileName));

StringBuffer sb = new StringBuffer();

char[] ca = new char[5];

while(br.ready()){

int len = br.read(ca);

sb.append(ca, 0, len);

br.close();

System.out.println("Du Lieu la:" + " " + sb);

String chuoi = sb.toString();

txtaCipher.setText(chuoi);

}catch(Throwable e){

e.printStackTrace();

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

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

// TODO add your handling code here:

try{
BufferedWriter bw = null;

String fileName = "D:\\Des.txt";

String s = txtaPlain.getText();

bw = new BufferedWriter(new FileWriter(fileName));

bw.write(s);

bw.close();

JOptionPane.showMessageDialog(null, "Đã ghi file");

txtaCipher.setText(s);

}catch(IOException ex){

Logger.getLogger(DESCS.class.getName()).log(Level.SEVERE,null,ex);

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

// TODO add your handling code here:

try{

BufferedReader br = null;

String fileName = "D:\\KhoaA.txt";

br = new BufferedReader(new FileReader(fileName));

StringBuffer sb = new StringBuffer();

JOptionPane.showMessageDialog(null, "Da mo file");

char[] ca = new char[5];

while(br.ready()){

int len = br.read(ca);

sb.append(ca, 0, len);

br.close();

System.out.println("Du lieu la: "+" "+sb);

String chuoi = sb.toString();

txtKey.setText(chuoi);

}catch(IOException ex){

Logger.getLogger(DESCS.class.getName()).log(Level.SEVERE,null,ex);

}
private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

try{

BufferedReader br = null;

String fileName = "D:\\DeDes.txt";

br = new BufferedReader(new FileReader(fileName));

StringBuffer sb = new StringBuffer();

JOptionPane.showMessageDialog(null, "Đã mở file");

char[] ca = new char[5];

while(br.ready()){

int len = br.read(ca);

sb.append(ca, 0, len);

br.close();

String ff = "D:\\EnDes.txt";

br = new BufferedReader(new FileReader(ff));

StringBuffer sb1 = new StringBuffer();

JOptionPane.showMessageDialog(null, "Đã mở file");

char[] ca1 = new char[5];

while(br.ready()){

int len = br.read(ca1);

sb1.append(ca1, 0, len);

System.out.println("Du lieu la: "+" "+sb);

String chuoi = sb.toString();

String chuoi1 = sb1.toString();

txtaPlain.setText(chuoi);

txtaCipher.setText(chuoi1);

}catch(IOException ex){}

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


// TODO add your handling code here:

try{

BufferedReader br = null;

String fileName = "D:\\KhoaB.txt";

br = new BufferedReader(new FileReader(fileName));

StringBuffer sb = new StringBuffer();

JOptionPane.showMessageDialog(null, "Đã mở file");

char[] ca = new char[5];

while(br.ready()){

int len = br.read(ca);

sb.append(ca, 0, len);

br.close();

System.out.println("Du lieu la: "+" "+sb);

String chuoi = sb.toString();

txtKey.setText(chuoi);

}catch(IOException ex){

Logger.getLogger(DESCS.class.getName()).log(Level.SEVERE,null,ex);

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(DESCS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(DESCS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(DESCS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(DESCS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new DESCS().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton btnDe;

private javax.swing.JButton btnEn;

private javax.swing.JButton btnOKA;

private javax.swing.JButton btnOKB;

private javax.swing.JButton btnShow;

private javax.swing.JButton btnWri;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;


private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JScrollPane jScrollPane2;

private javax.swing.JTextField txtKey;

private javax.swing.JTextArea txtaCipher;

private javax.swing.JTextArea txtaPlain;

// End of variables declaration

You might also like