当前位置: 首页 > 编程笔记 >

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

曹浩淼
2023-03-14
本文向大家介绍android实现视频的加密和解密(使用AES),包括了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" />


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

 类似资料:
  • 本文向大家介绍使用js实现摩斯密码的加密和解密相关面试题,主要包含被问及使用js实现摩斯密码的加密和解密时的应答技巧和注意事项,需要的朋友参考一下 网络上看到的,很长

  • 我正在尝试使用CryptoJS在JavaScript中进行加密,在C#中进行解密。花了很多时间试图让两种技术返回相同的输出。但是,输出是不同的--CryptoJS产生的加密字符串不同于C#产生的加密字符串。我做错了什么?谢谢你的帮助。

  • 我正在做一个简单的程序来加密/解密使用RSA算法在Java。我创建一个密码对象如下: 我通过调用加密函数进行加密: 解密过程如下: 但是,当我将创建密码对象的代码编辑为://Create a Cipher object Cipher rsapier=Cipher时。getInstance(“RSA”); 问题出在哪里。在第一种情况下(当空格出现时),我指定了NoPadd?为什么空格出现在解密的消息

  • 本文向大家介绍Java使用AES加密和解密的实例详解,包括了Java使用AES加密和解密的实例详解的使用技巧和注意事项,需要的朋友参考一下 Java使用AES加密和解密的实例详解 前言: AES的基本要求是,采用对称分组密码体制,密钥长度的最少支持为128、192、256,分组长度128位,算法应易于各种硬件和软件实现。1998年NIST开始AES第一轮分析、测试和征集,共产生了15个候选算法。1

  • 本文向大家介绍android中对文件加密解密的实现,包括了android中对文件加密解密的实现的使用技巧和注意事项,需要的朋友参考一下 现在项目里面有一个需求,本项目里面下载的视频和文档都不允许通过其他的播放器播放,在培训机构里面这样的需求很多。防止有人交一份钱,把所有的课件就拷给了别人。这样的事情培训机构肯定是不愿意的。现在我项目里面也出了这么个需求。下面介绍一下我的实现。 文件加解密的流程及原

  • 本文向大家介绍python实现移位加密和解密,包括了python实现移位加密和解密的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了python实现移位加密和解密的具体代码,供大家参考,具体内容如下 代码很简单,就不多做解释啦。主要思路是将字符串转为Ascii码,将大小写字母分别移位密钥表示的位数,然后转回字符串。需要注意的是,当秘钥大于26的时候,我使用循环将其不断减去26,直到密钥