473,383 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

C#.NET - Decryption problem

12
I use VS2008, and I encrypted a file using this function:
Expand|Select|Wrap|Line Numbers
  1.         public static void Encrypt(string fileIn, string fileOut, string Password)
  2.         {
  3.  
  4.             FileStream fsIn = new FileStream(fileIn,
  5.                 FileMode.Open, FileAccess.Read);
  6.             FileStream fsOut = new FileStream(fileOut,
  7.                 FileMode.OpenOrCreate, FileAccess.Write);
  8.  
  9.             PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
  10.                 new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
  11.             0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  12.  
  13.             Rijndael alg = Rijndael.Create();
  14.             alg.Key = pdb.GetBytes(32);
  15.             alg.IV = pdb.GetBytes(16);
  16.  
  17.             CryptoStream cs = new CryptoStream(fsOut,
  18.                 alg.CreateEncryptor(), CryptoStreamMode.Write);
  19.  
  20.             int bufferLen = 4096;
  21.             byte[] buffer = new byte[4096];
  22.             int bytesRead;
  23.  
  24.             do
  25.             {
  26.                 bytesRead = fsIn.Read(buffer, 0, bufferLen);
  27.  
  28.                 cs.Write(buffer, 0, bytesRead);
  29.             } while (bytesRead != 0);
  30.  
  31.             cs.Close();
  32.             fsIn.Close();
  33.         }
  34.  
Now I tried to make a function that will convert an encoded BMP file to bitmap and when it gets to the return line it says "A generic error occurred in GDI+."

Here is the function:
Expand|Select|Wrap|Line Numbers
  1.         public static Bitmap DecryptToBitmap(string fileIn, string Password)
  2.         {
  3.             FileStream fsIn = new FileStream(fileIn, FileMode.Open, FileAccess.Read);
  4.  
  5.             PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
  6.                 new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
  7.             0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  8.             Rijndael alg = Rijndael.Create();
  9.  
  10.             alg.Key = pdb.GetBytes(32);
  11.             alg.IV = pdb.GetBytes(16);
  12.  
  13.             MemoryStream ms = new MemoryStream();
  14.  
  15.             CryptoStream cs = new CryptoStream(ms,
  16.                 alg.CreateDecryptor(), CryptoStreamMode.Write);
  17.  
  18.             int bufferLen = 4096;
  19.             byte[] buffer = new byte[4096];
  20.             int bytesRead;
  21.  
  22.             do
  23.             {
  24.                 bytesRead = fsIn.Read(buffer, 0, bufferLen);
  25.  
  26.                 cs.Write(buffer, 0, bytesRead);
  27.  
  28.             } while (bytesRead != 0);
  29.  
  30.             fsIn.Close();
  31.  
  32.             byte[] decryptedData = ms.ToArray();
  33.  
  34.             MemoryStream img = new MemoryStream(decryptedData);
  35.  
  36.             return new Bitmap(img);
  37.         }
  38.  
Please help me figure out the problem.
Thanks in advance.
Jul 26 '08 #1
2 1564
It sounds like something might be going wrong in your decryption/encryption scheme. Have you tried matching the byte arrays that represent the Bitmap before and after encryption?
Jul 27 '08 #2
mcco
12
It sounds like something might be going wrong in your decryption/encryption scheme. Have you tried matching the byte arrays that represent the Bitmap before and after encryption?
I have another function that decrypts the file to another file. and it works perfectly. only when I run the one that decrypts to bitmap it doesn't work..
this is the decrypt function:
Expand|Select|Wrap|Line Numbers
  1.         public static void Decrypt(string fileIn, string fileOut, string Password)
  2.         {
  3.  
  4.             FileStream fsIn = new FileStream(fileIn,
  5.                         FileMode.Open, FileAccess.Read);
  6.             FileStream fsOut = new FileStream(fileOut,
  7.                         FileMode.OpenOrCreate, FileAccess.Write);
  8.  
  9.             PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
  10.                 new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
  11.             0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  12.             Rijndael alg = Rijndael.Create();
  13.  
  14.             alg.Key = pdb.GetBytes(32);
  15.             alg.IV = pdb.GetBytes(16);
  16.  
  17.             CryptoStream cs = new CryptoStream(fsOut,
  18.                 alg.CreateDecryptor(), CryptoStreamMode.Write);
  19.  
  20.             int bufferLen = 4096;
  21.             byte[] buffer = new byte[4096];
  22.             int bytesRead;
  23.  
  24.             do
  25.             {
  26.                 bytesRead = fsIn.Read(buffer, 0, bufferLen);
  27.  
  28.                 cs.Write(buffer, 0, bytesRead);
  29.  
  30.             } while (bytesRead != 0);
  31.  
  32.             cs.Close();
  33.  
  34.             fsIn.Close();
  35.         }
  36.  
Jul 27 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jase H | last post by:
Hello, I have a ASP.NET web application problem involving the data encryption and decryption assembly(DLL) used on the connection string value that is set in the webconfig file. The problem...
1
by: Martin | last post by:
Hi Im trying to make a Client/Server where im going to encrypt the stream but i cant get it to work. I can recieve and send as long as im not trying to send/recieve encrypted. but when i am i cant...
2
by: almurph | last post by:
Hi everyone, Can you help me please? I am having a problem with the encryption/decryption of words with the Irish fada in them. The Irish fada is like this: áéíóú/ÁÉÍÓÚ. It's kind of like the...
0
by: Showjumper | last post by:
I solved my previous problem. The new one is that the password is not being decrypted but rather is still in the enrypted format. I am using Rijndael emcryption. Below is the decryption code. And...
9
by: DarkProtoman | last post by:
How would I write a function to calculate RSA Decryption key. I'm trying to write an RSA encryption program, and I'm stuck on calculating the decryption key. How do you check for coprimality?...
1
by: nithyapriya | last post by:
Hai, I'm nithya. I have a visual basic program that decrypts a file from my client using a passphrase. My seniors have no problem in the decryption. As I'm new to this concept, i could not...
0
by: Dipanwita | last post by:
I have written a RSA encryption/decryption function in c using the formula a = b^c %n. For solving the equation I have used Squaring and multiplying method for modulo exponentiation . These...
3
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt...
13
by: Tom Andrecht | last post by:
I'm trying to get some encryption/decryption routines going to take care of my data, and while the encryption is working great, I keep running into the message "Padding is invalid and cannot be...
9
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.