Des 對文件進行加解密

加密: 

 

public static void encryptSimple(String file, String destFile)

throws Exception {

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

cipher.init(Cipher.ENCRYPT_MODE, desKey);

InputStream is = new FileInputStream(file);

OutputStream out = new FileOutputStream(destFile);

CipherInputStream cis = new CipherInputStream(is, cipher);

byte[] buffer = new byte[1024];

int r;

 

while ((r = cis.read(buffer)) > 0) {

out.write(buffer, 0, r);

}

cis.close();

is.close();

out.flush();

out.close();

}

 

 

解密: 

 

public static void decryptSimple(String file, String dest) throws Exception {

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

cipher.init(Cipher.DECRYPT_MODE, desKey);

InputStream is = new FileInputStream(file);

OutputStream out = new FileOutputStream(dest);

CipherOutputStream cos = new CipherOutputStream(out, cipher);

byte[] buffer = new byte[1024];

int r;

 

while ((r = is.read(buffer)) >= 0) {

cos.write(buffer, 0, r);

}

cos.close();

out.flush();

out.close();

is.close();

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章