Robert Ellis
11/5/2008 6:47:00 PM
Hi,
Could anyone advise as to why I am unable to persist RSA keys in a
container?
If I run this function, encoded() is filled with the same values on every
execution;
yet encrypted() is always populated with different values on every
execution.
Presumably, the RSACryptoServiceProvider is generated a new key Pair on each
execution, but this is exactly the opposite of what I want - I want it to
store and re-use the keys stored by the Container.
I have tried every combination of constructor arguments and properties etc.,
but I am really making zero progress and I am extremely frustrated.
Can anyone PLEASE advise as to how to persist these keys successfully?
thanks,
Robert
Friend Function WebIdentifier _
(ByVal RegistrationID As Integer, ByVal keyStoreName As String) _
As String
Try
' Get key pair from Container
Dim cp As New CspParameters()
cp.KeyContainerName = "abc"
Dim rsa As New RSACryptoServiceProvider(wuCryptShared.keySize, cp)
rsa.PersistKeyInCsp = True
' Wreck the original value in a predictable manner
Dim newId As Integer = (RegistrationID + 150977)
' Pad with leading zero's -> string
Dim padStr As String = newId.ToString
' Encode the resulting string
Dim encoding As New System.Text.ASCIIEncoding
Dim encoded As Byte() = encoding.GetBytes(padStr)
' Encrypt the encoded result
Dim encrypted As Byte() = rsa.Encrypt(encoded, False)
' & MD5 the encrypted data...
Dim Mdx5 As New MD5CryptoServiceProvider()
Dim resMd5 As Byte() = Mdx5.ComputeHash(encrypted)
' Ensure that returns is web-friendly by doing hex Conversion
' Create a new Stringbuilder to collect the bytes
' and create a string.
Dim sBuilder As New StringBuilder()
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
Dim i As Integer
For i = 0 To resMd5.Length - 1
sBuilder.Append(resMd5(i).ToString("x2"))
Next i
Dim resultStr As String = sBuilder.ToString
Return (resultStr)
Catch ex As Exception
Throw
End Try
End Function