本篇文章为大家展示了Android应用中怎么对视频进行加密与解密,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。MainActivity.javapublic class MainActivity e
本篇文章为大家展示了Android应用中怎么对视频进行加密与解密,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
MainActivity.java
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(); } } }); } 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(); }}
--结束END--
本文标题: android应用中怎么对视频进行加密与解密
本文链接: https://lsjlt.com/news/223238.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0