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

 更新时间:2017年05月22日 11:05:26   作者:oldfeel  
本篇文章主要介绍了android实现视频的加密和解密(使用AES),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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" />

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

相关文章

  • Flutter 设置全局字体的实现

    Flutter 设置全局字体的实现

    本文主要介绍了Flutter 设置全局字体的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Android md5加密与php md5加密一致详解

    Android md5加密与php md5加密一致详解

    这篇文章主要介绍了Android md5加密与php md5加密一致详解的相关资料,需要的朋友可以参考下
    2017-05-05
  • Flutter弹性布局Flex水平排列Row垂直排列Column使用示例

    Flutter弹性布局Flex水平排列Row垂直排列Column使用示例

    这篇文章主要为大家介绍了Flutter弹性布局Flex水平排列Row垂直排列Column使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Android实现QQ登录功能

    Android实现QQ登录功能

    这篇文章主要为大家详细介绍了Android实现QQ登录功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • Android 使用PopupWindow实现弹出更多的菜单实例详解

    Android 使用PopupWindow实现弹出更多的菜单实例详解

    最近想要做一个弹出更多的菜单,而原生的弹出菜单却不是我们想要的效果,所以必然要自定义菜单。接下来通过本文给大家介绍android 使用popupwindow实现弹出更多的菜单实例详解,需要的朋友可以参考下
    2017-04-04
  • Android仿支付宝支付从底部弹窗效果

    Android仿支付宝支付从底部弹窗效果

    这篇文章主要为大家详细介绍了Android仿支付宝选择支付方式,实现支付宝付款方式,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android开发之时间日期组件用法实例

    Android开发之时间日期组件用法实例

    这篇文章主要介绍了Android开发之时间日期组件用法,主要介绍了TimePicker和DatePicker组件,对于Android程序开发有不错的借鉴价值,需要的朋友可以参考下
    2014-08-08
  • Android Menu半透明效果的开发实例

    Android Menu半透明效果的开发实例

    这篇文章主要介绍了Android Menu半透明效果方法的相关资料,需要的朋友可以参考下
    2016-09-09
  • Android Studio 设置代码提示和代码自动补全快捷键方式

    Android Studio 设置代码提示和代码自动补全快捷键方式

    这篇文章主要介绍了Android Studio 设置代码提示和代码自动补全快捷键方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • 一文彻底搞懂Kotlin中的协程

    一文彻底搞懂Kotlin中的协程

    这篇文章主要给大家介绍了Kotlin中协程的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03

最新评论