加密文件的问题,调用方法参数问题
我在调用 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;
}
}
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;
}
}
...全文


