急~~请问,对文件进行加密,有什么好的方法吗?
请推荐种好的方法
------解决方案--------------------io中用加密算法啊,读取的时候解密
算法很多,对称密钥,AES等
------解决方案--------------------java专门提供了加密的包啊,javax.crypto。你可以调用DES,或者RSA等算法并对他们进行实现的。。对文件也能用那个包的方法,只要把文件转化成流就可以了。。
给你个例子:用DES算法来对文件进行加密~
public class DesUtil {
	/**
	 * 把成生的一对密钥保存到DesKey.xml文件中
	 */
	public static void saveDesKey() {
		try {
			SecureRandom sr = new SecureRandom();
			// 为我们选择的DES算法生成一个KeyGenerator对象
			KeyGenerator kg = KeyGenerator.getInstance("DES");
			//初始化密码生成器
			kg.init(sr);
			FileOutputStream fos = new FileOutputStream("c:/test/DesKey.xml");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			// 生成密钥
			Key key = kg.generateKey();
			oos.writeObject(key);
			oos.close();
			System.out.println("获取密钥对成功!!!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 *  
	 * @return Key 返回对称密钥
	 */
	public static Key getKey() {
		Key kp = null;
		try {
			String fileName = "c:/test/DesKey.xml";
			InputStream is = new FileInputStream(fileName);
			ObjectInputStream oos = new ObjectInputStream(is);
			kp = (Key) oos.readObject();
			oos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return kp;
	}
	/**
	 * 文件file进行加密并保存目标文件destFile中
	 *  
	 * @param file
	 *            要加密的文件 如c:/test/srcFile.txt
	 * @param destFile
	 *            加密后存放的文件名 如c:/加密后文件.txt
	 */
	public static void encrypt(String file, String destFile) throws Exception {
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.ENCRYPT_MODE, getKey());
		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.close();
		System.out.println("文件加密处理结束!");
	}
	/**
	 * 文件file进行加密并保存目标文件destFile中
	 *  
	 * @param file
	 *            已加密的文件 如c:/加密后文件.txt
	 * @param destFile
	 *            解密后存放的文件名 如c:/ test/解密后文件.txt
	 */
	public static void decrypt(String file, String dest) throws Exception {
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.DECRYPT_MODE, getKey());
		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.close();
		is.close();
		System.out.println("文件解密处理结束!");
	}
	public static void main(String[] args) {
		DesUtil.saveDesKey();
		DesUtil.getKey();
		try {
			DesUtil.encrypt("c:/test/XMLIndexAdvisorTechReport.pdf", "c:/test/加密后XMLIndexAdvisorTechReport.pdf");
		} catch (Exception e1) {			
			e1.printStackTrace();
		}
		try {
			DesUtil.decrypt("c:/test/加密后XMLIndexAdvisorTechReport.pdf", "c:/test/解密后XMLIndexAdvisorTechReport.pdf");
		} catch (Exception e) {			
			e.printStackTrace();
		}
	}
}