Can't seem to figure out using cryptography classes

T

trant

I am trying just to do a basic encryption/decryption example from articles I
am reading on the net and I can't get anything to work.

I am trying the following code which I adapted from online examples:

byte[] inputData = Encoding.ASCII.GetBytes("something to encrypt");
MemoryStream outputStream = new MemoryStream();

SymmetricAlgorithm algorithm = new RijndaelManaged();
algorithm.GenerateKey();
algorithm.GenerateIV();

Console.WriteLine(Encoding.ASCII.GetString(algorithm.Key));

ICryptoTransform encryptor = algorithm.CreateEncryptor();
CryptoStream cryptoStream = new CryptoStream(outputStream,
encryptor, CryptoStreamMode.Write);
cryptoStream.Write(inputData, 0, inputData.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();

StringBuilder sb = new StringBuilder();
outputStream.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[1028];
int read;
while ((read = outputStream.Read(buffer, 0, buffer.Length)) > 0)
{
sb.Append(Encoding.ASCII.GetString(buffer, 0, read));
}
Console.WriteLine(sb.ToString());

but it's not working, it fails because the outputStream gets closed by the
cryptography API when I close the cryptoStream object.

If I comment out the lines which close the CryptoStream I get no output
because the outputStream is completely empty. It did not encrypt the text or
anything.

Maybe I am using MemoryStream wrong?

I am trying to avoid using File streams, just want to handle this in memory...
 
T

trant

Actually I got it now, it was something with the Key that was generated, I
generated by own 32 byte key and set the Key property to that and it worked.
I also found another good example online which helped me try to figure out
decryption.

But I can't get decryption working.

CryptoStream.Read is throwing the error:
Padding is invalid and cannot be removed.

I verified the bytes I passed into it are exactly the same bytes I got from
the encryption call. So not sure what this means...

byte[] cipherTextBytes = outputStream.ToArray(); // this is the bytes from
the MemoryStream used for encryption code in my original post. So it's
directly from the cryptography's output.

MemoryStream memoryStream = new MemoryStream(cipherTextBytes);

SymmetricAlgorithm algorithm = new RijndaelManaged();

byte[] keyBytes = new byte[32];
string key = "keypass";
for (int x = 0; x < key.Length; x++)
{
keyBytes[x] = (byte)key[x];
}
algorithm.Key = keyBytes;
algorithm.GenerateIV();

byte[] plainTextBytes = new byte[cipherTextBytes.Length];

ICryptoTransform encryptor = algorithm.CreateDecryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
encryptor, CryptoStreamMode.Read);
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0,
plainTextBytes.Length); // throws the error here

memoryStream.Close();

cryptoStream.Close();

string plainText = Encoding.UTF8.GetString(plainTextBytes, 0,
decryptedByteCount);


Console.WriteLine("Is it? " + plainText);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top