长字节数组的解密

2024-09-30 22:17:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想解密一个长字节[],但我无法做到这一点。你知道吗

接收代码:我直接从流中读取一个bitmap(按照它应该的方式工作),然后我使用MemoryStream将它转换成byte[],然后我解密整个byte[],然后解密的数据应该变成bitmap

Bitmap bmpTmp = BitmapFactory.DecodeStream(CommunicationHandler.GetStream());
MemoryStream stream = new MemoryStream();
bmpTmp.Compress(Bitmap.CompressFormat.Png, 100, stream);
string imageString = DecryptStringFromBytes_Aes(stream.ToArray());

顺便说一下,我将流读入bitmap,然后将其转换为byte[],而不是将流直接读入byte[]的唯一原因是,我没有找到任何函数将NetworkStream直接读入byte[]。 我在想,BitmapFactory.DecodeStream()是怎么工作的?为什么这个函数会读取整个数据?它怎么知道什么时候停止?你知道吗

解密代码:

public static string DecryptStringFromBytes_Aes(byte[] cipherTextCombined)
    {
        string plaintext = null;

        // Create an Aes object with the specified key and IV.
        using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = key;
                aesAlg.Padding = PaddingMode.Zeros;
                aesAlg.Mode = CipherMode.CBC;

                byte[] IV = new byte[16];
                byte[] cipherText = new byte[cipherTextCombined.Length - IV.Length];

                Array.Copy(cipherTextCombined, IV, IV.Length);
                Array.Copy(cipherTextCombined, IV.Length, cipherText, 0, cipherText.Length);

                aesAlg.IV = IV;

                // Create a decryptor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            plaintext = srDecrypt.ReadToEnd(); //The error occurs here: 
//System.ArgumentException: Offset and length were out of bounds for the array or count is 
//greater than the number of elements from index to the end of the source collection. 
            }

        return plaintext.TrimEnd('\0');
    }

加密代码:

def encrypt(self, message):
    message = self.pad(message)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(str.encode(self.key), AES.MODE_CBC, iv)
    return iv + cipher.encrypt(message)

填充代码:

def pad(s): 
    return s + b"\0" * (AES.block_size - len(s) % AES.block_size)

这是我通过网络流发送的内容:

import mss.tools

def screenshot():
    with mss.mss() as sct:
        return sct.grab(sct.monitors[1])

socket.send(mss.tools.to_png(image.rgb, image.size))

根据我得到的错误-System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.,我认为输入太长了。这是错误堆栈跟踪:at Project.LoadingImage.RunInBackground (Java.Lang.Void[] params) [0x0004d]。你知道吗

请注意,这些方法和系统在简单而简短的输入下可以完美地工作,但是唯一的问题是图像,可能是因为byte[]输入太大,正如错误所暗示的那样。你知道吗

另外,请注意,解密方法返回string,而不是byte[],这使得将其解码为bitmap更加复杂。你知道吗

最后,提出三个主要问题:

  1. 这个错误是什么意思?我该怎么解决?

  2. BitmapFactory.DecodeStream()如何读取整个流?它是如何工作的?

  3. 有没有办法把解密函数的返回类型从string改为byte[]


Tags: ofthe代码newstreamstringbytelength