亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android應用中怎么對視頻進行加密與解密

發布時間:2020-12-05 16:06:31 來源:億速云 閱讀:186 作者:Leah 欄目:移動開發

本篇文章為大家展示了android應用中怎么對視頻進行加密與解密,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

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應用中怎么對視頻進行加密與解密,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

云安县| 双峰县| 十堰市| 遂溪县| 碌曲县| 芷江| 从江县| 宁陕县| 都匀市| 田东县| 内乡县| 孝义市| 舟曲县| 罗源县| 多伦县| 萨迦县| 巢湖市| 洛扎县| 额济纳旗| 剑川县| 平阳县| 团风县| 鲜城| 锦州市| 新宁县| 荔浦县| 汉阴县| 乌什县| 景谷| 收藏| 甘孜| 丽江市| 天全县| 若尔盖县| 儋州市| 东莞市| 霞浦县| 方山县| 开阳县| 五莲县| 榆林市|