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

  

AndroidManifest.xml要添加读取sd的权限

复制代码 代码如下:



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

以上就是android实现视频的加密和解密(使用AES)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动