|
Sorry about the confusion with byte[]/Array. Like I said, I was having to go without IDE at the time and couldn't remember how it was in C# exactly.
The line you point to where you get an index out of bounds error does not appear anywhere in the code I gave. Please use what I supplied, or if it's not working let's go from there.
void DecryptInstID(byte[] InstID)
{
byte[] Aux = new byte[8];
int i, k;
for (i = 0; i < 4; i++)
{
KeyedHash(InstID, out Aux);
for (k = 0; k < 8; k++)
Aux[k] ^= InstID[k + 8];
Array.Copy(InstID, 0, InstID, 8, 8);
Array.Copy(Aux, InstID, 8);
}
}
Now for this to work, your going to have to pass in for InstID a 16 byte array (just like it was in the C++ code; you said 15 bytes, but the C++ code suggests/needs 16 bytes).
EDIT: I was just thinking about the KeyedHash you are using. You should use the overload of ComputeHash that accepts start and size: HMACSHA1.ComputeHash(data, 0, 8) to only hash the first 8 bytes of the data supplied, as it was with your C++ code. Currently, your keyed hash function hashes the entire 16 byte (or so) block, rather than only the first 8 bytes as intended.
So:
void KeyedHash(byte[] data, out byte[] result)
{
System.Security.Cryptography.HMACSHA1 mac = new System.Security.Cryptography.HMACSHA1(yourKeyHere);
result = mac.ComputeHash(data, 0, 8);
}
modified on Wednesday, August 10, 2011 9:11 AM
|
|
|
|
|
Hi,
Thank you once again for the code, I have just a couple more questions if I may?
MicroVirus wrote: you said 15 bytes, but the C++ code suggests/needs 16 bytes)
Yes, and when I corrected that it worked. However, in my own code I modified InstID to be a lower amount of bytes. Where in this code do I edit it so that it can do the same, but with less bytes?
Also, When I pass the encypted value back through the function, it does not decrypt to the original data.
I was under the impresion that using a 4 round feistal cypher like this was simply the same function for both encryption and decryption?
Thank you again,
Stephen
|
|
|
|
|
You're mixing in a hash (and only a part of the hash at that). I don't see how this is ever going to be reversible. Do you have round-tripping working in C++ with this algorithm?
|
|
|
|
|
Hi,
The formula the code is based on is:
Left = Right ^ First-8 (SHA-1 (Left + Key))
Right = Left
Now, in the code and text I have, it is quite confident that this is reversable, although I am now doubting it, however, I have seen a simular function that combined part of a hash and was reversable,although I do not know if the "reverse" function or "Encryption" routine must be different, as I only have the code for this "decritption" code.
Kind Regards,
Stephen
|
|
|
|
|
stephen.darling wrote: Yes, and when I corrected that it worked. However, in my own code I modified InstID to be a lower amount of bytes. Where in this code do I edit it so that it can do the same, but with less bytes?
The code given is an exact translation of the C++ code. Can the C++ code do with less bytes? In that case the C# can too, though you're going to have to modify both the C++ code and the C# code to manage that.
I don't understand the algorithm and the other people that have replied to this thread also don't know if this'll work or not. That means all I have to go on is the original C++ code. From that, we can infer that the InstID contains a 16 byte 'encrypted' sequence and is then 'decrypted' in-place (with a small Aux buffer of 8 bytes) to a 16 byte sequence.
Like asked before, does the C++ code work in encrypting/decrypting and does it use the same function for both? I tested the C# code and it is not capable of using the same function for both encryption and decryption.
|
|
|
|
|
Be aware that SHA is not an encryption algorithm. It is a hashing algorithm. The difference is that encryption algorithms can be reversed, hashing algorithms can't.
If you try to use SHA as a base component for your data encryption, there is very little chance that you will ever be able to decrypt your data back to the original input.
There are good reasons why encrypted data is generally longer that the input: It includes information generated by successive iterations though the data and the key. If you need the size of you encrypted data to be the same as your input, then you will heavily compromise the security of that data.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
Hi OriginalGriff,
OriginalGriff wrote: Be aware that SHA is not an encryption algorithm. It is a hashing algorithm.
Yes; I am fully aware of this.
OriginalGriff wrote: If you try to use SHA as a base component for your data encryption, there is very little chance that you will ever be able to decrypt your data back to the original input.
It is used in combination with basic 4 round Feistel cypher. It is pretty standard and employed in a number of situations where we want to "secure" a small amount of data. In fact, the formula for it is:
Left = Right ^ First - 8 (SHA-1 (Left + Key))
Right = Left
OriginalGriff wrote: There are good reasons why encrypted data is generally longer that the input
Yes; under normal circumstances I fully agree.
OriginalGriff wrote: If you need the size of you encrypted data to be the same as your input, then you will heavily compromise the security of that data
Also agreed! However, my application must accept a small key with a fixed number of bytes, which will be decoded into a fixed length data block to be used.
However, it must not be given to the end user as plain text, so this method is chosen.
I am fully aware, however, that the level of security is way lower, however, I am not bothered about anyone who has a bit time and knowledge to sit and de-cypher it.
I simply need a basic employment to deter casual tampering!
Thank you,
Stephen
|
|
|
|
|
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
I think you got the KeyedHash translation already (it is just the first 8 bytes of a SHA1 hash).
void DecryptInstID(byte[]InstID)
{
int length = InstID.Length;
if(0 != length % 2) throw new ArgumentException("Must be even length");
length >>= 1;
byte[] Aux = new byte[length];
int i, k;
Left = InstID;
Right = InstID + 8;
for (i = 0; i < 4; i++)
{
KeyedHash(InstID, Aux, length);
for (k = 0; k < 8; k++)
Aux[k] ^= InstID[k + length];
CopyBytes(InstID, InstID, length, 0, length);
CopyBytes(InstID, Aux, 0, 0, 8);
}
}
void CopyBytes(byte[] target, byte[] source, int targetIndex, int sourceIndex, int length){
for(int i = 0; i < length; i++) target[i+targetIndex] = source[i+sourceIndex];
}
Something like that, anyway.
|
|
|
|
|
Hi,
Yes; this sugested code also works, however, after calling it again, I do not get my original data back
If it is not meant to be implemented like this, and I am not going to be able to reverse it,
Is there a better, simple algorithim to use for the following...
1) Accept a small amount of fixed lenght bytes, from 10-20 bytes
2) Produce an encrypted output that is the exact same length as the input
3) Use a private key
I am not concerned about all the debatable security issues as I am aware of them, and simply need something a little more secure than a basic Xor on each byte.
Kind Regards,
Stephen
|
|
|
|
|
For 10-20 bytes, if you generate a new key each time, XOR is fine.
If you want a persistent key, you could use this[^] (shameless personal plug).
|
|
|
|
|
Thank you,
Just what I was looking for!
Kind Regards,
Stephen
|
|
|
|
|
I am trying to scrape the values in these divs to security questions(sec_answers). Of course security question have been removed for security purposes. I would like to use the agility pack, but dont know how it works, any help would be appreciated. The retrieval of information takes lace on a form load and the lays it out for me in the form. Keep in mind I am trying to get the security answer. However this information is generated after the page loads so I am not pulling from a db.
<div class="sec_qaArea">
<div class="sec_title">Security Questions</div>
<div class="sec_question">Name of your favorite pet?</div>
<div class="sec_answer">John Doe</div>
<div class="sec_question">Name of the highschool you graduated from?</div>
<div class="sec_answer">Jane Doe</div>
<div class="sec_question">First car you owned?</div>
<div class="sec_answer">Fail Doe</div>
<div class="sec_question">First company you worked for?</div>
<div class="sec_answer">Doe Boy</div>
</div>
|
|
|
|
|
Is this your page? If so, why can't you just handle the form postback in the normal way? Are you trying to get the values client side (in Javascript)?
|
|
|
|
|
I am using a dynamic usercontrol on multiple forms, and I am trying to reset the form (any one of the forms) and control size by recalling the control using the new command "Control ctrl = new Control()". However it does not reset the size of the control. I tried adding the code I use in a form to the usercontrol as follows:
private Form m_InstanceRef = null;
public Form InstanceRef
{
get { return m_InstanceRef; }
set { m_InstanceRef = value; }
}
This also did not do the trick, how do I call an existing control and get the same instance.
Any help will be appreciated.
Thanks in advance,
Michael
|
|
|
|
|
Hi, a few questions I hope will clarify your query:
1. what exactly do you mean when you say 'dynamic UserControl' ?
a. do you mean that the user interacts with the UserControl and there is an exchange of data between the UserControl and the Form that hosts it ?
b. and/or do you mean that the UserControl fires Events that the Form subscribes to, and/or depending on what data is sent to the Form, then the Form calls public methods on the UserControl ?
c. maybe all of the above ?
2. you are describing a scenario where you have multiple forms each with an instance of the UserControl ... yes ?
3. when you say 'reset the Form,' exactly what do you mean ?
a. set all the user-entered values on the Form to some default value ?
b. set only the UserControl on the Form back to its default state ?
b. destroy the Form and re-create it, but somehow persist some user-entered values on the UserControl ?
4. by 'reset the control size,' do you mean reset the UserControl on the Form back to some original size ?
Sorry if these questions seem 'picky,' but the clearer you describe the scenario, the quicker you'll get good answers, I hope.
"In the River of Delights, Panic has not failed me." Jorge Luis Borges
|
|
|
|
|
By dynamic I mean I setup the usercontrol by my program not by dragging it from my toolbox.
I can choose how many times I wish for this control to be added to the form as follows:
this.Contol.Add(usercontrolname)
this is a chart for the stock market.
1 control for the stock price per form.
1-3 controls for indicators per form.
1-3 controls for oscillators per form.
Because I can choose multiple stocks to monitor each with its own form, I resize the forms.
The more stocks I choose to monitor, the form resizes per set ratio depending on quantity and size of screen.
Also, if I choose multiple indicators or oscillators I resize all charts per form (meaning price, indicators, and oscillators) as set ratio of number of charts.
If I am going from 4 stocks to 5 I would divide the screen differently and resize the forms. Therefore I resize the controls within. Just from talking about this I could setup on building the form the %size and postion of the control and it would automatically reset the controls within on resizing the form.
But if I want to add or delete an indicator then I would want to reset the size and positions of the individual controls as a set formula to maximize the space for the entire form.
I hope this is more specific.
Thanks,
Michael
|
|
|
|
|
Then you'll need to come up with a 'formula' for the 'optimal' size of each control.
A good start would be to take the client size area of the form that's available for the controls, then subtracting the left and right offset (the distance of the controls to the edges of the form).
Then you have the 'working area' that can be used for the controls. If you have N controls you want to place in a row, and X pixels spacing in between controls you have ControlWidth * N + X * (N - 1) == WorkingArea_Width . Solving for ControlWidth gives you the 'optimal' width of each control.
Then you just have to set the control size on each, for instance if you stored the dynamically created user controls in a list:
private List<MyUserControl> mControls = null;
...
mControls = new List<MyUserControl>();
...
mControls.Add( myNewUserControl );
...
foreach (MyUserControl ctrl in mControls)
{
...
ctrl.Left = ..; Ctrl.Top = ..; Ctrl.Width = ..; Ctrl.Height = ..;
}
I hope I managed to properly explain what I mean.
|
|
|
|
|
I have already calculated all the sizes needed, however I do it for each form, which is redundant.
I think I understand you correctly, if I place the correct claculation in a list every time the form is resized it refers to the list and resizes the control accordingly as a percentage or point, width, height, etc.
I think this will wok great, thanks.
Michael
|
|
|
|
|
Hi, I think you got a good response from MicroVirus, but I will add, based on the assumptions we are talking about an application built-on .NET / WinForms:
1. one way to address resize in general in .NET is to set the Anchor or Dock properties of the hosted Control/Form in a way that when the host container (Control, Form, UserControl) is re-sized, the hosted things automatically 'do the right thing' in terms of re-sizing. I'm wondering why that strategy would not work for you.
2. on the larger level ... architecture ... it sounds like what you are doing involves a lot of either Forms or Controls, or UserControls; have you considered using a meta-container, like a DataGridView, FlowLayout, or TableLayoutPanel, all of which can host Controls of different types, one where changing the size of its virtual rows or columns can take care of re-sizing issues, or inserting a new object that requires a change in its hosted row and or column size can have this change automatically handled.
good luck, Bill
"In the River of Delights, Panic has not failed me." Jorge Luis Borges
|
|
|
|
|
I was thinking about point 1 myself too. My 5
|
|
|
|
|
Hi guys. I'm trying to write an add-on for Visio... but I'm running into problems. Basically, I just don't know how Visio stores the data for shapes... and my searching hasn't given me any good examples that seem applicable.
As an example, if I create a database diagram and drop a "Table" master onto the sheet... I want to be able to click the add-on and it read all of the properties of that table. That is, I want it to know the name of the columns, their data types and so forth.
Can anyone give me a clear example of how this is done? So I've tried enumerating the shapes on the page, but I only see a single shape and it describes the single column I added.
Thanks, in advance.
Sam
|
|
|
|
|
I have written a program for the stock market in C#. I am now trying to put the data in charts, I keep seeing a lot of charts using ASP, my question is what are the advantages for using another language other than C# with .net forms. I am much more familiar with C#, what are the pros and cons for using another language, any recommendations please do not limit to ASP and C#.
Any advice will be appreciated
Thanks in advance,
Michael
|
|
|
|
|
C# can basically do what any other language can do.
You are already comfortable with it, so you should probably think about sticking to it rather than moving to another language.
Too much of heaven can bring you underground
Heaven can always turn around
Too much of heaven, our life is all hell bound
Heaven, the kill that makes no sound
|
|
|
|
|
Okay, I have had this problem in the past. However, the projects were small personal projects which I wrote at home, for fun. Now I am writing applications here at work. And it is beginning to hinder the process greatly.
I have noticed that whenever I use a custom control (or, for example, the free DevExpress XtraEditors controls which I registered for and downloaded) in any project, I receive the error:
"Unable to copy file "obj\Debug\LpsApp.exe" to "bin\Debug\LpsApp.exe". The process cannot access the file 'bin\Debug\LpsApp.exe' because it is being used by another process."
When I first open the project and I work on a form which uses a custom control it is okay. However, once I make a change and re-build the project a few times I begin to receive the error message. Like, every time from then on. Once I close VS2010, delete the bin/Debug directory and restart, it works fine again for a few builds. Then it goes back to the same crap. This is highly frustrating.
I have another project which was perfectly fine, for months. That is, until I made an upgrade and it involved a custom control from right here on good ol' CodeProject. I then began to receive the same error message. But as long as I do not open the forms which use them and make changes, and I do not make changes to the custom control itself or its code, everything is fine. I restarted the current project a while ago and I have been working on a different feature. And it has been flawless.
Why does a custom control do this? Is there any way around it?
I have tried deleting the .SUO file, the 'bin' folders, the 'obj' folders. I have tried the pre-build event which renames the file with a '.locked' extension. As well as a few other "solutions" which I found online. They work for two or three builds and then it is right back to the same thing.
Thanks CP.
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|