`
eeeeeeee
  • 浏览: 39261 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
文章分类
社区版块
存档分类
最新评论

各位高手,看我什么地方写错了

 
阅读更多
public class TestFile1 {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
File files = new File("e:/A.doc");
String key = "1111";
encrypt(files, key);
}

/**
*
* @param inFile 输入要加密文件
* @param strKey 加密的密码
* @throws Exception
*/
private static void encrypt(File inFile, String strKey) throws Exception{
InputStream fis = new FileInputStream(inFile);
byte[] date = new byte[(int) inFile.length()];
for(int i = 0; i < inFile.length(); i++){
//读取每一个字节
date[i] = (byte) fis.read();
}
//文件名
String fileName = inFile.getName().substring(0, inFile.getName().lastIndexOf(".")) + System.currentTimeMillis() + inFile.getName().substring(inFile.getName().lastIndexOf("."), inFile.getName().length());
//文件路径
String path = "e:/xx/" + fileName;
byte[] bKey = strKey.getBytes();
//输出的数据
byte[] outDate = encryptByDES(date, bKey);
File outFile = new File(path);
OutputStream fos = new FileOutputStream(outFile);
fos.write(outDate);
fos.close();
}

/**
用DES方法加密输入的字节
bytKey需为8字节长,是加密的密码
*/
private static byte[] encryptByDES(byte[] bytP,byte[] bytKey) throws Exception{
DESKeySpec desKS = new DESKeySpec(bytKey);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(desKS);
Cipher cip = Cipher.getInstance("DES");
cip.init(Cipher.ENCRYPT_MODE,sk);
return cip.doFinal(bytP);
}

}
[size=24][/size][color=red][/color]
分享到:
评论
1 楼 ddandyy 2007-08-04  
不是高手.....................

相关推荐

Global site tag (gtag.js) - Google Analytics