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

发布时间 - 2026-01-11 01:13:55    点击率:

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

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


# android  # 视频加密解密  # aes加密解密  # Android加密之全盘加密详解  # Android常用的数据加密方式代码详解  # Android数据传输中的参数加密代码示例  # 详解Android端与JavaWeb传输加密(DES+RSA)  # Android使用RSA加密和解密的示例代码  # Android 客户端RSA加密的实现方法  # android使用AES加密和解密文件实例代码  # Android md5加密与php md5加密一致详解  # 详解Android安全防护之加密算法  # 大家多多  # 空了  # 加密解密  # onClick  # OnClickListener  # main_decrypt  # setOnClickListener  # View  # getApplicationContext  # LENGTH_SHORT  # Toast  # makeText  # activity_main  # Button  # setContentView  # layout  # encryptButton  # main_encrypt  # DecryptButton  # findViewById 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Win11搜索栏无法输入_解决Win11开始菜单搜索没反应问题【技巧】  免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?  怎么用AI帮你设计一套个性化的手机App图标?  极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?  如何在阿里云部署织梦网站?  动图在线制作网站有哪些,滑动动图图集怎么做?  轻松掌握MySQL函数中的last_insert_id()  如何在 Pandas 中基于一列条件计算另一列的分组均值  如何在不使用负向后查找的情况下匹配特定条件前的换行符  专业商城网站制作公司有哪些,pi商城官网是哪个?  黑客入侵网站服务器的常见手法有哪些?  想要更高端的建设网站,这些原则一定要坚持!  如何用AI一键生成爆款短视频文案?小红书AI文案写作指令【教程】  Laravel如何使用.env文件管理环境变量?(最佳实践)  三星网站视频制作教程下载,三星w23网页如何全屏?  Linux系统命令中tree命令详解  Laravel项目怎么部署到Linux_Laravel Nginx配置详解  简历在线制作网站免费版,如何创建个人简历?  Laravel中的withCount方法怎么高效统计关联模型数量  如何用VPS主机快速搭建个人网站?  如何在万网ECS上快速搭建专属网站?  laravel怎么配置和使用PHP-FPM来优化性能_laravel PHP-FPM配置与性能优化方法  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  如何在Windows服务器上快速搭建网站?  Laravel如何使用Blade模板引擎?(完整语法和示例)  北京网站制作公司哪家好一点,北京租房网站有哪些?  html5如何实现懒加载图片_ intersectionobserver api用法【教程】  laravel怎么通过契约(Contracts)编程_laravel契约(Contracts)编程方法  CSS3怎么给轮播图加过渡动画_transition加transform实现【技巧】  如何用PHP工具快速搭建高效网站?  Python正则表达式进阶教程_复杂匹配与分组替换解析  Laravel如何配置和使用缓存?(Redis代码示例)  jquery插件bootstrapValidator表单验证详解  EditPlus中的正则表达式 实战(4)  佐糖AI抠图怎样调整抠图精度_佐糖AI精度调整与放大细化操作【攻略】  在线制作视频的网站有哪些,电脑如何制作视频短片?  UC浏览器如何切换小说阅读源_UC浏览器阅读源切换【方法】  香港服务器网站搭建教程-电商部署、配置优化与安全稳定指南  通义万相免费版怎么用_通义万相免费版使用方法详细指南【教程】  微信小程序 HTTPS报错整理常见问题及解决方案  微信小程序 canvas开发实例及注意事项  作用域操作符会触发自动加载吗_php类自动加载机制与::调用【教程】  微信公众帐号开发教程之图文消息全攻略  百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏  浏览器如何快速切换搜索引擎_在地址栏使用不同搜索引擎【搜索】  高防服务器租用首荐平台,企业级优惠套餐快速部署  智能起名网站制作软件有哪些,制作logo的软件?  Laravel如何实现全文搜索_Laravel Scout集成Algolia或Meilisearch教程  如何在万网自助建站中设置域名及备案?  手机怎么制作网站教程步骤,手机怎么做自己的网页链接?