如何对 url 中的视频进行加密并将加密视频保存到设备的 SD 卡
在我的应用程序中,我必须使用许多视频文件。所有视频的总大小大约为 500 MB。因此,我必须在第一次安装和运行应用程序时决定我在后台下载视频,并在加密该视频后将其保存到设备的 SD 卡上。当使用视频时,我会解密该视频并使用它。
我决定这样做是因为所有视频文件都非常重要,因此该文件的安全性非常重要。
我非常努力地寻找如何加密视频和文件。将其保存到 SD 卡...
最后我发现一件事我放在下面,但在此代码中发生了权限被拒绝的错误。
代码:
public class VideoActivity extends Activity
{
VideoView video_view;
MediaController m_controller;
MediaPlayer mplayer;
String[] a = {"1","2","3","4","5","6"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
main(a);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void writeToFile(String filename, Object object) throws Exception
{
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try
{
fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/corebird1.mp4"));
oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.flush();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (oos != null)
{
oos.close();
}
if (fos != null)
{
fos.close();
}
}
}
public void main(String[] args) throws Exception
{
//
// Generating a temporary key and stire it in a file.
//
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
writeToFile("secretkey.dat", key);
Log.d("#######key", String.valueOf(key));
//
// Preparing Cipher object for encryption.
//
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
//
// Here we seal (encrypt) a simple string message (a string object).
//
SealedObject sealedObject = new SealedObject("THIS IS A SECRET MESSAGE!", cipher);
//
// Write the object out as a binary file.
//
writeToFile("sealed.dat", sealedObject);
}
}
LogCat:
08-10 12:22:32.430: WARN/System.err(338): java.io.FileNotFoundException: /mnt/sdcard/corebird1.mp4 (Permission denied)
08-10 12:22:32.430: WARN/System.err(338): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
08-10 12:22:32.459: WARN/System.err(338): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
08-10 12:22:32.459: WARN/System.err(338): at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
08-10 12:22:32.459: WARN/System.err(338): at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
08-10 12:22:32.459: WARN/System.err(338): at com.technosoft.video.VideoActivity.writeToFile(VideoActivity.java:71)
08-10 12:22:32.459: WARN/System.err(338): at com.technosoft.video.VideoActivity.main(VideoActivity.java:99)
08-10 12:22:32.459: WARN/System.err(338): at com.technosoft.video.VideoActivity.onCreate(VideoActivity.java:54)
08-10 12:22:32.459: WARN/System.err(338): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-10 12:22:32.459: WARN/System.err(338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-10 12:22:32.459: WARN/System.err(338): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-10 12:22:32.470: WARN/System.err(338): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-10 12:22:32.470: WARN/System.err(338): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-10 12:22:32.470: WARN/System.err(338): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 12:22:32.470: WARN/System.err(338): at android.os.Looper.loop(Looper.java:123)
08-10 12:22:32.470: WARN/System.err(338): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-10 12:22:32.470: WARN/System.err(338): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 12:22:32.470: WARN/System.err(338): at java.lang.reflect.Method.invoke(Method.java:521)
08-10 12:22:32.480: WARN/System.err(338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-10 12:22:32.480: WARN/System.err(338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-10 12:22:32.480: WARN/System.err(338): at dalvik.system.NativeStart.main(Native Method)
08-10 12:22:32.480: DEBUG/#######key(338): javax.crypto.spec.SecretKeySpec@69
08-10 12:22:32.510: WARN/System.err(338): java.io.FileNotFoundException: /mnt/sdcard/corebird1.mp4 (Permission denied)
08-10 12:22:32.510: WARN/System.err(338): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
08-10 12:22:32.520: WARN/System.err(338): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
08-10 12:22:32.520: WARN/System.err(338): at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
08-10 12:22:32.520: WARN/System.err(338): at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
08-10 12:22:32.520: WARN/System.err(338): at com.technosoft.video.VideoActivity.writeToFile(VideoActivity.java:71)
08-10 12:22:32.520: WARN/System.err(338): at com.technosoft.video.VideoActivity.main(VideoActivity.java:116)
08-10 12:22:32.520: WARN/System.err(338): at com.technosoft.video.VideoActivity.onCreate(VideoActivity.java:54)
08-10 12:22:32.520: WARN/System.err(338): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-10 12:22:32.520: WARN/System.err(338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-10 12:22:32.520: WARN/System.err(338): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-10 12:22:32.520: WARN/System.err(338): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-10 12:22:32.520: WARN/System.err(338): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-10 12:22:32.520: WARN/System.err(338): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 12:22:32.520: WARN/System.err(338): at android.os.Looper.loop(Looper.java:123)
08-10 12:22:32.520: WARN/System.err(338): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-10 12:22:32.520: WARN/System.err(338): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 12:22:32.520: WARN/System.err(338): at java.lang.reflect.Method.invoke(Method.java:521)
08-10 12:22:32.530: WARN/System.err(338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-10 12:22:32.530: WARN/System.err(338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-10 12:22:32.530: WARN/System.err(338): at dalvik.system.NativeStart.main(Native Method)
任何人都有此错误的解决方案,那么请帮助我...
或者如果您有任何其他解决方案或建议,那么您也可以帮助...我对此表示感谢......
我还想要解密编码视频并播放该 android 的方法。
任何身体都有其他
In my application I have to use of many video files. Approximately total size of all the video is 500 MB. So I have to decide at the first time when application is install and run at that tike I download videos in background and after encrypting that video saved it to device's SD Card. When ever video is used I decrypt that video and use it.
I decide this because all the video files are very important, so Security of that file is very important..
I try very much to find how to encrypt video & save it to SD Card...
Finally one thing I found that I put below but In this code error occurred PERMISSION DENIED.
Code:
public class VideoActivity extends Activity
{
VideoView video_view;
MediaController m_controller;
MediaPlayer mplayer;
String[] a = {"1","2","3","4","5","6"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
main(a);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void writeToFile(String filename, Object object) throws Exception
{
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try
{
fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/corebird1.mp4"));
oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.flush();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (oos != null)
{
oos.close();
}
if (fos != null)
{
fos.close();
}
}
}
public void main(String[] args) throws Exception
{
//
// Generating a temporary key and stire it in a file.
//
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
writeToFile("secretkey.dat", key);
Log.d("#######key", String.valueOf(key));
//
// Preparing Cipher object for encryption.
//
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
//
// Here we seal (encrypt) a simple string message (a string object).
//
SealedObject sealedObject = new SealedObject("THIS IS A SECRET MESSAGE!", cipher);
//
// Write the object out as a binary file.
//
writeToFile("sealed.dat", sealedObject);
}
}
LogCat:
08-10 12:22:32.430: WARN/System.err(338): java.io.FileNotFoundException: /mnt/sdcard/corebird1.mp4 (Permission denied)
08-10 12:22:32.430: WARN/System.err(338): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
08-10 12:22:32.459: WARN/System.err(338): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
08-10 12:22:32.459: WARN/System.err(338): at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
08-10 12:22:32.459: WARN/System.err(338): at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
08-10 12:22:32.459: WARN/System.err(338): at com.technosoft.video.VideoActivity.writeToFile(VideoActivity.java:71)
08-10 12:22:32.459: WARN/System.err(338): at com.technosoft.video.VideoActivity.main(VideoActivity.java:99)
08-10 12:22:32.459: WARN/System.err(338): at com.technosoft.video.VideoActivity.onCreate(VideoActivity.java:54)
08-10 12:22:32.459: WARN/System.err(338): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-10 12:22:32.459: WARN/System.err(338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-10 12:22:32.459: WARN/System.err(338): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-10 12:22:32.470: WARN/System.err(338): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-10 12:22:32.470: WARN/System.err(338): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-10 12:22:32.470: WARN/System.err(338): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 12:22:32.470: WARN/System.err(338): at android.os.Looper.loop(Looper.java:123)
08-10 12:22:32.470: WARN/System.err(338): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-10 12:22:32.470: WARN/System.err(338): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 12:22:32.470: WARN/System.err(338): at java.lang.reflect.Method.invoke(Method.java:521)
08-10 12:22:32.480: WARN/System.err(338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-10 12:22:32.480: WARN/System.err(338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-10 12:22:32.480: WARN/System.err(338): at dalvik.system.NativeStart.main(Native Method)
08-10 12:22:32.480: DEBUG/#######key(338): javax.crypto.spec.SecretKeySpec@69
08-10 12:22:32.510: WARN/System.err(338): java.io.FileNotFoundException: /mnt/sdcard/corebird1.mp4 (Permission denied)
08-10 12:22:32.510: WARN/System.err(338): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
08-10 12:22:32.520: WARN/System.err(338): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
08-10 12:22:32.520: WARN/System.err(338): at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
08-10 12:22:32.520: WARN/System.err(338): at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
08-10 12:22:32.520: WARN/System.err(338): at com.technosoft.video.VideoActivity.writeToFile(VideoActivity.java:71)
08-10 12:22:32.520: WARN/System.err(338): at com.technosoft.video.VideoActivity.main(VideoActivity.java:116)
08-10 12:22:32.520: WARN/System.err(338): at com.technosoft.video.VideoActivity.onCreate(VideoActivity.java:54)
08-10 12:22:32.520: WARN/System.err(338): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-10 12:22:32.520: WARN/System.err(338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-10 12:22:32.520: WARN/System.err(338): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-10 12:22:32.520: WARN/System.err(338): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-10 12:22:32.520: WARN/System.err(338): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-10 12:22:32.520: WARN/System.err(338): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 12:22:32.520: WARN/System.err(338): at android.os.Looper.loop(Looper.java:123)
08-10 12:22:32.520: WARN/System.err(338): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-10 12:22:32.520: WARN/System.err(338): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 12:22:32.520: WARN/System.err(338): at java.lang.reflect.Method.invoke(Method.java:521)
08-10 12:22:32.530: WARN/System.err(338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-10 12:22:32.530: WARN/System.err(338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-10 12:22:32.530: WARN/System.err(338): at dalvik.system.NativeStart.main(Native Method)
Any body have solution for this error, then please help me...
or If u have any other solution or suggestions then also u can help...I am thankful for that....
I want also method for decrypt the encode video and play that android.
Any body have any other
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您看到的错误与加密无关,与文件权限有关。您的应用程序是否请求使用 SD 卡的权限?
其次,为什么采用 DES?在现代硬件上,DES 通常比 AES 慢得多,并且密钥对于现代使用来说太短。另外,请确保您使用的是 GCM、CTR 或 CBC 操作模式 - 简单的 ECB 不具有良好的安全特性。
请注意,您正在实施经典的 DRM 方案:任何有权访问设备和应用程序私有目录的人都将能够找到密钥文件并解密视频,或窃取传输中的视频。
The error you are seeing has nothing to do with encryption and everything to do with file permissions. Does your app request permission to use the SD card?
Secondly, why DES? DES is usually substantially slower than AES on modern hardware, and has keys that are far too short for modern use. Also, make sure you are using a GCM, CTR or CBC mode of operation - simple ECB does not have good security properties.
Do realize that you are implementing a classic DRM scheme: anyone with access to the device and the application private directory will be able to locate the secret key file and decrypt the video, or steal the video in transit.