RijndaelManaged and Rijndael CryptoTransforms do not support CFB or OFB CrytoModes

From: Chris (chrisNnOoSsPpAaMmtanger_at_technocisive.com)
Date: 06/14/04


Date: Mon, 14 Jun 2004 11:46:44 -0500


    Has anyone been able to get the Rijndael or RijndaelManaged encryptors
to work with CFB or OFB modes. I wish to use the Rijndael algorithm as a
stream cipher, however it does not look the the implementations of these
classess will work in .NET. I need to send data right away even if it does
not have a full block of cipherText yet. Ultimately I will use a
NetworkStream instead of a MemoryStream. If I could call FlushFinalBlock
multiple times that would also work (although it would be less size
efficient than CFB mode). I wish to avoid constantly recreating crypto
streams etc. Am I doing something wrong or is CFB really not supported in
.NET.

-Chris
byte[] IV = null;

byte[] key = null;

CipherMode mode = CipherMode.CBC;

// If I use CFB mode I get an exception when creating the Encryptor
ICryptoTransform

// CipherMode mode = CipherMode.CFB;

string plainText = textBox1.Text;

byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(plainText);

int times = 100000;

RijndaelManaged rme = new RijndaelManaged();

//Rijndael rme = Rijndael.Create();

rme.Mode = mode;

rme.GenerateKey();

key = rme.Key;

rme.GenerateIV();

IV = rme.IV;

ICryptoTransform rmet = rme.CreateEncryptor();

MemoryStream ms = new MemoryStream();

CryptoStream cs = new CryptoStream(ms,rmet,CryptoStreamMode.Write);

BinaryWriter bwe = new BinaryWriter(cs);

syncQ.Enqueue("Starting encryption loop " + times.ToString() + " times");

for(int i = 0; i < times; i++)

{

bwe.Write((int)plainBytes.Length);

bwe.Write(plainBytes,0,plainBytes.Length);

bwe.Flush();

cs.Flush();

// Can't do this more than once without getting an exception while
decrypting, so CBC mode will not do what I want

//cs.FlushFinalBlock();

}

cs.FlushFinalBlock();

syncQ.Enqueue("Encryption loop completed");

plainBytes = null;

RijndaelManaged rmd = new RijndaelManaged();

//Rijndael rmd = Rijndael.Create();

rmd.Key = key;

rmd.IV = IV;

rmd.Mode = mode;

ICryptoTransform rmdt = null;

rmdt = rmd.CreateDecryptor();

ms.Position = 0;

CryptoStream csd = new CryptoStream(ms,rmdt,CryptoStreamMode.Read);

BinaryReader brd = new BinaryReader(csd);

syncQ.Enqueue("Starting decryption loop " + times.ToString() + " times");

for(int i = 0; i < times; i++)

{

int bytesLen = brd.ReadInt32();

plainBytes = brd.ReadBytes(bytesLen);

}

syncQ.Enqueue("Decryption loop completed");

plainText = null;



Relevant Pages


Quantcast