473,756 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C#.NET - Decryption problem

12 New Member
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 1592
Celeritask
3 New Member
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 New Member
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
2470
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 occurs in the application when you instantiate a new instance of the class as shown below: ---Dim dp As DPAPIComp.DataProtectorComp = New DPAPIComp.DataProtectorComp--- where DPAPIComp is the name of the namespace referenced to in the library
1
3446
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 send, recieve or both. The Send/Recieve Code is the same on both Server and client. Send/Recieve Code: private Stream Net() { if(Encrypted) {
2
7151
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 French grave... Anyway when I run encryption on a plaintext word like:
0
275
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 here is th eline to return the decrypted password. Any help is appreciated Thanks Ashok Return s.DecryptToString(encryptedData, CType(HttpContext.Current.Application("Key"), Rijndael)) Public Shared Function DecryptToString(ByVal dataToDecrypt...
9
3849
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? Thanks!!!!! I'm using C++.
1
2162
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 proceed in it. The problem is, My client sends me the public key, say for eg, Key.txt I import the file (Key.txt) in my gpg. The public key gets stored in my gpg (pubring.gpg) But I could not find my secret key in my gpg (secring.gpg)
0
2372
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 functions work fine when Two random prime numbers (required to generate public n private key) are within certain range but fails afterwords.All the variables are of datatype ULONGLONG. I m unable to fix up the problem. Is these really a storage problem...
3
3075
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 functions. While my functions read and write files the encryption/decryption is not working properly. My test file has an original length of 66,048 bytes. My encrypted file ends up with 66,056 bytes … 8 bytes more than my original. When I...
13
3592
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 removed" on the decryption piece. From everything I can see, I am doing things correctly here My code is as follows: private const string PassStr = "MyPrivateKey"; private static readonly byte PassSalt = new byte { Byte Byte Byte Byte Byte};...
9
4817
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 . I didn't test script on windows. Here is the code, please send me your views. <?php /* Mother Eye Chipper with PHP :), Licence:GPL,
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.