Mister E Posted August 14, 2004 Posted August 14, 2004 (edited) I have this method, with takes in path to a file on the system, encrypts it, and then writes the encrypted file to disk using the file path as a name and ".enc" extension. It works fine, and I am able to decrypt the file later with a different function:public void Encrypt(string fileName) { FileStream fsCrypt=new FileStream(fileName + ".enc", FileMode.Create); RijndaelManaged RMCrypto = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key,iv), CryptoStreamMode.Write); FileStream fsIn=new FileStream(fileName,FileMode.Open); int data; while ((data=fsIn.ReadByte())!=-1) cs.WriteByte((byte) data); fsIn.Close(); cs.Close(); fsCrypt.Close(); }Here's the problem. Instead of writing to disk, I want to get the encrypted stream directly into a byte[] array. I'm trying to do this as follows:public byte[] Encrypt(byte[] plainFile) { MemoryStream cryptoStream = new MemoryStream(); Stream fsCrypt = (Stream)cryptoStream; RijndaelManaged RMCrypto = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(this.key,this.iv), CryptoStreamMode.Write); Stream fsIn = (Stream)new MemoryStream( plainFile ); int data; while ((data=fsIn.ReadByte())!=-1) cs.WriteByte((byte) data); byte[] temp = new byte[ cryptoStream.Length ]; int count = 0; while ((data=cryptoStream.ReadByte())!=-1) { temp[count++] = (byte)data; } fsIn.Close(); cs.Close(); fsCrypt.Close(); return temp; }This seems to work, but the returned byte[] does not appear to be the correct size -- even though I am using the exact same key (this.key) and initialization vector (this.iv) in both methods. Unencrypted file: 327,286 bytes Encrypted file #1: 327,296 bytes (first method) Encrypted file #2: 327,280 bytes (second method) When I try to decyrpt the #2 file, I get the following exception: PKCS7 padding is invalid and cannot be removed I'm obviously not doing something right in method #2. The byte[] in #2 is exactly 16 bytes smaller. I don't know if this is a conicidence, but that is the size of the Key and IV in the Rijndael class. Any help is appreciated. Thanks... Edited August 14, 2004 by Mister E Quote
Joe Mamma Posted August 14, 2004 Posted August 14, 2004 did you look at this? http://support.microsoft.com/default.aspx?scid=kb;en-us;842791 Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Mister E Posted August 14, 2004 Author Posted August 14, 2004 Hmmm... no, I didn't. But that is interesting. It really doesn't apply to me though, as I KNOW that I'm using the same IV. Thanks anyway. I found a better way to do it: public byte[] Encrypt(byte[] plainFile) { RijndaelManaged myRijndael = new RijndaelManaged(); ICryptoTransform encryptor = myRijndael.CreateEncryptor( this.key, this.iv ); MemoryStream msEncrypt = new MemoryStream(); CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write); csEncrypt.Write(plainFile, 0, plainFile.Length); csEncrypt.FlushFinalBlock(); return msEncrypt.ToArray(); } public byte[] Decrypt(byte[] encryptedFile) { RijndaelManaged myRijndael = new RijndaelManaged(); ICryptoTransform decryptor = myRijndael.CreateDecryptor(this.key,this.iv); MemoryStream msDecrypt = new MemoryStream(encryptedFile); CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read); byte[] fromEncrypt = new byte[encryptedFile.Length]; csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); return fromEncrypt; } Quote
Khallaf Posted August 22, 2009 Posted August 22, 2009 You can check my solution to this problem and more here... http://www.codeproject.com/KB/IP/2WRCS.aspx Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.