Hashing, Signing and Encryption using an X509 Certificate from the Certificate Store
From: Seb (sebastiengarrioch_at_boardex.com)
Date: 07/30/04
- Next message: Ari Glaizel: "WSE717 error message when trying the quickstart samples (C#)"
- Previous message: Sean: "Re: RequestSoapContext is always null"
- Messages sorted by: [ date ] [ thread ]
Date: 30 Jul 2004 09:35:11 -0700
Hi All,
I have been having a really hard time with X509Certificates. I have
installed WSE 2.0 on my workstation and I am using a certificate
stored in my local machine certificate store.
Goal:
Use a Server Authentication Certificate stored in my server's Local
Machine Store to encrypt a SQL connection string stored in the
registry using the public key, create a SHA-1 hash of the cipher and
sign the cipher using my private key. The signature is then stored in
the registry as a binary value along with the encrypted connection
string.
Sample code:
private void Encryt()
{
byte[] clearConn;
byte[] cipherConn;
byte[] signature;
byte[] hash;
X509CertificateStore store;
store = X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);
store.OpenRead();
X509CertificateCollection certColl = store.Certificates;
foreach (X509Certificate cert in certColl)
{
// I have my own customized Item class
Item itemCert = new Item();
itemCert.Certificate = cert;
itemCert.SetName();
listBox1.Items.Add(itemCert);
}
// I have some other code to select the appropriate certificate, not
relevant here.
// We will use the public key to encrypt the SQL Connection string.
RSAParameters lockConn = cert.Key.ExportParameters(false);
// Instantiate the cryptographic service provider (RSA).
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
// Set the algorhythm parameters, encryption key, length, etc.
// lockConn recieved these parameters above.
csp.ImportParameters(lockConn);
// Convert the connection string to an array of bytes.
clearConn = ASCIIEncoding.ASCII.GetBytes("SQL Connection");
// Encrypt the SQL connection string
cipherConn = csp.Encrypt(clearConn, false);
// Create a hash of the encrypted data.
SHA1Managed sm = new SHA1Managed();
hash = sm.ComputeHash(cipherConn);
// Sign the hash.
RSAPKCS1SignatureFormatter sf = new
RSAPKCS1SignatureFormatter(cert.Key);
sf.SetHashAlgorithm("SHA1");
signature = sf.CreateSignature(cipherConn);
}
The problem:
When I try to sign the hash it gives me an invalid OID exception and
crashes the app. I have looked everywhere on the MSDN site for
something to help me with this but have not been able to find
anything. Also it will not let me use the private key to decrypt the
data. This has been really frustrating and I would greatly appreciate
any help you could provide.
Thanks,
- Next message: Ari Glaizel: "WSE717 error message when trying the quickstart samples (C#)"
- Previous message: Sean: "Re: RequestSoapContext is always null"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|