时间:2021-05-20
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" />
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了python实现AES加密和解密的具体代码,供大家参考,具体内容如下参考:python实现AES加密和解密AES加密算法是一种对称加密算法,
本文实例讲述了java使用Hex编码解码实现Aes加密解密功能。分享给大家供大家参考,具体如下:这里的Aes加密解密方法使用Hex进行了编码解码packagec
在本教程中,我们将看到如何使用JDK中的Java密码体系结构(JCA)来实现AES加密和解密。对称密钥块密码在数据加密中起重要作用。这意味着同一密钥可用于加密和
在Java世界中,AES、DES加密解密需要使用Cipher对象构建加密解密系统,Hutool中对这一对象做再包装,简化了加密解密过程。介绍AES和DES同属对
本文实例为大家分享了python实现AES加密解密的具体代码,供大家参考,具体内容如下(1)对于AES加密解密相关知识(2)实现的功能就是输入0-16个字符,然