ExoPlayerで再生する前に復号化する必要があるAES暗号化されたビデオはほとんどありません。これらの動画はアプリのアセットフォルダに含まれていますが、SDカードに保存する必要はほとんどありません。ファイルの復号中に最後のブロックが復号化に不完全になる
私は動画を解読するために提供されたユーティリティクラスを使用していますが、正しく動作していないようです。資産フォルダ内の動画の
static String key = "xxx"; // key should be exactly 16bit long
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES";
public static void encrypt(File inputFile, File outputFile) throws CryptoException {
doCrypto(Cipher.ENCRYPT_MODE, inputFile, outputFile);
}
public static void decrypt(File inputFile, File outputFile) throws CryptoException {
doCrypto(Cipher.DECRYPT_MODE, inputFile, outputFile);
}
private static void doCrypto(int cipherMode, File inputFile, File outputFile) throws CryptoException {
try {
Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(cipherMode, secretKey);
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();
} catch (IOException | NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
e.printStackTrace();
throw new CryptoException("Error encrypting/decrypting file", e);
}
}
、私は直接のInputStreamがgetAssets().open(filePath)
から返さ渡そうとしました、しかし、それは、パディングに関連するいくつかのエラーが発生しました。だから私が代わりに
public static void copyFromAssets(Context context, String filePath, File outputFile) {
InputStream in = null;
OutputStream out = null;
try {
in = context.getAssets().open(filePath);
out = new FileOutputStream(outputFile);
copyFile(in, out);
} catch (IOException e) {
e.printStackTrace();
LumberJack.e("tag", "Failed to copy asset file: " + filePath);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
private static void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
ビデオがあった最初の次のコードを使用して内部ストレージにビデオファイルをコピーしたが、それは暗号化されたビデオですので、私はそれを再生することができませんでした。抽出したビデオファイルを解読しようとしたとき、次の例外があります。 -
javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:853)
at javax.crypto.Cipher.doFinal(Cipher.java:1502)
at com.example.utilities.CryptoUtils.doCrypto(CryptoUtils.java:42)
at com.example.utilities.CryptoUtils.decrypt(CryptoUtils.java:29)
at com.example.activities.HomeActivity.onVideoPlayButtonClick(HomeActivity.java:107)
at java.lang.reflect.Method.invoke(Native Method)
at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:485)
at org.greenrobot.eventbus.EventBus.postToSubscription(EventBus.java:420)
at org.greenrobot.eventbus.EventBus.postSingleEventForEventType(EventBus.java:397)
at org.greenrobot.eventbus.EventBus.postSingleEvent(EventBus.java:370)
at org.greenrobot.eventbus.EventBus.post(EventBus.java:251)
at com.example.viewmodels.BaseDataLevelItemView$1.onClick(BaseDataLevelItemView.java:65)
at android.view.View.performClick(View.java:5210)
at android.view.View$PerformClick.run(View.java:21169)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
私はここで何が間違っているのか分かりません。同じ解読コードが、同じビデオで以前のバージョンのアプリで使用されていましたが、現在は問題があります。私は同じ例外に関連するStackoverflowのいくつかの他の答えでチェックしたが、それらのほとんどは暗号テキストの問題をエンコードに関連していた。私はここに文字列がありませんが、ファイルはありません。
ここで何が間違っているのか、どのように修正できますか?既に問題を見つけた場合は、どうすればよいですか?
ファイルはAndroidでも暗号化されていましたか?上記のコードを使用して?また、実際には何バイトが実際に読み込まれているかを調べるために、戻り値をチェックすることで、javadocが言うべき方法でInputstream.read()を使うべきです。 –
@JamesKPolkわかりませんが、おそらくはいです。 – noob
'read()'の戻り値を確認してください。おそらく、あなたのファイル設定がうまくいきません。変換、モード、およびIVを再確認する必要があります。 –