package com
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import javax
import javax
public class DESEncryptUtil {
public static Key createKey() throws NoSuchAlgorithmException {//創建密鑰
Security
KeyGenerator generator = KeyGenerator
generator
Key key = generator
return key;
}
public static Key getKey(InputStream is) {
try {
ObjectInputStream ois = new ObjectInputStream(is);
return (Key) ois
} catch (Exception e) {
e
throw new RuntimeException(e);
}
}
private static byte[] doEncrypt(Key key
try {
Cipher cipher = Cipher
cipher
byte[] raw = cipher
return raw;
} catch (Exception e) {
e
throw new RuntimeException(e);
}
}
public static InputStream doDecrypt(Key key
try {
Cipher cipher = Cipher
cipher
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[
int count =
while ((count = in
bout
tmpbuf = new byte[
}
in
byte[] orgData = bout
byte[] raw = cipher
ByteArrayInputStream bin = new ByteArrayInputStream(raw);
return bin;
} catch (Exception e){
e
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws Exception{
System
if (args
Key key = DESEncryptUtil
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(args[
oos
oos
System
} else if (args
File file = new File(args[
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[
int count =
while ((count = in
bout
tmpbuf = new byte[
}
in
byte[] orgData = bout
Key key = getKey(new FileInputStream(args[
byte[] raw = DESEncryptUtil
file = new File(file
FileOutputStream out = new FileOutputStream(file);
out
out
System
} else if (args
File file = new File(args[
FileInputStream fis = new FileInputStream(file);
Key key = getKey(new FileInputStream(args[
InputStream raw = DESEncryptUtil
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[
int count =
while ((count = raw
bout
tmpbuf = new byte[
}
raw
byte[] orgData = bout
file = new File(file
FileOutputStream fos = new FileOutputStream(file);
fos
System
}else if(args
System
System
System
System
System
System
System
System
System
}else {
System
}
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28658.html