Android 在流异常中找不到密文的匹配密钥
您好,我正在使用jetpack安全加密库对文件进行加密。我已使用以下代码生成主密钥。Android 在流异常中找不到密文的匹配密钥,android,encryption,android-jetpack,android-jetpack-security,Android,Encryption,Android Jetpack,Android Jetpack Security,您好,我正在使用jetpack安全加密库对文件进行加密。我已使用以下代码生成主密钥。 MasterKey masterKey = null; try { masterKey = new MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS) .setKeyScheme(MasterKey.KeySc
MasterKey masterKey = null;
try {
masterKey = new
MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
InputStream inputStream = new BufferedInputStream(appCtx.getAssets().open("Sample.txt"));
byte[] fileBytes=new byte[inputStream.available()];
inputStream.read(fileBytes);
File file = new File(Environment.getExternalStorageDirectory(), "Sample.txt");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
EncryptedFile encryptedFile = new EncryptedFile.Builder(
appCtx,
file,
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build();
OutputStream outputStream = encryptedFile.openFileOutput();
outputStream.write(fileBytes);
outputStream.flush();
outputStream.close();
我已加密了文本文件Sample.txt,并将加密文件写入设备外部存储器。代码如下所示。
MasterKey masterKey = null;
try {
masterKey = new
MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
InputStream inputStream = new BufferedInputStream(appCtx.getAssets().open("Sample.txt"));
byte[] fileBytes=new byte[inputStream.available()];
inputStream.read(fileBytes);
File file = new File(Environment.getExternalStorageDirectory(), "Sample.txt");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
EncryptedFile encryptedFile = new EncryptedFile.Builder(
appCtx,
file,
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build();
OutputStream outputStream = encryptedFile.openFileOutput();
outputStream.write(fileBytes);
outputStream.flush();
outputStream.close();
我将加密文件放在资产文件夹中,现在尝试解密,但根据文档EncryptedFile.Builder始终将文件对象作为参数,当前我在从资产读取文件后有Inputstream因此,为了获取文件对象,我将这个Inutstream作为Temp.txt写入外部存储器,并传递这个文件进行解密。但是我得到的异常是java.io.IOException:在流中找不到与密文匹配的密钥。
MasterKey masterKey = null;
try {
masterKey = new
MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
InputStream inputStream = new BufferedInputStream(appCtx.getAssets().open("Sample.txt"));
byte[] fileBytes=new byte[inputStream.available()];
inputStream.read(fileBytes);
File file = new File(Environment.getExternalStorageDirectory(), "Sample.txt");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
EncryptedFile encryptedFile = new EncryptedFile.Builder(
appCtx,
file,
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build();
OutputStream outputStream = encryptedFile.openFileOutput();
outputStream.write(fileBytes);
outputStream.flush();
outputStream.close();
解密代码如下所示:
InputStream myInputstream = new BufferedInputStream(appCtx.getAssets().open("Sample.txt"));
File enfile = createFileFromInputStream(myInputstream,appCtx);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
EncryptedFile encryptedFile = new EncryptedFile.Builder(
appCtx,
enfile,
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build();
InputStream inputStream1 = encryptedFile.openFileInput();
BufferedOutputStream os1 = new BufferedOutputStream( new FileOutputStream(new File(dstPath)));
int length = 0;
byte[] buffer = new byte[1024];
while ((length = inputStream1.read(buffer)) != -1) {
os1.write(buffer, 0, length);
}
os1.close();
}
private static File createFileFromInputStream(InputStream stream, Context context) {
File f = new File(Environment.getExternalStorageDirectory(), "Temp.txt");
BufferedOutputStream os1 = null;
try {
os1 = new BufferedOutputStream( new FileOutputStream(f));
int length = 0;
byte[] buffer = new byte[1024];
while ((length = stream.read(buffer)) != -1) {
os1.write(buffer, 0, length);
}
os1.close();
stream.close();
return f;
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
//Logging exception
}
return null;
}
主要场景:
若将加密文件写入外部存储器,并直接从外部存储器读取以进行解密,那个么所有文件都正常工作。但是,如果我将加密文件粘贴到资产文件夹中,并将从资产文件夹获取的Inputstream写入某个临时文件,然后尝试对其进行解密,则会出现错误java.io.IOException:在流中找不到与密文匹配的密钥。
MasterKey masterKey = null;
try {
masterKey = new
MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
InputStream inputStream = new BufferedInputStream(appCtx.getAssets().open("Sample.txt"));
byte[] fileBytes=new byte[inputStream.available()];
inputStream.read(fileBytes);
File file = new File(Environment.getExternalStorageDirectory(), "Sample.txt");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
EncryptedFile encryptedFile = new EncryptedFile.Builder(
appCtx,
file,
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build();
OutputStream outputStream = encryptedFile.openFileOutput();
outputStream.write(fileBytes);
outputStream.flush();
outputStream.close();
请任何人帮我解决这个问题。
谢谢这是android加密库的内部实现问题。尝试更新到最新版本,这对我很有帮助。具体来说,版本
1.1.0-alpha02
解决了我的问题。