即使在套接字中使用非阻塞密码流,应用程序仍然会阻塞
您好,我在这篇文章中使用了密码5777105
但是解密代码仍然会阻塞,直到达到缓冲区大小。您知道另一种使其非阻塞的方法吗?请注意,解密部分在 Android 上运行。
加密部分:
CipherInputStream cis;
String salt = "1234567890123456";
String password = "abcdEFGH";
password = password.concat(salt);
String validpassword = password.substring(0, 16);
SecretKeySpec secretKey = new SecretKeySpec(validpassword.getBytes(),"AES");
AlgorithmParameterSpec paramSpec = new IvParameterSpec(salt.getBytes());
try {
// Creation of Cipher objects
Cipher encrypt =
Cipher.getInstance("AES/CFB8/NoPadding");
encrypt.init(Cipher.ENCRYPT_MODE, secretKey,paramSpec);
// Open the file
try {
fis = new FileInputStream(file);
} catch(IOException err) {
System.out.println("Cannot open file!");
return null;
}
cis = new CipherInputStream(fis, encrypt);
// Write to the Encrypted file
fos = new FileOutputStream(desFile);
byte[] b = new byte[256];
int i = cis.read(b);
while (i != -1) {
fos.write(b, 0, i);
i = cis.read(b);
}
解密部分:
CipherInputStream cis;
String salt = "1234567890123456";
String password = "abcdEFGH";
password = password.concat(salt);
String validpassword = password.substring(0, 16);
SecretKeySpec secretKey =new SecretKeySpec(validpassword.getBytes(),"AES");
AlgorithmParameterSpec paramSpec = new IvParameterSpec(salt.getBytes());
try {
// Creation of Cipher objects
Cipher decrypt =
Cipher.getInstance("AES/CFB8/NoPadding");
decrypt.init(Cipher.DECRYPT_MODE, secretKey,paramSpec);
// Open the Encrypted file
cis = new CipherInputStream(is, decrypt);
int bytesRead;
int current = 0;
byte[] b = new byte[256];
bytesRead = cis.read(b,0,256);
Hello I used the cipher in this post 5777105
But the decrypting code still blocks until the buffer size is reached. Do you know another way to make it non-blocking? Note the decrypting part is running on Android.
Encrypting part:
CipherInputStream cis;
String salt = "1234567890123456";
String password = "abcdEFGH";
password = password.concat(salt);
String validpassword = password.substring(0, 16);
SecretKeySpec secretKey = new SecretKeySpec(validpassword.getBytes(),"AES");
AlgorithmParameterSpec paramSpec = new IvParameterSpec(salt.getBytes());
try {
// Creation of Cipher objects
Cipher encrypt =
Cipher.getInstance("AES/CFB8/NoPadding");
encrypt.init(Cipher.ENCRYPT_MODE, secretKey,paramSpec);
// Open the file
try {
fis = new FileInputStream(file);
} catch(IOException err) {
System.out.println("Cannot open file!");
return null;
}
cis = new CipherInputStream(fis, encrypt);
// Write to the Encrypted file
fos = new FileOutputStream(desFile);
byte[] b = new byte[256];
int i = cis.read(b);
while (i != -1) {
fos.write(b, 0, i);
i = cis.read(b);
}
Decrypting part:
CipherInputStream cis;
String salt = "1234567890123456";
String password = "abcdEFGH";
password = password.concat(salt);
String validpassword = password.substring(0, 16);
SecretKeySpec secretKey =new SecretKeySpec(validpassword.getBytes(),"AES");
AlgorithmParameterSpec paramSpec = new IvParameterSpec(salt.getBytes());
try {
// Creation of Cipher objects
Cipher decrypt =
Cipher.getInstance("AES/CFB8/NoPadding");
decrypt.init(Cipher.DECRYPT_MODE, secretKey,paramSpec);
// Open the Encrypted file
cis = new CipherInputStream(is, decrypt);
int bytesRead;
int current = 0;
byte[] b = new byte[256];
bytesRead = cis.read(b,0,256);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cis.read 被阻止的原因很简单:密码流环绕套接字流(您将套接字流传递给密码流构造函数),因此每当您在密码流上调用 read 时,都会导致密码流中的代码从套接字以便它可以解密数据。这是代码块的位置(从套接字流读取)。
在 UI 线程中运行此代码时,您应该不会遇到阻塞单元的任何问题。您可以在另一个工作线程上运行此代码,这样您的 UI 就不会冻结
The reason for cis.read getting blocked is simple: The Cipher stream wraps around the socket stream (you pass the socket stream to Cipher stream constructor) hence whenever you call read on Cipher stream it will cause the code in cipher stream to read data from socket so that it can decrypt the data. This is where (read from the socket stream) the code blocks.
You should not have any problem with blocking unitll you are running this code in UI thread. You can run this code on another worker thread so that your UI doesn't freeze