欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现视频的加密和解密(使用AES)

程序员文章站 2023-12-11 20:34:23
java语言进行加密解密速度挺慢的。。一个6mb左右的文件需要10多秒。。。等有空了瞅瞅ffmpeg去。。 mainactivity.java /** *...

java语言进行加密解密速度挺慢的。。一个6mb左右的文件需要10多秒。。。等有空了瞅瞅ffmpeg去。。

mainactivity.java

/**
 * 视频加密/解密
 * 
 * @author oldfeel
 * 
 *   created on: 2014-2-17
 */
public class mainactivity extends activity {
 // 原文件
 private static final string filepath = "/sdcard/dcim/camera/vid_20140217_144346.mp4";
 // 加密后的文件
 private static final string outpath = "/sdcard/dcim/camera/encrypt.mp4";
 // 加密再解密后的文件
 private static final string inpath = "/sdcard/dcim/camera/decrypt.mp4";

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 button encryptbutton = (button) findviewbyid(r.id.main_encrypt);
 button decryptbutton = (button) findviewbyid(r.id.main_decrypt);
 encryptbutton.setonclicklistener(new onclicklistener() {
 @override
 public void onclick(view v) {
 try {
  encrypt();
  toast.maketext(getapplicationcontext(), "加密完成",
  toast.length_short).show();
 } catch (invalidkeyexception e) {
  e.printstacktrace();
 } catch (nosuchalgorithmexception e) {
  e.printstacktrace();
 } catch (nosuchpaddingexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  e.printstacktrace();
 }
 }
 });

 decryptbutton.setonclicklistener(new onclicklistener() {
 @override
 public void onclick(view v) {
 try {
  decrypt();
  toast.maketext(getapplicationcontext(), "解密完成",
  toast.length_short).show();
 } catch (invalidkeyexception e) {
  e.printstacktrace();
 } catch (nosuchalgorithmexception e) {
  e.printstacktrace();
 } catch (nosuchpaddingexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  e.printstacktrace();
 }
 }
 });
 }

 /**
 * here is both function for encrypt and decrypt file in sdcard folder. we
 * can not lock folder but we can encrypt file using aes in android, it may
 * help you.
 * 
 * @throws ioexception
 * @throws nosuchalgorithmexception
 * @throws nosuchpaddingexception
 * @throws invalidkeyexception
 */

 static void encrypt() throws ioexception, nosuchalgorithmexception,
 nosuchpaddingexception, invalidkeyexception {
 // here you read the cleartext.
 fileinputstream fis = new fileinputstream(filepath);
 // this stream write the encrypted text. this stream will be wrapped by
 // another stream.
 fileoutputstream fos = new fileoutputstream(outpath);
 // length is 16 byte
 secretkeyspec sks = new secretkeyspec("mydifficultpassw".getbytes(),
 "aes");
 // create cipher
 cipher cipher = cipher.getinstance("aes");
 cipher.init(cipher.encrypt_mode, sks);
 // wrap the output stream
 cipheroutputstream cos = new cipheroutputstream(fos, cipher);
 // write bytes
 int b;
 byte[] d = new byte[8];
 while ((b = fis.read(d)) != -1) {
 cos.write(d, 0, b);
 }
 // flush and close streams.
 cos.flush();
 cos.close();
 fis.close();
 }

 static void decrypt() throws ioexception, nosuchalgorithmexception,
 nosuchpaddingexception, invalidkeyexception {
 fileinputstream fis = new fileinputstream(outpath);
 fileoutputstream fos = new fileoutputstream(inpath);
 secretkeyspec sks = new secretkeyspec("oldfeelwasverynb".getbytes(),
 "aes");
 cipher cipher = cipher.getinstance("aes");
 cipher.init(cipher.decrypt_mode, sks);
 cipherinputstream cis = new cipherinputstream(fis, cipher);
 int b;
 byte[] d = new byte[8];
 while ((b = cis.read(d)) != -1) {
 fos.write(d, 0, b);
 }
 fos.flush();
 fos.close();
 cis.close();
 }

}

activity_main.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingbottom="@dimen/activity_vertical_margin"
 android:paddingleft="@dimen/activity_horizontal_margin"
 android:paddingright="@dimen/activity_horizontal_margin"
 android:paddingtop="@dimen/activity_vertical_margin"
 tools:context=".mainactivity" >

 <button
  android:id="@+id/main_encrypt"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignparenttop="true"
  android:layout_centerhorizontal="true"
  android:layout_margintop="147dp"
  android:text="encrypt" />

 <button
  android:id="@+id/main_decrypt"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignright="@+id/main_encrypt"
  android:layout_centervertical="true"
  android:text="decrypt" />

</relativelayout>

androidmanifest.xml要添加读取sd的权限

复制代码 代码如下:

<uses-permission android:name="android.permission.write_external_storage" />

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: