android 视频 加密/解密(使用AES)
16lz
2021-01-26
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"/>
更多相关文章
- Android模拟器中创建和使用SDCard
- Android(安卓)Studio如何导入android源码
- android 添加核心层服务
- Android开发学习笔记2---HelloWord安卓程序结构简介
- 原生Android应用中内嵌RN
- Android(安卓)unZip
- NDK使用log的问题
- android webview 加载本地文件
- Android(安卓)热修复 技术浅析