|
I know this is off-topic, but where would I go to make suggestions for the improvement of C# and the .NET Framework?
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
I am using the following to open and write and encryped datafile.
FileStream fout = new FileStream(OptionsDataFile, FileMode.Create, FileAccess.Write);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125}
,new byte[] {216,237,102,42,142,11,133,104,}),
CryptoStreamMode.Write);
ASCIIEncoding AE = new ASCIIEncoding();
and the following to decrypt.
FileStream fin = new FileStream(OptionsDataFile, FileMode.Open, FileAccess.Read);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fin, tdes.CreateDecryptor(new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125}
,new byte[] {216,237,102,42,142,11,133,104,}),
CryptoStreamMode.Read);
ASCIIEncoding AE = new ASCIIEncoding();
int tempByte = new int();
byte[] inBytes = new byte[(int)fin.Length];
encStream.Read(inBytes,0,(int)fin.Length);
When it gets to the read I get the following.
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll
Additional information: Bad Data.
Anyone have any ideas why. Everything is the same for the write. I just do not get why the data would be bad.
|
|
|
|
|
Not sure what's going on for you. Maybe it's something in the code you *don't* show <g>.
The following works just fine for me (I separated out the creation of some the objects into separate statements for readability):
class Class1 {
static void Encrypt(string inputFile, string outputFile) {
FileStream fsin = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
FileStream fsout = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
byte[] key = new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125};
byte[] iv = new byte[] {216,237,102,42,142,11,133,104};
TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider();
ICryptoTransform t = tripleDes.CreateEncryptor(key, iv);
CryptoStream cs = new CryptoStream(fsout, t, CryptoStreamMode.Write);
int len = (int) fsin.Length;
byte[] buffer = new byte[len];
len = fsin.Read(buffer, 0, len);
cs.Write(buffer, 0, len);
cs.Close();
fsout.Close();
fsin.Close();
}
static void Decrypt(string inputFile, string outputFile) {
FileStream fsin = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
FileStream fsout = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
byte[] key = new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125};
byte[] iv = new byte[] {216,237,102,42,142,11,133,104};
TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider();
ICryptoTransform t = tripleDes.CreateDecryptor(key, iv);
CryptoStream cs = new CryptoStream(fsout, t, CryptoStreamMode.Write);
int len = (int) fsin.Length;
byte[] buffer = new byte[len];
len = fsin.Read(buffer, 0, len);
cs.Write(buffer, 0, len);
cs.Close();
fsout.Close();
fsin.Close();
}
static void Main(string[] args) {
Console.WriteLine("Starting encryption...");
Encrypt(@"c:\test.txt", @"c:\test.enc");
Console.WriteLine("...done");
Console.WriteLine("Starting decryption...");
Decrypt(@"c:\test.enc", @"c:\test.dec");
Console.WriteLine("...done");
Console.ReadLine();
}
}
Cheers, Julian
Program Manager, C#
This posting is provided "AS IS" with no warranties, and confers no rights.
|
|
|
|
|
The problem with the orginal code is I was not Seeking to the beginning of the stream.
|
|
|
|
|
I'm borrowing some code from Pavel's Command Prompt Explorer Bar installer to use in my own explorer bar's installer.
Recently I've been thinking that using an assembly version like "1.0.*" (hence, for example, "1.0.1304.25935") would be useful, at least for development and debugging purposes. (Someone please point out the cons to this idea!)
Well, anyway, Pavel's installer basically exists to register the assembly, and unregister the assembly at uninstallation. So it all comes down to getting the relevant assembly so it can be fed to RegistrationServices.RegisterAssembly().
Currently the code for getting the relevant assembly looks like this:
Assembly VisITBarAssembly
{
get
{
return AppDomain.CurrentDomain.Load("VisIT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4d504ee06f99380a");
}
}
This works because the version is hardcoded to "1.0.0.0" in Installer.cs as well as AssemblyInfo.cs. I'd like to remove this string from Installer.cs so I can only worry about AssemblyInfo.cs whenever I want to bump up a version number. (Bonus points if I could remove the need to change both AssemblyInfo.cs and the version number in the Setup project! Additional bonus points if I can access the Setup project's "Manufacturer" etc. fields from the application, but I guess the best (or only?) solution would be to use the Registry.)
So what are some good ways to go about this? I think using the Registry might be one way to go about it, having to store the assembly's full name somehow (programmatically) in a registry entry. Personally I think it would be nicer to avoid it, but hey! If I assume a constant GUID, then I could look it up in the registry (it's already stored there, but then again, this might implicitly get stored from the RegisterAssembly call).
It would be even nicer, I think, to have a dynamic GUID, that allows the GUID to change with the Product GUID (set in the Setup project), but I don't know if that's feasible.
And furthermore, what can I do with the savedState object that is an argument to so many Installer event handlers?
Any tips, comments, or smooth suggestions?
Thanks,
Arun
|
|
|
|
|
Alright,
I have some source code libraries that help me convert text into pdf417 (2d) barcodes. That all works fine. Heres my issue, the
public sbyte[]createbarcode() method gives me an array of sbytes with which to create a bitmap. I want to then print this bitmap. My question is, are there any gdi junkies out there who can point me in the right direction as far as creating a bitmap through a big old sbyte array?
Thanks for the help in advance,
Ryan
|
|
|
|
|
System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
stream.Write(bytes, 0, bytes.Length);
Bitmap bmp = new Bitmap(stream);
|
|
|
|
|
I would really need some help with using the WIN32 API SendMessage() from C#. I want to start a program from mine, but if it's already an instance open, I wanna find that window and send it a message of a file to load instead of starting a second instance.
I'll do this through the FindWindow and OpenIcon methods, but how do I send my message to the other window?
I know how to get my own message ID's and so my second application will listen for a specific custom message, but how do I send the actual data to it? I've found something called COPYDATASTRUCT or something, but how to use it? and from C#
I'd really appreciate some help I haven't done any windows programming before and it's kind of a Jungle of structs and defines =) And on top of that the Marshal-As-issues...
have a nice day i'd appreciate some help on this one. really.
|
|
|
|
|
I now know that i should use WM_COPYDATA together with a COPYDATASTRUCT. but how do I get something corresponding to a pointer from my object, to pass as lpData in the struct?
I guess that's my main question really. And also what types to use.
|
|
|
|
|
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private extern static int SendMessage(IntPtr hwnd, int msg, int wParam, ref COPYDATASTRUCT lParam);
[StructLayout(LayoutKind.Sequential)]
struct COPYDATASTRUCT {
int dwData;
int cbData;
IntPtr lpData;
}
COPYDATASTRUCT cds=new COPYDATASTRUCT;
cds.cbData=Marshall.SizeOf(data);
IntPtr mem=Marshall.AllocHGlobal(cds.cbData);
cds.lpData=Marshall.StructureToPtr(data,mem,true);
cds.lpData=mem;
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private extern static int SendMessage(IntPtr hwnd, int msg, int wParam, void* lParam);
COPYDATASTRUCT cds=new COPYDATASTRUCT;
SendMessage(handle,WM_COPYDATA,wParam,(void*)&cds);
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
thanks very much, this helped me a lot. Kind of you to find the time helping me.
have a good one!
|
|
|
|
|
misterbear wrote:
thanks very much, this helped me a lot.
BTW, I made a mistake:
The line:
cds.lpData=Marshall.StructureToPtr(data,mem,true);
should be:
Marshall.StructureToPtr(data,mem,true);
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
You'll find a Win32Window class on www.csharp.net in the code section that will do the FindWindow part for you.
|
|
|
|
|
|
You're right.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
|
I've been doing some testing with the HttpWebRequest object returned from WebRequest.Create(sURL) method and found that the timeout property is not very precise.
It often times out after more than twice the value I set. For example it takes about 30 seconds for a timeout value of 15000. I’ve seen it take 15 seconds for a value of 5000.
Plus or minus a few seconds I could let slip but what might be the reason for more than doubling the timeout value?
(I tested the timeout by turning the power off to my cable modem. My computer is connected to the modem through a router which I left on.)
|
|
|
|
|
Is this happening in a separate thread? How many threads might there be running simultaneously?
|
|
|
|
|
I've been doing all my tests in a new "test" solution right in the constructor of Form1. So its only one thread. hmmm.... I wonder if its the debugger that is causing this. I will have to test it in a Release version.
|
|
|
|
|
Certainly try it outside of the debugger ("Start Without Debugging"). A Release build shouldn't be necessary, but it would be interesting if it did...
|
|
|
|
|
Hi, if anyone has not seen these controls, you should, they are really good.
Anyway, the problem is this. The comboBox in these controls, was inherited and modified, and now I cannot find the SelectedValue option. Does anyone know how to re-enable this?
|
|
|
|
|
Talking about the look and feel of the controls - I would say they are nice. But this is exactly the reason I don't like custom contols.
In this case the problems is very easy to solve. Just call comboBox.SelectedItem and convert it to string if you need.
For this comboBox control he derived form his own custom class ButtonEdit & attached his own ComboListBox based on ListBoxBase. Just look at the source and it should make everything clear
|
|
|
|
|
Hi, I went into the control, and added the following. I have never done this before, so not sure what I am doing wrong - but I get a stack overflow error.
Sorry about how bad the code looks in this box, but the parts without ;'s are wraparounds.
[Browsable(false)]
public int SelectedValue
{
get
{
if(this.SelectedValue != -1)
{
return ((System.Windows.Forms.ListBox)this.internalListBox).Items.IndexOf(this.SelectedValue);
}
else
{
return -1;
}
}
set
{
if(value > -1 && value < ((System.Windows.Forms.ListBox)this.internalListBox).Items.Count)
{
((System.Windows.Forms.ListBox)this.internalListBox).SelectedValue = ((System.Windows.Forms.ListBox)this.internalListBox).Items[value];
this.Text = ((System.Windows.Forms.ListBox)this.internalListBox).SelectedValue.ToString();
}
}
}
|
|
|
|
|
Is there a way to reduce the color depth (in bits) of an image (whether it is saved or in a Bitmap form) in c#? If possible, please include a sample code.
|
|
|
|
|
There might be a simpler way, but this way works:
Bitmap newbmp=new Bitmap(format,bmp.Width,bm.Height);
Graphics newbmpgr=Graphics.FromImage((Image)newbmp);
newbmpgr.DrawImageUnscaled((Image)bmp,0,0);
newbmpgr.Dispose();
Remember that if you convert the image from another format into 8-bit indexed color, you will have to set the palette entries yourself before doing the DrawImageUnscaled() call.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|