首页 > 解决方案 > 解密长字节数组

问题描述

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

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

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

顺便说一句,我将流读入 abitmap然后将其转换为 abyte[]而不是将流直接读入 a 的唯一原因byte[]是因为我没有找到任何将 a 读NetworkStream入 a 的函数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)

这是我通过 NetworkStream 发送的:

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.

作为结论,3个主要问题:

  1. 错误是什么意思?我该如何解决?

  2. 如何BitmapFactory.DecodeStream()读取整个流?这个怎么运作?

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

标签: c#pythonencryptioncryptographyaes

解决方案


推荐阅读