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

MainActivity.java

/***视频加密/解密**@authoroldfeel**Createdon:2014-2-17*/publicclassMainActivityextendsActivity{//原文件privatestaticfinalStringfilePath="/sdcard/DCIM/Camera/VID_20140217_144346.mp4";//加密后的文件privatestaticfinalStringoutPath="/sdcard/DCIM/Camera/encrypt.mp4";//加密再解密后的文件privatestaticfinalStringinPath="/sdcard/DCIM/Camera/decrypt.mp4";@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButtonencryptButton=(Button)findViewById(R.id.main_encrypt);ButtonDecryptButton=(Button)findViewById(R.id.main_decrypt);encryptButton.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){try{encrypt();Toast.makeText(getApplicationContext(),"加密完成",Toast.LENGTH_SHORT).show();}catch(InvalidKeyExceptione){e.printStackTrace();}catch(NoSuchAlgorithmExceptione){e.printStackTrace();}catch(NoSuchPaddingExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}});DecryptButton.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){try{decrypt();Toast.makeText(getApplicationContext(),"解密完成",Toast.LENGTH_SHORT).show();}catch(InvalidKeyExceptione){e.printStackTrace();}catch(NoSuchAlgorithmExceptione){e.printStackTrace();}catch(NoSuchPaddingExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}});}/***HereisBothfunctionforencryptanddecryptfileinSdcardfolder.we*cannotlockfolderbutwecanencryptfileusingAESinAndroid,itmay*helpyou.**@throwsIOException*@throwsNoSuchAlgorithmException*@throwsNoSuchPaddingException*@throwsInvalidKeyException*/staticvoidencrypt()throwsIOException,NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException{//Hereyoureadthecleartext.FileInputStreamfis=newFileInputStream(filePath);//Thisstreamwritetheencryptedtext.Thisstreamwillbewrappedby//anotherstream.FileOutputStreamfos=newFileOutputStream(outPath);//Lengthis16byteSecretKeySpecsks=newSecretKeySpec("MyDifficultPassw".getBytes(),"AES");//CreatecipherCiphercipher=Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE,sks);//WraptheoutputstreamCipherOutputStreamcos=newCipherOutputStream(fos,cipher);//Writebytesintb;byte[]d=newbyte[8];while((b=fis.read(d))!=-1){cos.write(d,0,b);}//Flushandclosestreams.cos.flush();cos.close();fis.close();}staticvoiddecrypt()throwsIOException,NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException{FileInputStreamfis=newFileInputStream(outPath);FileOutputStreamfos=newFileOutputStream(inPath);SecretKeySpecsks=newSecretKeySpec("oldfeelwasverynb".getBytes(),"AES");Ciphercipher=Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE,sks);CipherInputStreamcis=newCipherInputStream(fis,cipher);intb;byte[]d=newbyte[8];while((b=cis.read(d))!=-1){fos.write(d,0,b);}fos.flush();fos.close();cis.close();}}

activity_main.xml

<RelativeLayoutxmlns: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"><Buttonandroid: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"/><Buttonandroid: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-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


更多相关文章

  1. Android模拟器中创建和使用SDCard
  2. Android(安卓)Studio如何导入android源码
  3. android 添加核心层服务
  4. Android开发学习笔记2---HelloWord安卓程序结构简介
  5. 原生Android应用中内嵌RN
  6. Android(安卓)unZip
  7. NDK使用log的问题
  8. android webview 加载本地文件
  9. Android(安卓)热修复 技术浅析

随机推荐

  1. Android开发工具(Android(安卓)Studio)
  2. java引入Android(安卓)NinePatch技术的意
  3. Android(安卓)多分辨率机器适配
  4. 关于Android不能启动的问题
  5. Android(安卓)在xml布局配置文件中给Butt
  6. Rockie's Android(安卓)Porting Guide(1)—
  7. Android面试题精选:讲一讲 Android(安卓)
  8. android底层开发
  9. Mac 10.12 快速下载 Android(安卓)源码
  10. Android中SQLite操作示例