Мне нужно зашифровать и расшифровать файл в Android, а зашифрованный файл (изображение, видео) можно расшифровать и с другого устройства.
Я шифрую и расшифровываю на одном устройстве, все работает нормально, но когда я переключаю пользователя устройства, зашифрованный файл для расшифровки показывает мне ошибку в doFinal()
javax.crypto.IllegalBlockSizeException: последний блок незавершен при расшифровке
это любой способ зашифровать файл с одного устройства и получить доступ ко всем другим устройствам, таким как шифрование паролей в android. И, используя этот ключ пароля на другом устройстве, мы получаем доступ к информации о файле.
// код
private boolean encrypt() {
try {
String path = Environment.getExternalStorageDirectory() + File.separator + "Download/tools.png";
String pathe = Environment.getExternalStorageDirectory() + File.separator + "Download/tools.png";
byte[] fileData = FileUtils.readFile(path);
byte[] encodedBytes = EncryptDecryptUtils.encode(EncryptDecryptUtils.getInstance(this).getSecretKey(), fileData);
FileUtils.saveFile(encodedBytes, pathe);
return true;
} catch (Exception e) {
Toast.makeText(this, "File Encryption failed.\nException: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
return false;
}
/**
* Decrypt and return the decoded bytes
*
* @return
*/
private byte[] decrypt() {
try {
String pathe = Environment.getExternalStorageDirectory() + File.separator + "Download/tools.png";
byte[] fileData = FileUtils.readFile(pathe);
byte[] decryptedBytes = EncryptDecryptUtils.decode(EncryptDecryptUtils.getInstance(this).getSecretKey(), fileData);
return decryptedBytes;
} catch (Exception e) {
Toast.makeText(this, "File Decryption failed.\nException: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
return null;
}
Ниже приведен код класса EncryptDecryptUtils.
public static byte[] encode(SecretKey yourKey, byte[] fileData)
throws Exception {
byte[] data = yourKey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(data, 0, data.length, EncoDecode.KEY_SPEC_ALGORITHM);
Cipher cipher = Cipher.getInstance(EncoDecode.CIPHER_ALGORITHM, EncoDecode.PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
return cipher.doFinal(fileData);
}
public static byte[] decode(SecretKey yourKey, byte[] fileData)
throws Exception {
byte[] decrypted;
Cipher cipher = Cipher.getInstance(EncoDecode.CIPHER_ALGORITHM, EncoDecode.PROVIDER);
cipher.init(Cipher.DECRYPT_MODE, yourKey, new IvParameterSpec(new byte[cipher.getBlockSize()]));
Log.d("value ", "decode() returned: " + cipher.toString());
decrypted = cipher.doFinal(fileData);
Log.d("", "decode() returned: " + decrypted.length);
return decrypted;
}
public SecretKey getSecretKey() {
String encodedKey = "8qkWUsFfdY8yy5lIad4rjw==";
byte[] decodedKey = Base64.decode(encodedKey, Base64.NO_WRAP);
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, EncoDecode.KEY_SPEC_ALGORITHM);
return originalKey;
}
String.getBytes()
, вместо этого используйтеString.getBytes(UTF-8)
или что-то еще, чтобы кодировка символов была указана явно. - person rossum   schedule 24.10.2018