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

}

 

你可能感兴趣的:(exception,String,File,解密,buffer,byte)