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


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.
> 


Relevant Pages

  • Re: Encryption / string encoding
    ... commented out my calls to the encryption stuff.... ... >> later date decrypting the string. ... >> Dim msEnc As New MemoryStream ... >> Public Function EncryptStream(ByVal PlainStream As MemoryStream) As ...
    (microsoft.public.dotnet.security)
  • Re: RijndaelManaged and Rijndael CryptoTransforms do not support CFB or OFB CrytoModes
    ... > transform instance the same IV will be used to start off on the first ... In CFB mode, it is *vital* that you rotate IVs as often as possible. ... can induce plain text data into your encryption process himself. ... > message to its respective CryptoStream I will call FlushFinalBlock. ...
    (microsoft.public.dotnet.security)
  • Re: DES and UUEncoded
    ... using DES encryption is easy. ... decryption. ... > Then you hook up a CryptoStream to that memory stream, ... > decrypted output will be avaliable in the memory stream. ...
    (microsoft.public.dotnet.security)
  • Re: Im having problems with cryptography and sockets, help
    ... > I made many changes to the original code after I posted, ... Then, after looking some example code for encryption, I tried the ... > FlushFinalBlock or close the cryptostream, ... so that things like ReadToEnd will complete. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Encrypt a string to a string and vice versa
    ... All I want to do is to encrypt a string into an encrypted ... The encryption libraries in .NET are ... Look at CryptoStream for some sample code. ... Convert the resulting binary data into a string again. ...
    (microsoft.public.dotnet.languages.csharp)