即使在套接字中使用非阻塞密码流,应用程序仍然会阻塞

发布于 2024-11-05 13:10:08 字数 2134 浏览 0 评论 0原文

您好,我在这篇文章中使用了密码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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零時差 2024-11-12 13:10:08

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文