android實(shí)現(xiàn)視頻的加密和解密(使用AES)
java語(yǔ)言進(jìn)行加密解密速度挺慢的。。一個(gè)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的權(quán)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)繪制折線圖APP代碼
大家好,本篇文章主要講的是Android實(shí)現(xiàn)繪制折線圖APP代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02Android軟鍵盤的顯示隱藏功能實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了Android軟鍵盤的顯示隱藏功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
這篇文章介紹了Android中自定義對(duì)話框(Dialog)的實(shí)例代碼,有需要的朋友可以參考一下2013-08-08Android使用ViewDragHelper實(shí)現(xiàn)圖片下拽返回示例
這篇文章主要介紹了Android使用ViewDragHelper實(shí)現(xiàn)圖片下拽返回示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-082014值得推薦的10個(gè)移動(dòng) Web 應(yīng)用程序開(kāi)發(fā)框架
今天這篇文章向大家推薦10大優(yōu)秀的移動(dòng) Web 開(kāi)發(fā)框架,幫助開(kāi)發(fā)者更加高效的開(kāi)發(fā)移動(dòng)Web應(yīng)用。2014-08-08Android編程之ListPreference用法實(shí)例分析
這篇文章主要介紹了Android編程之ListPreference用法,結(jié)合實(shí)例形式較為詳細(xì)的分析說(shuō)明了ListPreference的功能、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2015-12-12Android 中CheckBox多項(xiàng)選擇當(dāng)前的position信息提交的示例代碼
這篇文章主要介紹了Android 中CheckBox多項(xiàng)選擇當(dāng)前的position信息提交的示例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-07-07