视频文件加密? [英] Encryption of video files?
问题描述
我正在使用这种方法来加密视频文件:
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);
}
}
但是它给出了一个错误 Outofmemoryerror 说拒绝分配 301023321 元素.
But It gives an error Outofmemoryerror saying rejecting allocation of 301023321 element.
1.对于这么大的文件,我使用的方法是否正确?
1.Is the method I am using correct for such big files?
2.如果是,为什么会出现这个错误?有什么解决办法?
2.If yes why I am getting this error?What is solution?
3.如果没有请推荐一些好的方法?
3.If no please suggest some good method for that?
推荐答案
CipherInputStream
和 CipherOutputStream
将帮助您轻松做到这一点.我编写了一个示例程序来加密和解密视频文件.根据您的选择修改它...
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();
我正在使用 generateKey()
生成一个新密钥.您也可以使用字节数组来生成自己的密钥....
I am generating a new key using generateKey()
. You can use a byte array too, to generate your own key....
这篇关于视频文件加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!