Encryption of Video Files

Encryption of video files?

CipherInputStream and CipherOutputStream will help you do that easily. I wrote a sample program to encrypt and decrypt a video file. Modify it as per your choice...

FileInputStream fis = new FileInputStream(new File("D:/Shashank/inputVideo.avi"));
File outfile = new File("D:/Shashank/encVideo.avi");
int read;
if(!outfile.exists())
outfile.createNewFile();
File decfile = new File("D:/Shashank/decVideo.avi");
if(!decfile.exists())
decfile.createNewFile();
FileOutputStream fos = new FileOutputStream(outfile);
FileInputStream encfis = new FileInputStream(outfile);
FileOutputStream decfos = new FileOutputStream(decfile);
Cipher encipher = Cipher.getInstance("AES");
Cipher decipher = Cipher.getInstance("AES");
KeyGenerator kgen = KeyGenerator.getInstance("AES");
//byte key[] = {0x00,0x32,0x22,0x11,0x00,0x00,0x00,0x00,0x00,0x23,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
SecretKey skey = kgen.generateKey();
//Lgo
encipher.init(Cipher.ENCRYPT_MODE, skey);
CipherInputStream cis = new CipherInputStream(fis, encipher);
decipher.init(Cipher.DECRYPT_MODE, skey);
CipherOutputStream cos = new CipherOutputStream(decfos,decipher);
while((read = cis.read())!=-1)
{
fos.write((char)read);
fos.flush();
}
fos.close();
while((read=encfis.read())!=-1)
{
cos.write(read);
cos.flush();
}
cos.close();

I am generating a new key using generateKey(). You can use a byte array too, to generate your own key....

How do I encrypt Video Files in Amazon S3 Storage for Video Streaming?

There are many possible ways that a person can stream videos and secure.

  • Origin blocking (Origin, CORS, Referrer)
  • Embed busting
  • Tokenized URLs
  • Generic token
  • Advanced user specific tokens
  • Login/Paywall
  • Stream encryption
  • DRM (Digital Rights Management)

There are Modern Techniques like.

  • Domain filtering
  • Referrer filtering
  • Embed buster
  • Session token
  • AES encryption.

    Link might be helpful for you to secure the video streaming service using Nginix. https://gist.github.com/mrbar42/09c149059f72da2f09e652d4c5079919

How to encrypt & decrypt large video file

Use Below Code:

import java.io.File;
import java.io.RandomAccessFile;

public class VideoCrypt {
public static final int REVERSE_BYTE_COUNT = 1024;

public static boolean decrypt(String path) {
try {
if (path == null) return false;
File source = new File(path);
int byteToReverse = source.length() < REVERSE_BYTE_COUNT ? ((int) source.length()) : REVERSE_BYTE_COUNT;
RandomAccessFile f = new RandomAccessFile(source, "rw");
f.seek(0);
byte b[] = new byte[byteToReverse];
f.read(b);
f.seek(0);
reverseBytes(b);
f.write(b);
f.seek(0);
b = new byte[byteToReverse];
f.read(b);
f.close();
return true;
} catch (Exception e) {
return false;
}
}

public static boolean encrypt(String path) {
try {
if (path == null) return false;
File source = new File(path);
RandomAccessFile f = new RandomAccessFile(source, "rw");
f.seek(0);
int byteToReverse = source.length() < REVERSE_BYTE_COUNT ? ((int) source.length()) : REVERSE_BYTE_COUNT;
byte b[] = new byte[byteToReverse];
f.read(b);
f.seek(0);
reverseBytes(b);
f.write(b);
f.seek(0);
b = new byte[byteToReverse];
f.read(b);
f.close();
return true;
} catch (Exception e) {
return false;
}
}

private static void reverseBytes(byte[] array) {
if (array == null) return;
int i = 0;
int j = array.length - 1;
byte tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
}

How can I encrypt video file using Dart?

Here is the solution that I found. Hope it helps. Remember to add the package encryption package to pubspec.yaml

import 'dart:convert';
import 'dart:io';

import 'package:encrypt/encrypt.dart';

main() {

perfomEncryptionTasks();
}

perfomEncryptionTasks() async {
await encryptFile();
await decryptFile();
}

encryptFile() async {
File inFile = new File("video.mp4");
File outFile = new File("videoenc.aes");

bool outFileExists = await outFile.exists();

if(!outFileExists){
await outFile.create();
}

final videoFileContents = await inFile.readAsStringSync(encoding: latin1);

final key = Key.fromUtf8('my 32 length key................');
final iv = IV.fromLength(16);

final encrypter = Encrypter(AES(key));

final encrypted = encrypter.encrypt(videoFileContents, iv: iv);
await outFile.writeAsBytes(encrypted.bytes);
}

decryptFile() async {
File inFile = new File("videoenc.aes");
File outFile = new File("videodec.mp4");

bool outFileExists = await outFile.exists();

if(!outFileExists){
await outFile.create();
}

final videoFileContents = await inFile.readAsBytesSync();

final key = Key.fromUtf8('my 32 length key................');
final iv = IV.fromLength(16);

final encrypter = Encrypter(AES(key));

final encryptedFile = Encrypted(videoFileContents);
final decrypted = encrypter.decrypt(encryptedFile, iv: iv);

final decryptedBytes = latin1.encode(decrypted);
await outFile.writeAsBytes(decryptedBytes);

}

Flutter - How do I secure a video so it can only play in my App?

It seems that you are referring to encryption. Your videos will need to be encrypt(by your app obviously) so they could only be played by the app which have the encryption key (so your app) to decrypt them.
By doing so only your app will be able to display and play the encrypted videos.

To do so you will need an encryption package like encrypt.

Other post that may help you How can I encrypt video file using Dart?.

Hope this will be useful.

Encrypt video served by php to only show on certain html player

As mentioned in the comments, the typical approach to use would be DRM. This is designed to restrict access to a particular user or device rather than a type of HTML5 player, but I suspect that would work for you anyway.

If you want a lightweight, less costly but also less secure approach, you could use a 'clear key' approach along with either HLS or DASH streaming protocols.

These are similar to the major DRM solutions, encrypting the content on the server side and decrypting on the client side, but the content decryption key itself is not encrypted and is passed in an open way between the server and the client.

Given that it sounds like you are just trying to make things hard for the casual copier of your video, this may be enough for you.

Further details of both the HLS and DASH approaches here: https://stackoverflow.com/a/45103073/334402



Related Topics



Leave a reply



Submit