加密文件的问题,调用方法参数问题

qq_27457127 2015-05-12 09:14:13
我在调用 File encryptFile(File sourceFile,String fileType, String sKey)时候,直接
String souceFile="D:\\A.txt"; //这是我随机选择的一个文件。
String fileType= all type??? // 问题1:这里我想所有类型文件。怎么写啊。就是给所有类型文件都加密
String sKey = ???? // 问题2 : 这里是随机生成的秘钥,我应该赋值什么呢



求高手解答、






public File encryptFile(File sourceFile,String fileType, String sKey){
//新建临时加密文件
File encrypfile = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(sourceFile);
encrypfile = File.createTempFile(sourceFile.getName(), fileType);
outputStream = new FileOutputStream(encrypfile);
Cipher cipher = initAESCipher(sKey,Cipher.ENCRYPT_MODE);
//以加密流写入文件
CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);
byte[] cache = new byte[1024];
int nRead = 0;
while ((nRead = cipherInputStream.read(cache)) != -1) {
outputStream.write(cache, 0, nRead);
outputStream.flush();
}
cipherInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
return encrypfile;
}
}
...全文
197 1 打赏 收藏 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_27457127 2015-05-12
  • 打赏
  • 举报
回复
public class SystemOS3 extends JFrame implements ActionListener{ String filepath ; String sourceFile=filepath; String fileType=txt; JPanel pnlMain; JTextField txtfile; JButton btnSelect; JFileChooser fc = new JFileChooser(); JButton Download; public SystemOS3() { pnlMain=new JPanel(); this.getContentPane().add(pnlMain); txtfile=new JTextField(30); btnSelect =new JButton("选择"); btnSelect.addActionListener(this); pnlMain.add(txtfile); pnlMain.add(btnSelect); this.getContentPane().add(pnlMain); Download =new JButton("发送"); Download.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jiami.encryptFile(sourceFile,fileType,sKey); new ServerTest().start(filepath); }

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧