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

DES_cách 3

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 TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

DES_cách 3

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 TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

private void btnMaHoa(java.awt.event.

ActionEvent evt) {
// TODO add your handling code here:
try{
String key = txtk.getText();
FileInputStream fis = new FileInputStream("D:\\TH_BMTT\\DESCS\\
Des.txt");
FileOutputStream fos = new FileOutputStream("D:\\TH_BMTT\\DESCS\\
EnDes.txt");
encrypt(key, fis, fos);
JOptionPane.showMessageDialog(null, "Đã mã hóa văn bản");

BufferedReader br = null;
String fileName ="D:\\TH_BMTT\\DESCS\\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();
txtc.setText(chuoi);

}catch(Throwable e){
e.printStackTrace();
}

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


// TODO add your handling code here:
FileInputStream fis2 = null;
try{
String key = txtk.getText();
fis2 = new FileInputStream("D:\\TH_BMTT\\DESCS\\EnDes.txt");
FileOutputStream fos2 = new FileOutputStream("D:\\TH_BMTT\\DESCS\\
DeDes.txt");
decrypt(key, fis2, fos2);
JOptionPane.showMessageDialog(null, "Đã giải mã");
BufferedReader br = null;
String fileName ="D:\\TH_BMTT\\DESCS\\DeDes.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();
txtc.setText(chuoi);
}catch(Throwable ex){}
}

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


// TODO add your handling code here:
try{
BufferedReader br = null;
String fileName = "D:\\TH_BMTT\\DESCS\\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:\\TH_BMTT\\DESCS\\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();
txtp.setText(chuoi);
txtc.setText(chuoi1);
}catch(IOException ex){}
}

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


// TODO add your handling code here:
try{
BufferedWriter bw = null;
String fileName = "D:\\TH_BMTT\\DESCS\\Des.txt";
String s = txtp.getText();
bw = new BufferedWriter(new FileWriter(fileName));
bw.write(s);
bw.close();
JOptionPane.showMessageDialog(null, "Đã ghi file");
txtc.setText(s);
}catch(IOException ex){
Logger.getLogger(DESCS.class.getName()).log(Level.SEVERE,null,ex);
}

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);
}
}

You might also like