DES
DES
*/
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;
/**
*/
int numBytes;
os.write(bytes, 0, numBytes);
os.flush();
os.close();
is.close();
private static void encrypt(String key, InputStream is, OutputStream os) throws Throwable{
private static void decrypt(String key, InputStream is, OutputStream os) throws Throwable{
private static void encryptOrDecrypt(String key,int mode, InputStream is, OutputStream os) throws Throwable{
if(mode==Cipher.ENCRYPT_MODE){
cipher.init(Cipher.ENCRYPT_MODE, desKey);
doCopy(cis, os);
}else if(mode==Cipher.DECRYPT_MODE){
cipher.init(Cipher.DECRYPT_MODE, desKey);
doCopy(is, cos);
}
/**
*/
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
*/
@SuppressWarnings("unchecked")
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Plain Text:");
txtaPlain.setColumns(20);
txtaPlain.setRows(5);
jScrollPane2.setViewportView(txtaPlain);
jLabel3.setText("Input Key:");
txtaCipher.setColumns(20);
txtaCipher.setRows(5);
jScrollPane1.setViewportView(txtaCipher);
jLabel4.setText("Cirpher Text:");
btnEn.setBackground(new java.awt.Color(102, 102, 102));
btnEn.setText("Mã hóa");
btnEn.addActionListener(new java.awt.event.ActionListener() {
btnEnActionPerformed(evt);
});
btnDe.setText("Giải mã");
btnDe.addActionListener(new java.awt.event.ActionListener() {
btnDeActionPerformed(evt);
});
btnWri.setText("Ghi File");
btnWri.addActionListener(new java.awt.event.ActionListener() {
btnWriActionPerformed(evt);
});
btnOKA.addActionListener(new java.awt.event.ActionListener() {
btnOKAActionPerformed(evt);
});
btnShow.setText("All Show");
btnShow.addActionListener(new java.awt.event.ActionListener() {
btnShowActionPerformed(evt);
});
btnOKB.addActionListener(new java.awt.event.ActionListener() {
btnOKBActionPerformed(evt);
});
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.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)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.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()
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.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))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
);
pack();
}// </editor-fold>
private void btnEnActionPerformed(java.awt.event.ActionEvent evt) {
try{
BufferedReader br = null;
while(br.ready()){
sb.append(ca, 0, len);
br.close();
txtaCipher.setText(chuoi);
}catch(Throwable e){
e.printStackTrace();
try{
BufferedWriter bw = null;
String s = txtaPlain.getText();
bw.write(s);
bw.close();
txtaCipher.setText(s);
}catch(IOException ex){
Logger.getLogger(DESCS.class.getName()).log(Level.SEVERE,null,ex);
try{
BufferedReader br = null;
while(br.ready()){
sb.append(ca, 0, len);
br.close();
txtKey.setText(chuoi);
}catch(IOException ex){
Logger.getLogger(DESCS.class.getName()).log(Level.SEVERE,null,ex);
}
private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {
try{
BufferedReader br = null;
while(br.ready()){
sb.append(ca, 0, len);
br.close();
String ff = "D:\\EnDes.txt";
while(br.ready()){
sb1.append(ca1, 0, len);
txtaPlain.setText(chuoi);
txtaCipher.setText(chuoi1);
}catch(IOException ex){}
try{
BufferedReader br = null;
while(br.ready()){
sb.append(ca, 0, len);
br.close();
txtKey.setText(chuoi);
}catch(IOException ex){
Logger.getLogger(DESCS.class.getName()).log(Level.SEVERE,null,ex);
/**
*/
//<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.
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new DESCS().setVisible(true);
});