java IO 流加密解密小例子
2013-04-24 17:37
302 查看
一定要关流,完了。
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.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);
}
}
}
相关文章推荐
- java EJB 加密与解密原理的一个例子
- java 私钥加密解密的例子
- GO语言JAVA语言实现的AES/CFB/256位的加密解密例子
- Java IO4 实现凯撒密码加密解密文本文件
- java EJB 加密与解密原理的一个例子
- Java探索之Thread+IO文件的加密解密代码实例
- (黑马)总结一:Java 加密解密简单例子
- java加密解密(简单的小例子)
- java EJB 加密与解密原理的一个例子
- Java的压缩、解压及压缩加密、解密解压 例子
- 加密解密工具类(Java,DES)
- java基于AES对称加密算法实现的加密与解密功能示例
- java IO文件操作入门经典例子
- java基于Des对称加密算法实现的加密与解密功能详解
- DES解密 - java加密 php解密
- 利用Java自带的Base64加密,解密
- java实现AES的简单加密解密
- Java 加密解密基础
- JAVA和C# 3DES加密解密
- Java数字证书对文件/加密/解密/签名/校验签名