hi john,
sorry if I didn't make myself clear enough. The encryption method works
fine (at least I don't get an exception anywhere, if the string is
correctly encryption is left aside). The problem lies in the
Decryption-part of the task, where I'm just creating a MemoryStream by
converting the result of my previously mentioned encryption method using
Convert.FromBase64String().
The problem is, as soon as I call ReadByte on the CryptoStream, I get the
CryptographicException mentioned below.
+++
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(sSource));
CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(),
CryptoStreamMode.Read);
for (int i = 0; i < ms.Length; i++) {
// the next line with throw a CryptographicException with the
// message: "Padding is invalid and can not be removed."
cs.ReadByte();
+++
As to your answer on the Read-Method, I actually don't know how to Read
*anything* from the stream if I can't retrieve the current Position or
Length (both not provided in the CryptoStream).
btw, big thanks to you for helping me out on this one.
kind regards,
matthias
--
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]
Matthias S. said:
thanks for your reply which was of big help. So far I managed to encrypt
the string but running into problems when decrypting:
Here is how I encrypt:
+++
// sSource contains the string to be encrypted
UTF8Encoding utf8 = new UTF8Encoding();
byte[] utf8Bytes = utf8.GetBytes(sSource);
// encrypt
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(),
CryptoStreamMode.Write);
for (int i = 0; i < utf8Bytes.Length; i++) {
cs.WriteByte(utf8Bytes);
}
cs.Flush();
You're neither calling Close() nor FlushFinalBlock() on the
CryptoStream. That may well be the problem.
By the way: using ReadByte and WriteByte is a pretty painful way of
reading and writing data. Use the forms which read and write blocks of
data at a time, paying attention to the return value from Read.