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

qq_27457127 qq_27457127 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我在调用 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;
}
}
...全文
评论 1
收藏 已收藏
分享
返回顶部
qq_27457127
Java 51,407 成员 86,129 内容