加密视频文件? [英] Encryption of video files?
问题描述
public static void encryptToBinaryFile(String password,byte [] bytes ,文件文件)throws EncrypterException {
try {
final byte [] rawKey = getRawKey(password.getBytes());
final FileOutputStream ostream = new FileOutputStream(file,false);
ostream.write(encrypt(rawKey,bytes));
ostream.flush();
ostream.close();
} catch(IOException e){
throw new EncrypterException(e);
}
}
private static byte [] encrypt(byte [] raw,byte [] clear)throws EncrypterException {
try {
final SecretKeySpec skeySpec = new SecretKeySpec(raw,AES);
final Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
return cipher.doFinal(clear);
} catch(Exception e){
throw new EncrypterException(e);
}
}
但它给出错误Outofmemoryerror说拒绝分配301023321元素。
1.我正在使用这种大文件的方法吗?
2.如果是为什么我得到这个错误?什么是解决方案?
3.如果没有,请建议一些好的方法?
CipherInputStream
和 CipherOutputStream
将帮助您轻松实现。我写了一个示例程序来加密和解密视频文件。根据您的选择修改它...
FileInputStream fis = new FileInputStream(new File(D:/ Shashank / inputVideo。 AVI));
文件outfile = new File(D:/Shashank/encVideo.avi);
int读;
if(!outfile.exists())
outfile.createNewFile();
文件解析文件=新文件(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.getInstance(AES);
密码解密= 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 using this method for encrypting a video file:
public static void encryptToBinaryFile(String password, byte[] bytes, File file) throws EncrypterException {
try {
final byte[] rawKey = getRawKey(password.getBytes());
final FileOutputStream ostream = new FileOutputStream(file, false);
ostream.write(encrypt(rawKey, bytes));
ostream.flush();
ostream.close();
} catch (IOException e) {
throw new EncrypterException(e);
}
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws EncrypterException {
try {
final SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
final Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
return cipher.doFinal(clear);
} catch (Exception e) {
throw new EncrypterException(e);
}
}
But It gives an error Outofmemoryerror saying rejecting allocation of 301023321 element.
1.Is the method I am using correct for such big files?
2.If yes why I am getting this error?What is solution?
3.If no please suggest some good method for that?
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....
这篇关于加密视频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!