我有一个服务器/客户端系统,它加密服务器(python)上的图像并在客户端(c#)上解密它们。加密部分工作得很好,除了尝试解密几个块长的信息(例如,图像,而不是“Hello World”字符串)。
因此,当我尝试通过 CryptoStream 解密加密图像时 - 有时它可以工作,但由于某种原因 - 有时它不会。
这是图像的解密过程:
public static byte[] DecryptBytesFromBytes(byte[] encryptedImage, byte[] key)
{
byte[] decryptedImage;
// 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[] cipherBytes = new byte[encryptedImage.Length - IV.Length];
Array.Copy(encryptedImage, IV, IV.Length);
Array.Copy(encryptedImage, IV.Length, cipherBytes , 0, cipherBytes.Length);
aesAlg.IV = IV;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (var input = new MemoryStream(cipherBytes))
using (var output = new MemoryStream())
{
using (var cryptStream = new CryptoStream(input, decryptor, CryptoStreamMode.Read))
{
var buffer = new byte[1024];
var read = cryptStream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
output.Write(buffer, 0, read);
read = cryptStream.Read(buffer, 0, buffer.Length); // Error here
}
decryptedImage= output.ToArray();
}
}
return decryptedImage;
}
如果它有帮助,即使在执行以下操作时我也会遇到相同的错误:
cryptStream.CopyTo(output);
这是用于加密的python代码:
@staticmethod
def pad(s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
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)
例外
有时我会收到此错误: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 System.Buffer.BlockCopy (System.Array src, System.Int32 srcOffset, System.Array dst, System.Int32 dstOffset, System.Int32 count) [0x00097] in <d4a23bbd2f544c30a48c44dd622ce09f>:0
at Mono.Security.Cryptography.SymmetricTransform.InternalTransformBlock (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount, System.Byte[] outputBuffer, System.Int32 outputOffset) [0x000b0] in <d4a23bbd2f544c30a48c44dd622ce09f>:0
at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00020] in <d4a23bbd2f544c30a48c44dd622ce09f>:0
at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x0002e] in <d4a23bbd2f544c30a48c44dd622ce09f>:0
at System.Security.Cryptography.CryptoStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x002e3] in <d4a23bbd2f544c30a48c44dd622ce09f>:0
at Project.Crypto.DecryptBytesFromBytes (System.Byte[] encryptedImage, System.Byte[] key) [0x000b9] in C:\Users\user\Desktop\Project\Crypto.cs:129
注意Project.Crypto.DecryptBytesFromBytes
是解密函数。
有谁知道它为什么会发生,我该如何解决?如果需要更多信息,请告诉我。
这是我在函数文档中找到的Read()
:
// Exceptions:
// T:System.ArgumentException:
// Thesum of the count and offset parameters is longer than the length of the buffer.
顺便说一句,我使用 CBC 模式和零填充。
我想澄清最后一件事——我做这整个过程只是为了我可以获取加密的字节,然后把它们变成解密的字节。如果有另一种方法可以在 c# 中将字节解密为字节,请告诉我。