Re: Encrypt a string to a string and vice versa
From: Matthias S. (matthias_at__e_m_v_o_i_d_.de)
Date: 10/20/04
- Next message: Adrian: "Re: if(word_a != word_b) then ..."
- Previous message: Skip: "Displaying a form from a class library: Help needed!"
- In reply to: Jon Skeet [C# MVP]: "Re: Encrypt a string to a string and vice versa"
- Next in thread: Jon Skeet [C# MVP]: "Re: Encrypt a string to a string and vice versa"
- Reply: Jon Skeet [C# MVP]: "Re: Encrypt a string to a string and vice versa"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 20 Oct 2004 18:42:21 +0200
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]
Jon Skeet [C# MVP] wrote:
> Matthias S. <matthias@_e_m_v_o_i_d_.de> wrote:
>
>>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[i]);
>>}
>>
>>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.
>
- Next message: Adrian: "Re: if(word_a != word_b) then ..."
- Previous message: Skip: "Displaying a form from a class library: Help needed!"
- In reply to: Jon Skeet [C# MVP]: "Re: Encrypt a string to a string and vice versa"
- Next in thread: Jon Skeet [C# MVP]: "Re: Encrypt a string to a string and vice versa"
- Reply: Jon Skeet [C# MVP]: "Re: Encrypt a string to a string and vice versa"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|