SoFunction
Updated on 2025-04-05

Android implements encryption and decryption of videos (using AES)

Java language encryption and decryption speed is quite slow. . It takes more than 10 seconds to have a file of about 6MB. . . When you have time, take a look at ffmpeg. .

/**
  * Video encryption/decryption
  *
  * @author oldfeel
  *
  * Created on: 2014-2-17
  */
public class MainActivity extends Activity {
 // Original file private static final String filePath = "/sdcard/DCIM/Camera/VID_20140217_144346.mp4";
 // Encrypted file private static final String outPath = "/sdcard/DCIM/Camera/encrypt.mp4";
 // Encrypt and decrypted file private static final String inPath = "/sdcard/DCIM/Camera/decrypt.mp4";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_main);
 Button encryptButton = (Button) findViewById(.main_encrypt);
 Button DecryptButton = (Button) findViewById(.main_decrypt);
 (new OnClickListener() {
 @Override
 public void onClick(View v) {
 try {
  encrypt();
  (getApplicationContext(), "Encryption completed",
  Toast.LENGTH_SHORT).show();
 } catch (InvalidKeyException e) {
  ();
 } catch (NoSuchAlgorithmException e) {
  ();
 } catch (NoSuchPaddingException e) {
  ();
 } catch (IOException e) {
  ();
 }
 }
 });

 (new OnClickListener() {
 @Override
 public void onClick(View v) {
 try {
  decrypt();
  (getApplicationContext(), "Decryption is completed",
  Toast.LENGTH_SHORT).show();
 } catch (InvalidKeyException e) {
  ();
 } catch (NoSuchAlgorithmException e) {
  ();
 } catch (NoSuchPaddingException e) {
  ();
 } catch (IOException e) {
  ();
 }
 }
 });
 }

 /**
 * 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 = ("AES");
 (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 = (d)) != -1) {
 (d, 0, b);
 }
 // Flush and close streams.
 ();
 ();
 ();
 }

 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 = ("AES");
 (Cipher.DECRYPT_MODE, sks);
 CipherInputStream cis = new CipherInputStream(fis, cipher);
 int b;
 byte[] d = new byte[8];
 while ((b = (d)) != -1) {
 (d, 0, b);
 }
 ();
 ();
 ();
 }

}

activity_main.xml

<RelativeLayout xmlns:andro
 xmlns:tools="/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:
  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:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignRight="@+id/main_encrypt"
  android:layout_centerVertical="true"
  android:text="Decrypt" />

</RelativeLayout>

To add permission to read sd

Copy the codeThe code is as follows:

<uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.