简体   繁体   English

即使使用带有套接字的非阻塞密码 stream,应用程序仍然会阻塞

[英]Application still blocks even when using non-blocking cipher stream with socket

Hello I used the cipher in this post 5777105您好,我在这篇文章中使用了密码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.请注意,解密部分在 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);

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. 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套接字,以便它可以解密数据。 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.如果您在 UI 线程中运行此代码,则阻塞单元应该没有任何问题。 You can run this code on another worker thread so that your UI doesn't freeze您可以在另一个工作线程上运行此代码,这样您的 UI 就不会冻结

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM