|
There is no short way to solve this issue. This isn't an easy or quick task to solve, so you shouldn't look for quick fixes.
|
|
|
|
|
ok,,,,,yes i think so
thanx for ur replies
|
|
|
|
|
i tried with cryptEncypt and cryptDecrypt function. Below are the key function which i used to get the publc and private key from certificate
Please check my code.. it not working (its in c# .net)
// create 128 bit hash object
if (!WinApi.CryptCreateHash(hProv,
WinApi.CALG_MD5, IntPtr.Zero, 0, ref hHash))
Failed("CryptCreateHash");
// add passphrase to hash
byte[] keyData = ASCIIEncoding.ASCII.GetBytes(passphrase);
if (!WinApi.CryptHashData(hHash, keyData, (uint)keyData.Length, 0))
Failed("CryptHashData");
// create 40 bit crypto key from passphrase hash
// if (!WinApi.CryptDeriveKey(hProv, WinApi.CALG_RC2,
// hHash, WinApi.CRYPT_EXPORTABLE, ref hKey))
// Failed("CryptDeriveKey");
// Generate new key pair
//WinApi.CryptImportKey(hProv, keyData, (uint)keyData.Length, 0, 0, ref hProv);
// Get a handle to the key exchange key.
if (!WinApi.CryptGetUserKey(hProv, 1, ref hProv))
{
uint dwResult = WinApi.GetLastError();
// printf("Error %x during CryptGetUserKey!\n", GetLastError());
//goto done;
}
if (!WinApi.CryptImportKey(hProv, keyData, (uint)keyData.Length, 0, 0, ref hProv))
{
uint dwResult = WinApi.GetLastError();
}
if (!WinApi.CryptGenKey(hProv, WinApi.CALG_RC2, WinApi.CRYPT_EXPORTABLE, ref hKey))
{
uint dwResult = WinApi.GetLastError();
//MessageBox("Error CryptGenKey() failed.", "Information", MB_OK);
// return;
}
// if (!WinApi.CryptGenKey(hProv, WinApi.CALG_RC2, WinApi.CRYPT_EXPORTABLE, ref hKey))
// {
// Failed("CryptGenKey");
// }
|
|
|
|
|
Suresh Ramasamy wrote: Please check my code.. it not working
Welcome to CodeProject
Please take the time to read this handy Forum Guidelines article[^] Pay particular attention to item #4.
led mike
|
|
|
|
|
Apologies for the vague thread title, the following is a tad complicated to summarise well. Anyway, enough excuses...
Lets say I have a multi tier application that consists of 1 client and 2 server applications (A & B). Both servers have remoting listener services each of which are entirely self contained and do not share any common sinks, both remoting frameworks contain a custom encryption sink (each of which are entirely independent implementations)
In my current setup, the client pushes all its requests to server A. Some of these are handled within A others require a server A to server B remoting call.
Now here's the problem:
When server A is invoked by the client and this requires a remoting call to server B the call from A to B fails with a CryptographicException. It seems that for some reason the A to B encryption sink is not being called, so when B receives the data it cannot decrypt as the format is invalid.
Does anybody have any ideas of what could be happening here?
p.s. Interesting if I code the client to remote directly to A or B then these calls work without any issues.
modified on Wednesday, September 10, 2008 1:05 PM
|
|
|
|
|
I think its a 'Serialization' issue.
I am not sure but, Try without encryption. If it still gives you error then its a problem of Serialization else your encription methods mismatched.
|
|
|
|
|
I've found this issue with double hop applications before. Something, and I could never figure out what, caused the code to fall over with an invalid key issue.
|
|
|
|
|
After much hair pulling I have found a solution to this problem, I am not particularly happy with it but it is a solution none the less.
After doing some further debugging it seems that when the A to B request is made the .NET Framework pushes this into the incorrect channel (i.e. the Client to A) as this channel has different sinks, when B receives the data it is unable to process it. Interestingly if I unregistered the Client to A channel the A to B call would work, obviously this isn't a solution as this then breaks all Client to A comms.
At first it seemed that the solution was obvious, make sure that the correct channel is used. After much googling I have been unable to find a method of doing this. I did however find alot of people with the same problem
So the solution...
The server B remoting framework has a client library which is installed on A. This library contains all the methods that B exposes across remoting. The solution was to create a wrapper around this client library, on instantiation the wrapper creates a new AppDomain and loads the client library into it. I then use a marhsalled transparent proxy object across to call the client library. Doing this means that the new AppDomain on has a single TcpChannel registered and therefore the selection issue doesn't exist.
As always with these things there was a gotcha, which is if you leave the AppDomain unused for more than 5 minutes (default) the lease expires and the proxy fails. This can be fixed by, in the client library, overriding "InitializeLifetimeService" and returning null.
So, wrapper code:
public sealed class ClientLibraryWrapper
{
AppDomain clientDomain;
ServerB.Client.EntryPoint client;
public ClientLibraryWrapper
{
this.clientDomain = AppDomain.CreateDomain("ServerBClientDomain");
Assembly clientAssembly = Assembly.LoadFile(Environment.CurrentDirectory + @"\ServerB.Client.dll");
this.clientDomain.Load(clientAssembly.FullName);
Object obj = mhsClientDomain.CreateInstanceAndUnwrap(clientAssembly.FullName, "ServerB.Client.EntryPoint");
this.client = (ServerB.Client.EntryPoint)obj;
}
public int ServerBMethod()
{
this.client.ServerBMethod();
}
}
Client library code:
public sealed class EntryPoint : MarshalByRefObject
{
public override object InitializeLifetimeService()
{
return null;
}
public int ServerBMethod()
{
return 1;
}
}
|
|
|
|
|
Here “Key not valid for use in specified state” is coming
---------------------------------------------------------
CspParameters cspParam = new CspParameters();
cspParam.Flags = CspProviderFlags.UseMachineKeyStore;
cspParam.KeyContainerName = "MyContainer";
cspParam.KeyNumber = 1;
System.Security.Cryptography.RSACryptoServiceProvider RSA
= new System.Security.Cryptography.RSACryptoServiceProvider(cspParam);
// Encryption based on Public Key
Byte[] EncryptedStrAsByt;
RSA.ImportParameters(RSA.ExportParameters(false)); // gets the public key for encryption
String str = "HelloThere";
EncryptedStrAsByt = RSA.Encrypt(System.Text.Encoding.Unicode.GetBytes(str), false);
string EncryptedStrAsString = System.Text.Encoding.Unicode.GetString(EncryptedStrAsByt);
// As a "proof of concept", to DECRYPT the same data, but now using the Private key:
//---Load the Public key---
RSA.ImportParameters(RSA.ExportParameters(true));
Byte[] DecryptedStrAsByt = RSA.Decrypt(System.Text.Encoding.Unicode.GetBytes(EncryptedStrAsString), false);
string DecryptedStrAsString = System.Text.Encoding.Unicode.GetString(DecryptedStrAsByt);
|
|
|
|
|
i have test your code, but more detail you can see this link[^]
or this link[^]
but i change your code like this and it works
CspParameters cspParam = new CspParameters();
cspParam.Flags = CspProviderFlags.UseMachineKeyStore;
cspParam.KeyContainerName = "MyContainer";
cspParam.KeyNumber = 1;
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(cspParam);
RSAParameters privatePar = RSA.ExportParameters(true);
RSAParameters publicPar = RSA.ExportParameters(false);
Byte[] EncryptedStrAsByt;
RSA.ImportParameters(publicPar);
String str = "HelloThere";
EncryptedStrAsByt = RSA.Encrypt(System.Text.Encoding.Unicode.GetBytes(str), false);
RSA.ImportParameters(privatePar );
Byte[] DecryptedStrAsByt = RSA.Decrypt(EncryptedStrAsByt, false);
string DecryptedStrAsString = System.Text.Encoding.Unicode.GetString(DecryptedStrAsByt);
hope it helps
dhaim
programming is a hobby that make some money as side effect
|
|
|
|
|
Is it possible to sort a generic list base don the sort of another generic list?
Here is what I am working with:
I have a List<string> object that I built out of a List<q_object>, where the strings in the first list are populated with a specific string property from the second.
so, say this is my example, the List<Q_object> object is made up of this:
lets say Q_object has the follwing properties:
string str1
string str2
int i
and my objects in the list are specifically:
Q_object[0] has
str1 = "fudge";
str2 = "brownie";
i = 0;
Q_object[1] has
str1 = "Beer";
str2 = "Sausage";
i = 1;
Q_object[2] has
str1 = "Pancakes";
str2 = "waffles";
i = 2;
now, the List<string> has the following:
"Sausage"
"brownie"
"waffles"
essentially, somwhere along the line, the list got out of order. Now, I need it to be in the original order, but it has to be the string list as opposed to me trying to access the properties of each individual object in the original list. is there a way to say "sort List<string> to match List<q_object>'s properties with the same values"?
I'm thinking Linq has my answers, but I dont know for sure, and I dont know where to begin. Any suggestions would be appreciated
______________________
Oh Hamburgers!
|
|
|
|
|
You can write a custom comparer for the object to be used in the list?
I don't really understand the problem but sometimes that is a good solution for sorting woes, There is also the SortedList class available for use.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.
|
|
|
|
|
What is the reason that you need to sort the list of strings? If it should just be a copy of some of the data in the list of objects, can't you just recreate the list? Do you actually have to create the list of strings, can't you just read the data from the list of objects?
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
the logic being used is very flexible and was designed to grab strings from any properties contained in a list of any objects. if you know the properties of the object (and you should at least one, otherwise you shouldn't be using it )and return the valu eof that property in each instance of the object in the list. Its hard to read, I know, but it works. the issue is that somewhere along the line, the data is getting out of sort, and I dont have time (yet) to add logic for sorting into the method. (it would have to identify the other property in the object to sort from, if such a property exists, blah blah blah)
Why wouldnt i just access the property of each individual object in the list? Believe it or not, this was written to run through huge XML strings and grab the potential instances of object descriptions contained within, and then return the property values, if the objects exist, and if their properties arent null. I didnt design it, but I have to use it, so this method ignores the "what if" and just returns what is.
And now i have to work with what comes out th eother side.
______________________
Oh Hamburgers!
|
|
|
|
|
As ennis said you have to make comparer by your self
see this link[^]
dhaim
programming is a hobby that make some money as side effect
|
|
|
|
|
Hi,
I need to build XML files and populate them with data from our database.
They need to conform to a schema defined by a third party.
Unfortunately the schema is too complex to build a stronly typed DataSet from so I'm going to have to write the XML building code myself.
I'm having difficulty so I've built a simple test application to build and validate some XML.
But I can't get it to work, XmlDocument.Validate just succeeds even if the XML is wrong.
Here is the sample code as well as the respective XML and XSD files.
Container.xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Container" targetNamespace="http://www.spursolutions.com/Container.xsd" elementFormDefault="qualified" xmlns="http://www.spursolutions.com/Container.xsd" xmlns:mstns="http://www.spursolutions.com/Container.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Envelope" nillable="false">
<xs:complexType>
<xs:sequence>
<xs:element name="Header" nillable="false">
<xs:complexType>
<xs:sequence>
<xs:element name="Source" type="xs:string" nillable="false" />
<xs:element name="Dest" type="xs:string" nillable="false" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Payload" type="xs:anyType" nillable="false" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Code
using System;
using System.Xml;
using System.Xml.Schema;
namespace XMLTest
{
class Program
{
static void Main(string[] args)
{
Validate();
}
public static void Validate()
{
XmlDocument oDoc;
oDoc = new XmlDocument();
oDoc.Schemas.Add("http://www.spursolutions.com/Container.xsd", "Container.xsd");
oDoc.AppendChild(oDoc.CreateXmlDeclaration("1.0", "utf-8", null));
oDoc.AppendChild(oDoc.CreateElement("Envelope"));
oDoc.DocumentElement.SetAttribute("xmlns", "http://www.spursolutions.com/Container.xsd");
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEvent);
oDoc.Validate(eventHandler);
}
public static void ValidationEvent(object sender, ValidationEventArgs e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.Severity.ToString());
if (e.Severity == XmlSeverityType.Error)
{
throw e.Exception;
}
}
}
}
Now, if I save that XML file, then change the code to load it in instead of creating it from scratch, it fails the Validation.
Can anybody help? Having to save and load the XML to validate it is a rather ugly solution.
Thanks,
James.
|
|
|
|
|
|
Hi there.
How can we set a maximum time for a Thread variable.
For example :
I want to create a Thread variable that if it couldn't finish its work in 5 minutes , itself abort.
Do you understand me ?
Thanks in advance.
|
|
|
|
|
Well, one way to do it would be to have the code inside the Thread's callback handler periodically take a look at System.Enviroment.TickCount.
That would only work if the code is performing a loop though, so it could check the time at the start of each loop. Once the time is up, just return; the method.
What's the thread doing?
J.
|
|
|
|
|
|
There is no built in way to time limit a thread, you have to do it yourself.
Store the start time in a variable when the thread method starts. Then periodically check the difference between the start time and the current time, and exit out of the method if the predefined time has elapsed.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
Create two BackgroundWorker objects. One of them does the "work", and the other sits/spins until the time limit is reached, and then it cancels the other worker. Piece of cake...
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
|
Hello all,
I have a little problem regarding a double quote. I wrote this filter, to weed out all invalid characters in an Excel sheet. It works fine, however my invalid character list is defined in a string array. One of the invalid characters is "
string invalidChar = "' ` # ";
string[] invChar = invalidChar.Split(' ');
How do I get " into the string as well ?
kind regards,
|
|
|
|
|