我的目标是,
我需要保护我的音频文件免受其他媒体播放器应用的侵害。
在我的Android媒体播放器应用程序中可以选择下载歌曲。当我在内部/外部存储器中保存时,此音频需要更安全,此音频只能通过我的应用程序播放。所以我试图加密我的音频文件并以.txt格式保存。为此,我使用了此代码段,
要加密
// Buffer used to transport the bytes from one stream to another
byte[] buf = new byte[1024];
/**
* Encrypt.
*
* @param in
* the in
* @param out
* the out
*/
public void encrypt(InputStream in, OutputStream out) {
try {
// Bytes written to out will be encrypted
out = new CipherOutputStream(out, ecipher);
// Read in the cleartext bytes and write to out to encrypt
int numRead = 0;
while ((numRead = in.read(buf)) >= 0) {
Log.e("encrypt - buffer - val", buf + "");
out.write(buf, 0, numRead);
}
out.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
**To Decrypt**
/**
* Decrypt.
*
* @param in
* the in
* @param out
* the out
*/
public void decrypt(InputStream in, OutputStream out) {
try {
// Bytes read from in will be decrypted
in = new CipherInputStream(in, dcipher);
// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
while ((numRead = in.read(buf)) >= 0) {
Log.e("decrypt - buffer - val", buf + "");
out.write(buf, 0, numRead);
}
out.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
这段代码对我来说很好,我可以知道是否能够实现我的目标。
答案 0 :(得分:0)
在您拥有音频文件的文件夹中创建一个名为“.nomedia”的文件。这样可以保护