java IO 加密解密小例子

一定要关流,完了。
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);
}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值