By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,186 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,186 IT Pros & Developers. It's quick & easy.

image encryption problem, please help!!

P: n/a
Hello, I am having a problem encrypting a tiff image..it always loses
information after i decrypt and I know it has something to do with the
encoding but i can't figure it out. Any help would be appreciated..
Here is the encrypt function:

private byte[] Encrypt(byte[] bytes)
{
key = new byte[8];
iv = new byte[8];
for (int x = 0; x < 8; x++)
{
key[x] = Convert.ToByte(textBox3.Text[x]);
iv[x] = Convert.ToByte(textBox4.Text[x]);

}
DESCryptoServiceProvider cryptProvider = new
DESCryptoServiceProvider();
ICryptoTransform transform = cryptProvider.CreateEncryptor(key, iv);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, transform,
CryptoStreamMode.Write);
cs.Write(bytes, 0, bytes.Length);
cs.FlushFinalBlock();
ms.Flush();

return (ms.GetBuffer());

Mar 31 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
pachinco <pa**********@mail2world.com> wrote:
Hello, I am having a problem encrypting a tiff image..it always loses
information after i decrypt and I know it has something to do with the
encoding but i can't figure it out. Any help would be appreciated..
Here is the encrypt function:


<snip>

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that. It shouldn't need to use a GUI or anything like
that (i.e. use a hard-coded key and iv).

One problem in your code is that you're using MemoryStream.GetBuffer
instead of MemoryStream.ToArray - the first call just returns the raw
buffer, whereas the second one takes account of the logical size of the
stream (i.e. it trims it).

How sure are you that the error is in the encryption rather than how
you're reading the data in the first place?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 31 '06 #2

P: n/a
Hello Jon, here is the entire code:

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Security.Cryptography;

namespace encrypt_console
{
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
private byte[] key;
private byte[] iv;

private byte[] read_byte;
private byte[] write_byte;
private FileStream writeStream;
private FileStream readStream;
public Class1()
{
Open_file("C:\\1011.tif");
Save_file("C:\\1011_encrypt.tif");
Encrypt_Begin();
Open_file("C:\\1011_encrypt.tif");
Save_file("C:\\1011_decrypt.tif");
Decrypt_Begin();
Console.WriteLine("Encrypt and Decrypt complete");
}

private void Encrypt_Begin()
{
read_byte = new byte[readStream.Length];
readStream.Read(read_byte, 0, Convert.ToInt32(readStream.Length));
write_byte = Encrypt(read_byte, "12345678");
writeStream.Write(write_byte, 0, write_byte.Length);
writeStream.Flush();
readStream.Close();
writeStream.Close();
}

private void Decrypt_Begin()
{
read_byte = new byte[readStream.Length];
readStream.Read(read_byte, 0, Convert.ToInt32(readStream.Length));
write_byte = Decrypt(read_byte, "12345678");
writeStream.Write(write_byte, 0, write_byte.Length);
writeStream.Flush();
readStream.Close();
writeStream.Close();
}

private byte[] Encrypt(byte[] bytes, string k)
{
key = new byte[8];
iv = new byte[8];

for (int x = 0; x < 8; x++)
{
key[x] = Convert.ToByte(k[x]);
iv[x] = Convert.ToByte(k[x]);
}

DESCryptoServiceProvider cryptProvider = new
DESCryptoServiceProvider();
ICryptoTransform transform = cryptProvider.CreateEncryptor(key, iv);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, transform,
CryptoStreamMode.Write);
cs.Write(bytes, 0, bytes.Length);
cs.FlushFinalBlock();
ms.Flush();

return (ms.ToArray());

}

private byte[] Decrypt(byte[] bytes, string k)
{
key = new byte[8];
iv = new byte[8];

for (int x = 0; x < 8; x++)
{
key[x] = Convert.ToByte(k[x]);
iv[x] = Convert.ToByte(k[x]);
}

DESCryptoServiceProvider cryptProvider = new
DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(bytes);
CryptoStream cs = new CryptoStream(ms,
cryptProvider.CreateDecryptor(key, iv), CryptoStreamMode.Read);

return ms.ToArray();
}

private void Save_file(string file_name)
{
writeStream = new FileStream(file_name, FileMode.Create,
FileAccess.Write);
}

private void Open_file(string file_name)
{
readStream = new FileStream(file_name, FileMode.Open,
FileAccess.Read);
}

static void Main(string[] args)
{
Class1 new_class = new Class1();
}
}
}

Apr 3 '06 #3

P: n/a
pachinco <pa**********@mail2world.com> wrote:
Hello Jon, here is the entire code:


<snip>

Okay - the problem is that you're not actually doing any decrypting.
You're creating the CryptoStream, but not reading anything from it.
Here's a change you could use in the decryption:

MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,
cryptProvider.CreateDecryptor(key, iv), CryptoStreamMode.Write);
cs.Write (bytes, 0, bytes.Length);
cs.FlushFinalBlock();
cs.Close();

(Note that normally I'd use using blocks to make sure that things got
closed, and read in a more robust manner - hopefully the reason your
sample code wasn't written like that was to get a short example. See
http://www.pobox.com/~skeet/csharp/readbinary.html for more on the
reading issue.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.