一定要关流,完了。
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class T {
static String fileA = "D:/a.txt";
static String fileB = "D:/b.txt";
private static final byte[] KEY = { 37, -82, 88, -42, 1, -36, -104, -125,
4, 81, -75, -94, -33, -75, 110, -3, -32, 64, -48, -105, -43, -113,
104, 32 };
public static void main(String[] args) throws IOException,
InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException {
String sourceString = doReadNoDes(fileB);
doWrite(sourceString, fileA);
System.out.println(doRead(fileA));
}
private static String doRead(String file) throws InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException, IOException {
FileInputStream input = null;
try {
input = new FileInputStream(file);
return read(input);
} finally {
input.close();
}
}
private static void doWrite(String sourceString, String file)
throws IOException, InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException {
FileOutputStream output = null;
try {
output = new FileOutputStream(file);
write(output, sourceString);
} finally {
output.close();
}
}
private static String doReadNoDes(String file) throws IOException {
FileInputStream input = null;
try {
input = new FileInputStream(file);
return getStringFromStream(input);
} finally {
input.close();
}
}
private static String read(InputStream input)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IOException {
CipherInputStream cin = null;
try {
SecretKey key = getKey();
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, key);
cin = new CipherInputStream(input, cp);
return getStringFromStream(cin);
} finally {
cin.close();
}
}
private static void write(OutputStream output, String sourceString)
throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
CipherOutputStream cin = null;
try {
SecretKey key = getKey();
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, key);
cin = new CipherOutputStream(output, cp);
cin.write(sourceString.getBytes());
} finally {
cin.close();
}
}
private static SecretKey getKey() {
return new SecretKeySpec(KEY, "DESede");
}
private static String getStringFromStream(InputStream stream) {
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class T {
static String fileA = "D:/a.txt";
static String fileB = "D:/b.txt";
private static final byte[] KEY = { 37, -82, 88, -42, 1, -36, -104, -125,
4, 81, -75, -94, -33, -75, 110, -3, -32, 64, -48, -105, -43, -113,
104, 32 };
public static void main(String[] args) throws IOException,
InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException {
String sourceString = doReadNoDes(fileB);
doWrite(sourceString, fileA);
System.out.println(doRead(fileA));
}
private static String doRead(String file) throws InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException, IOException {
FileInputStream input = null;
try {
input = new FileInputStream(file);
return read(input);
} finally {
input.close();
}
}
private static void doWrite(String sourceString, String file)
throws IOException, InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException {
FileOutputStream output = null;
try {
output = new FileOutputStream(file);
write(output, sourceString);
} finally {
output.close();
}
}
private static String doReadNoDes(String file) throws IOException {
FileInputStream input = null;
try {
input = new FileInputStream(file);
return getStringFromStream(input);
} finally {
input.close();
}
}
private static String read(InputStream input)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IOException {
CipherInputStream cin = null;
try {
SecretKey key = getKey();
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, key);
cin = new CipherInputStream(input, cp);
return getStringFromStream(cin);
} finally {
cin.close();
}
}
private static void write(OutputStream output, String sourceString)
throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
CipherOutputStream cin = null;
try {
SecretKey key = getKey();
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, key);
cin = new CipherOutputStream(output, cp);
cin.write(sourceString.getBytes());
} finally {
cin.close();
}
}
private static SecretKey getKey() {
return new SecretKeySpec(KEY, "DESede");
}
private static String getStringFromStream(InputStream stream) {
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}