急~~请问,对文件进行加密,有什么好的方法吗? 请推荐种好的方法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 io中用加密算法啊,读取的时候解密算法很多,对称密钥,AES等 关注中!以前就做过MD5单向非对称加密算法和3-DES对称型加密算法。并且只用在加密密码上了,文件的还没弄过,我看3-DES对称型加密算法只用到了私匙就那么麻烦,对于RSA那种既有私匙又有公匙的对称型加密算法就更加复杂了。 MD5能对文件做加密吗?如何文件,WORD,XML,DBF等 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(); } }} 请教,怎样按条件复制CSV文件内容到另一个CSV文件 求代码,求出一个整型数组里面每个元素的和 java 反编译的问题。 菜鸟求问如何给按钮指定快捷键? 吐血推荐,没有看过这个真是白活了? 取resultset中数据问题(高分求) 两个对象相等的意义是什么? 请教一个关于java记录集的问题 哪里有down jswdk的 同样的问题我已经问了5遍了,全部分数为200,到给你啦 新手!jdbc连接sql2000的问题?在线等! java实现的快速排序,看不太懂,请人帮忙注释一下
算法很多,对称密钥,AES等
以前就做过MD5单向非对称加密算法和3-DES对称型加密算法。并且只用在加密密码上了,文件的还没弄过,我看3-DES对称型加密算法只用到了私匙就那么麻烦,对于RSA那种既有私匙又有公匙的对称型加密算法就更加复杂了。
如何文件,WORD,XML,DBF等
给你个例子:用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();
}
}}