- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
尝试涉及部分加密文件的操作。除了这个小问题,一切都很好。因为某些原因。刷新方法一直有效,直到 n > 52,其中 n 是循环计数。可以在解密方法中看到。如果我将 n fomr < 10 更改为 < 53 它会刷新。我通过查看文件进行了测试。直到53才添加新内容。但是应该有。
public class DesEncrypter {
Cipher ecipher;
Cipher dcipher;
DesEncrypter(SecretKey key) {
// Create an 8-byte initialization vector
byte[] iv = new byte[]{
(byte)0x8E, 0x12, 0x39, (byte)0x9C,
0x07, 0x72, 0x6F, 0x5A
};
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
try {
ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
// CBC requires an initialization vector
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
} catch (java.security.InvalidAlgorithmParameterException e) {
} catch (javax.crypto.NoSuchPaddingException e) {
} catch (java.security.NoSuchAlgorithmException e) {
} catch (java.security.InvalidKeyException e) {
}
}
// Buffer used to transport the bytes from one stream to another
byte[] buf = new byte[1024];
public void encrypt(InputStream in, OutputStream out) {
try {
// Bytes written to out will be encrypted
AppendableOutputStream out_append = new AppendableOutputStream(out);
OutputStream out_c = new CipherOutputStream(out_append, ecipher);
// Read in the cleartext bytes and write to out to encrypt
int numRead = 0;
int count = 0;
int max = 1024;
boolean first = true;
while ((numRead = in.read(buf, 0, max)) > 0) {
System.out.println("running Total: " + count);
count += numRead;
// if this read puts as at less than a meg, encrypt
if(count <= 1024*1024){
System.out.println("encrypted " + numRead + " of " + max +" bytes : total " + count);
out_c.write(buf, 0, numRead);
// last encryption pass, close buffer and fix max
if(count == 1024*1024){
// fix reading 1k in case max was decreased
max = 1024;
out_c.close();
}
// if next read will go over a meg, read less than 1k
else if(count + max > 1024*1024)
max = 1024*1024 - count;
}
// past the first meg, don't encrypt
else{
System.out.println("processed " + numRead + " of " + max +" bytes : total " + count);
out.write(buf, 0, numRead);
}
}
out.close();
} catch (java.io.IOException e) {}
}
// Movies encrypt only 1 MB 128 passes.
public void decrypt(InputStream in, OutputStream out) {
try {
// Bytes read from in will be decrypted
InputStream in_c = new CipherInputStream(in, dcipher);
// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
int count = 0;
int max = 1024;
while ((numRead = in_c.read(buf, 0, max)) > 0) {
count += numRead;
System.out.println("decrypted " + numRead + " of " + max +" bytes : total " + count);
out.write(buf, 0, numRead);
if(count + max > 1024*1024){
max = 1024*1024 - count;
}
if(count == 1024*1024)
max = 0;
}
//in.skip(count);
int n = 0;
while((numRead = in.read(buf)) > 0 && n < 10){
count += numRead;
System.out.println("processed " + numRead + " of 1024 bytes : total " + count);
out.write(buf,0,numRead);
//System.out.println("buf"+buf.length +" numered" + numRead+ " n"+n);
// If i look at the file after anything under n < 51 the file doesn't change.
n++;
}
out.flush();
out.close();
} catch (java.io.IOException e) {
System.out.println("AHHHHHHHH!!!!!!");
}
}
最佳答案
根据给定的信息,我只能推测。至少如果您指定 OutputStream
的类型会非常有帮助。 out
当您调用 decrypt
时方法。 flush()
的行为不同的具体实现方法不同。
例如,如果您的输出流恰好是 CipherOutputStream
,或连接到该类型流的其他一些流,则其 flush()
的文档方法说明以下内容(我强调的重要部分):
Flushes this output stream by forcing any buffered output bytes that have already been processed by the encapsulated cipher object to be written out. Any bytes buffered by the encapsulated cipher and waiting to be processed by it will not be written out. For example, if the encapsulated cipher is a block cipher, and the total number of bytes written using one of the write methods is less than the cipher's block size, no bytes will be written out.
这听起来确实可能是您问题的原因。但是,正如我所说,了解有关您的特定流类型的更多详细信息将非常有帮助。
关于java - OutputStream 的 Flush 方法不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9895299/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!