c# - 解密长字节数组
问题描述
我想解密一个长字节 [],但我无法做到这一点。
接收代码:我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个主要问题:
错误是什么意思?我该如何解决?
如何
BitmapFactory.DecodeStream()
读取整个流?这个怎么运作?有没有办法将解密函数的返回类型更改为
byte[]
fromstring
?
解决方案
推荐阅读
- react-native - 任务“:app:bundleReleaseExpoUpdatesAssets”执行失败
- python - 如何在施工期间消除瞬变?
- mysql - 查询 s3 存储桶中的大文件
- android - 程序类型已经存在:androidx.appcompat.R$anim
- xamarin - 适用于 Xamarin 的 OneSignal SDK:没有定义 inFocusDisplayType
- python - 如何将 IANA/Olson 时区转换为 POSIX.1?
- observable - 为什么我不能用 RxJava 连接两个 Single Android Room 结果
- c# - Linq通过搜索关键字从对象的关系列表中选择对象
- r - 如何在 r 中的 ggplot 中向衰退条添加标签
- azure - 如何在 Azure CI/CD 的 Azure CLI 步骤中使用变量