视频文件加密? [英] 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.Is我使用正确的这么大文件的方法?
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屋!