मैं एन्क्रिप्ट और एक सॉकेट RijndaelManaged का उपयोग करने पर एक फ़ाइल धारा डिक्रिप्ट करने के लिए कोशिश कर रहा हूँ, लेकिन मैं अपवादलंबाई अमान्य है
CryptographicException: Length of the data to decrypt is invalid. at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) at System.Security.Cryptography.CryptoStream.FlushFinalBlock() at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
अपवाद के अंत में फेंक दिया जाता है में जोड़ने से रखना प्राप्त फ़ाइल में फ़ाइल का उपयोग करें, जब पूरी फ़ाइल स्थानांतरित कर दी गई हो।
मैंने वेब पर खोज करने की कोशिश की लेकिन केवल एक स्ट्रिंग को एन्क्रिप्ट करने और डिक्रिप्ट करते समय एन्कोडिंग का उपयोग करते समय उत्पन्न होने वाली समस्याओं का जवाब मिला। मैं एक फ़ाइलस्ट्रीम का उपयोग करता हूं, इसलिए मैं उपयोग करने के लिए कोई एन्कोडिंग निर्दिष्ट नहीं करता, इसलिए यह समस्या नहीं होनी चाहिए। ये मेरी विधियां हैं:
private void transferFile(FileInfo file, long position, long readBytes)
{
// transfer on socket stream
Stream stream = new FileStream(file.FullName, FileMode.Open);
if (position > 0)
{
stream.Seek(position, SeekOrigin.Begin);
}
// if this should be encrypted, wrap the encryptor stream
if (UseCipher)
{
stream = new CryptoStream(stream, streamEncryptor, CryptoStreamMode.Read);
}
using (stream)
{
int read;
byte[] array = new byte[8096];
while ((read = stream.Read(array, 0, array.Length)) > 0)
{
streamSocket.Send(array, 0, read, SocketFlags.None);
position += read;
}
}
}
private void receiveFile(FileInfo transferFile)
{
byte[] array = new byte[8096];
// receive file
Stream stream = new FileStream(transferFile.FullName, FileMode.Append);
if (UseCipher)
{
stream = new CryptoStream(stream, streamDecryptor, CryptoStreamMode.Write);
}
using (stream)
{
long position = new FileInfo(transferFile.Path).Length;
while (position < transferFile.Length)
{
int maxRead = Math.Min(array.Length, (int)(transferFile.Length - position));
int read = position < array.Length
? streamSocket.Receive(array, maxRead, SocketFlags.None)
: streamSocket.Receive(array, SocketFlags.None);
stream.Write(array, 0, read);
position += read;
}
}
}
यह विधि है जिसे मैं सिफर स्थापित करने के लिए उपयोग करता हूं। बाइट [] init एक जेनरेट बाइट सरणी है।
private void setupStreamCipher(byte[] init)
{
RijndaelManaged cipher = new RijndaelManaged();
cipher.KeySize = cipher.BlockSize = 256; // bit size
cipher.Mode = CipherMode.ECB;
cipher.Padding = PaddingMode.ISO10126;
byte[] keyBytes = new byte[32];
byte[] ivBytes = new byte[32];
Array.Copy(init, keyBytes, 32);
Array.Copy(init, 32, ivBytes, 0, 32);
streamEncryptor = cipher.CreateEncryptor(keyBytes, ivBytes);
streamDecryptor = cipher.CreateDecryptor(keyBytes, ivBytes);
}
किसी को भी यह पता है कि मैं क्या गलत कर रहा हूं?
FlushFinalBlock() विधि कथन का उपयोग
मैं CipherMode बदल जाएगा की "बंद करने खंड" में कहा जाता है, मैं सिर्फ यह एक उदाहरण के रूप में प्रवेश किया तो तुम्हें पता है कि मैं किसी भी में मेरी सिफर को प्रारंभ नहीं है "अजीब" रास्ता। Sendfile में readBytes() अभी तक नहीं किया जाता है, मैं इसे दूर करने के लिए भूल गया था। मैंने फ़ाइल के अंत तक पढ़ा है, इसलिए यह मुद्दा यहां नहीं होना चाहिए। मैंने सोचा था कि गद्दी की देखभाल करने गया था? इसे काम करने के लिए मैं क्या बदल सकता हूं? – Patrickयदि समेकित स्ट्रीम रीड मोड में है, तो यदि आप इसका निपटान करते हैं तो अंतिम ब्लॉक खो जाएगा; अंतिम ब्लॉक का उत्पादन करने के लिए इसे वास्तव में अपनी अंतर्निहित स्रोत धारा से फ़ाइल का अंत पढ़ना होगा। –
जेफरी के जवाब में: यदि मैं stream.FlushFinalBlock फोन करने की कोशिश() यह कहता है NonSupportedException: FlushFinalBlock दो बार एक ही धारा पर नहीं कहा जा सकता। इसका मतलब यह नहीं है कि फ़ाइल का अंत पढ़ा गया है (और भेजा गया है)? – Patrick