代码在JAVA上运行良好,但在Android上无法创建解密文件
我正在尝试为Android制作一个文件柜应用程序。对于加密和解密,我使用了一些我以前在JAVA上测试过的代码。它在JAVA上运行良好,但在Android平台上无法创建解密文件 我的代码有点像bellow代码在JAVA上运行良好,但在Android上无法创建解密文件,java,android,encryption,Java,Android,Encryption,我正在尝试为Android制作一个文件柜应用程序。对于加密和解密,我使用了一些我以前在JAVA上测试过的代码。它在JAVA上运行良好,但在Android平台上无法创建解密文件 我的代码有点像bellow public class Cryptographer { private static final String ALGORITHM = "AES"; private final static String ALGO_RANDOM_NUM_GENERATOR = "SHA1PRNG"; pu
public class Cryptographer {
private static final String ALGORITHM = "AES";
private final static String ALGO_RANDOM_NUM_GENERATOR = "SHA1PRNG";
public static int encrypt(String key, File inputFile, File outputFile)
throws CryptographyException {
if(!outputFile.getParentFile().exists())outputFile.getParentFile().mkdir();
return StartCryptography(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
}
public static int decrypt(String key, File inputFile, File outputFile)
throws CryptographyException {
if(!outputFile.getParentFile().exists())outputFile.getParentFile().mkdir();
return StartCryptography(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
}
private static int StartCryptography(int cipherMode, String key, File inputFile,
File outputFile) throws CryptographyException {
int performance = -1;
try {
SecureRandom random = SecureRandom.getInstance(ALGO_RANDOM_NUM_GENERATOR);
random.setSeed(key.getBytes());
KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
generator.init(random);
SecretKey key1 = generator.generateKey();
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(cipherMode, key1);
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
performance = 1;
} catch (NoSuchPaddingException | NoSuchAlgorithmException
| InvalidKeyException | BadPaddingException
| IllegalBlockSizeException | IOException ex) {
throw new CryptographyException("Error encrypting/decrypting file", ex);
} finally {
return performance;
}
}
}
public void onKeyDown1(View view) throws CryptographyException {
File outfile = new File(ExternalStorageDirectoryPath, "Encrypted");
File outfileDec = new File(ExternalStorageDirectoryPath, "Decrypted");
for (MyFiles f : files) {
if (f.getCheckstate()) {
File EncFile=new File(outfile,f.getName());
//File DecFile=new File(outfileDec,f.getName());
Cryptographer.encrypt(key, f, EncFile);
Cryptographer.decrypt(key, EncFile, DecFile);
}
}
}
我正在使用一个按钮,当按下该按钮时,声明数组中的所有文件都应该首先加密,然后加密的文件将被解密到另一个位置。但是没有创建解密文件
我点击按钮的方法看起来像下面的
public class Cryptographer {
private static final String ALGORITHM = "AES";
private final static String ALGO_RANDOM_NUM_GENERATOR = "SHA1PRNG";
public static int encrypt(String key, File inputFile, File outputFile)
throws CryptographyException {
if(!outputFile.getParentFile().exists())outputFile.getParentFile().mkdir();
return StartCryptography(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
}
public static int decrypt(String key, File inputFile, File outputFile)
throws CryptographyException {
if(!outputFile.getParentFile().exists())outputFile.getParentFile().mkdir();
return StartCryptography(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
}
private static int StartCryptography(int cipherMode, String key, File inputFile,
File outputFile) throws CryptographyException {
int performance = -1;
try {
SecureRandom random = SecureRandom.getInstance(ALGO_RANDOM_NUM_GENERATOR);
random.setSeed(key.getBytes());
KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
generator.init(random);
SecretKey key1 = generator.generateKey();
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(cipherMode, key1);
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
performance = 1;
} catch (NoSuchPaddingException | NoSuchAlgorithmException
| InvalidKeyException | BadPaddingException
| IllegalBlockSizeException | IOException ex) {
throw new CryptographyException("Error encrypting/decrypting file", ex);
} finally {
return performance;
}
}
}
public void onKeyDown1(View view) throws CryptographyException {
File outfile = new File(ExternalStorageDirectoryPath, "Encrypted");
File outfileDec = new File(ExternalStorageDirectoryPath, "Decrypted");
for (MyFiles f : files) {
if (f.getCheckstate()) {
File EncFile=new File(outfile,f.getName());
//File DecFile=new File(outfileDec,f.getName());
Cryptographer.encrypt(key, f, EncFile);
Cryptographer.decrypt(key, EncFile, DecFile);
}
}
}
有人请帮帮我
ExternalStorageDirectoryPath
是通过调用Environment.getExternalStorageDirectory().getAbsolutePath()检索的字符串
代码>
而MyFiles类是
public class MyFiles extends File {
boolean Checkstate=false;
public MyFiles(File dir, String name) {
super(dir, name);
}
public MyFiles(String path) {
super(path);
}
public MyFiles(String dirPath, String name) {
super(dirPath, name);
}
public MyFiles(URI uri) {
super(uri);
}
public void setCheckstate(boolean checkstate) {
Checkstate = checkstate;
}
public boolean getCheckstate(){
return Checkstate;
}
public MyFiles[] listFiles(FileFilter filter) {
MyFiles[] files = listFiles();
if (filter == null || files == null) {
return files;
}
List<MyFiles> result = new ArrayList<MyFiles>(files.length);
for (MyFiles file : files) {
if (filter.accept(file)) {
result.add(file);
}
}
return result.toArray(new MyFiles[result.size()]);
}
public MyFiles[] listFiles() {
return filenamesToFiles(list());
}
private MyFiles[] filenamesToFiles(String[] filenames) {
if (filenames == null) {
return null;
}
int count = filenames.length;
MyFiles[] result = new MyFiles[count];
for (int i = 0; i < count; ++i) {
result[i] = new MyFiles(this, filenames[i]);
}
return result;
}
}
公共类MyFiles扩展文件{
布尔Checkstate=false;
公共MyFiles(文件目录、字符串名称){
super(dir,name);
}
公共MyFiles(字符串路径){
超级(路径);
}
公共MyFiles(字符串路径、字符串名称){
super(dirPath,name);
}
公共MyFiles(URI){
超级(uri);
}
public void setCheckstate(布尔checkstate){
Checkstate=Checkstate;
}
公共布尔getCheckstate(){
返回Checkstate;
}
公共MyFiles[]列表文件(文件过滤器过滤器){
MyFiles[]files=listFiles();
if(filter==null | | files==null){
归还文件;
}
列表结果=新的ArrayList(files.length);
用于(MyFiles文件:文件){
if(filter.accept(文件)){
结果.添加(文件);
}
}
返回result.toArray(新的MyFiles[result.size()]);
}
公共MyFiles[]列表文件(){
返回filenamesToFiles(list());
}
私有MyFiles[]文件名文件(字符串[]文件名){
如果(文件名==null){
返回null;
}
int count=filenames.length;
MyFiles[]结果=新MyFiles[计数];
对于(int i=0;i
我想可能是您的android应用程序的清单文件中缺少权限。检查:
或者您正在尝试在不允许您的应用程序的位置写入内容刚才我发现我的解密方法无法工作,因为出现了“javax.crypto.BadPaddingException:pad block corrupted”错误。但是我不知道如何修复它。如果包含MyFiles.java和可选的ExternalStorageDirectoryPath,它将更易于测试。我不知道自定义对象在循环中发生了什么