国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android實現視頻的加密和解密(使用AES)

2019-10-23 18:28:17
字體:
來源:轉載
供稿:網友

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/209856.html">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" />

 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定安县| 红安县| 宜都市| 洛隆县| 西吉县| 桑植县| 开封县| 始兴县| 蓬莱市| 清新县| 吐鲁番市| 凤冈县| 子洲县| 张家口市| 西平县| 中超| 理塘县| 山东省| 宜兴市| 四平市| 玉溪市| 龙江县| 长宁区| 邵东县| 聂荣县| 弥勒县| 海原县| 镇沅| 松桃| 江川县| 长丰县| 新密市| 开原市| 崇左市| 昌图县| 含山县| 岳池县| 隆化县| 鄱阳县| 宾阳县| 长岛县|