Java 为什么我的一种解密方法运行速度比另一种快?
我创建了加密解密方法来加密图像和视频。我正在对视频进行部分加密,准确地说是1MB。更复杂的解密方法需要很长时间才能解密android设备上的内容。然而,img_解密根本不需要很长时间。不知道他们为什么这么做 这是一个 .这个可以完全解密视频或图像。在ms中解密完整图像how ever无法解密部分加密的视频Java 为什么我的一种解密方法运行速度比另一种快?,java,android,outputstream,des,Java,Android,Outputstream,Des,我创建了加密解密方法来加密图像和视频。我正在对视频进行部分加密,准确地说是1MB。更复杂的解密方法需要很长时间才能解密android设备上的内容。然而,img_解密根本不需要很长时间。不知道他们为什么这么做 这是一个 .这个可以完全解密视频或图像。在ms中解密完整图像how ever无法解密部分加密的视频 public void img_decrypt(InputStream in, OutputStream out) { try { // Bytes read fr
public void img_decrypt(InputStream in, OutputStream out) {
try {
// Bytes read from in will be decrypted
in = new CipherInputStream(in, dcipher);
// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
while ((numRead = in.read(buf)) >= 0 ) {
out.write(buf, 0, numRead);
}
out.close();
} catch (java.io.IOException e) {
}
}
public void decrypt(InputStream in, OutputStream out) {
try {
// Bytes written to out will be decrypted
AppendableOutputStream out_append = new AppendableOutputStream(out);
System.out.println(ecipher.getOutputSize(1024*1024));
OutputStream out_d = new CipherOutputStream(out_append, dcipher);
// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
int count = 0;
int max = 1024;
boolean out_d_closed = false;
while ((numRead = in.read(buf, 0, max)) > 0) {
count += numRead;
if(count <= ecipher.getOutputSize(1024*1024)){
out_d.write(buf, 0, numRead);
out_d_closed = false;
// last encryption pass, close buffer and fix max
if(count == ecipher.getOutputSize(1024*1024)){
// fix reading 1k in case max was decreased
max = 1024;
out_d.close();
out_d_closed = true;
}
// if next read will go over a meg, read less than 1k
else if(count + max > ecipher.getOutputSize(1024*1024))
max = ecipher.getOutputSize(1024*1024) - count;
}
// past the first meg, don't decrypt
else{
out.write(buf, 0, numRead);
}
}
if(!out_d_closed){
out_d.close();
}
out.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
这是另一个。这需要永远运行。将解密完全加密的图像或部分加密的视频
public void img_decrypt(InputStream in, OutputStream out) {
try {
// Bytes read from in will be decrypted
in = new CipherInputStream(in, dcipher);
// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
while ((numRead = in.read(buf)) >= 0 ) {
out.write(buf, 0, numRead);
}
out.close();
} catch (java.io.IOException e) {
}
}
public void decrypt(InputStream in, OutputStream out) {
try {
// Bytes written to out will be decrypted
AppendableOutputStream out_append = new AppendableOutputStream(out);
System.out.println(ecipher.getOutputSize(1024*1024));
OutputStream out_d = new CipherOutputStream(out_append, dcipher);
// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
int count = 0;
int max = 1024;
boolean out_d_closed = false;
while ((numRead = in.read(buf, 0, max)) > 0) {
count += numRead;
if(count <= ecipher.getOutputSize(1024*1024)){
out_d.write(buf, 0, numRead);
out_d_closed = false;
// last encryption pass, close buffer and fix max
if(count == ecipher.getOutputSize(1024*1024)){
// fix reading 1k in case max was decreased
max = 1024;
out_d.close();
out_d_closed = true;
}
// if next read will go over a meg, read less than 1k
else if(count + max > ecipher.getOutputSize(1024*1024))
max = ecipher.getOutputSize(1024*1024) - count;
}
// past the first meg, don't decrypt
else{
out.write(buf, 0, numRead);
}
}
if(!out_d_closed){
out_d.close();
}
out.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
因为解密方法需要很长时间才能解密100KB的文件,所以设备会要求我中止或等待
如果我使用img_解密,它根本不需要时间。这对我来说毫无意义,他们在做同样的事情
我正在尝试使用解密来解密视频的第一个MB
在电脑上,一切正常
任何想法都可能有用
这两种方法都适用于解密完全加密的文件,但解密时间太长
还有一件事。decrypt对写入的数据进行解密。img_解密读取的数据。不知道会不会有什么影响
如果有人在乎,谢谢你。FilterOutputStream实现错误。孙把书写方法写错了。必须重写写操作才能正常工作。只是好奇:如果难看、令人困惑的代码也很慢,你为什么还要费劲呢?AppendableOutputStream out\u append=new AppendableOutputStreamout;过度估计关闭,因此它不会关闭父对象。进行了大量的测试。写出来是让它慢下来的原因。但是在另一种方法中out.write要快得多。使用img_解密方法完全解密的保存视频大约需要4秒钟。用另一个解密1MB大约需要20.0秒更新。out\u d.write速度较慢,因为我使用了AppendableOutputStream,因此父级关闭不会关闭。。。。。嗯,如果有人关心的话。FilterOutputStream实现错误。孙把书写方法写错了。必须重写write才能正常工作。您的意思是FilterOutputStream的write方法调用每个字节上一个参数的write方法进行输出,而此方法不使用相同的参数调用其基础输入流的write方法;FilterOutputStream的子类应该提供此方法更有效的实现?这些要点在文档中都有明确说明,因此我认为说您错误地使用了API比说,Sun将写入方法写错了。@erickson从文档中可以看出,类FilterOutputStream本身只是用将所有请求传递给底层输出流的版本覆盖OutputStream的所有方法。FilterOutputStream的子类可能会进一步覆盖其中一些方法,并提供其他方法和字段。文档声称filteredoutputstream类除了将调用传递到底层输出流之外,什么都不做,但相反,它篡改了底层对象和文档的功能,这可能是有意造成字节数组写入调用效率低下的原因。