|
Post by thinker on Apr 23, 2008 12:07:06 GMT -5
Hi, I am attempting to use RSACryptoServiceProvider. In .Net the constructor takes a parameter of type CspParameters. I can not figure out how to initialize the object with using CspParameters.
Dim rsa As New RSACryptoServiceProvider Dim cp As New CspParameters cp.KeyContainerName = "RSAKEY" & CInt(Int((10000 * Rnd()) + 1))
Any help would be greatly appreciated.
|
|
|
Post by Kelly Ethridge on Apr 23, 2008 12:34:11 GMT -5
Hello Thinker,
There is a constructor you can use that accepts a CspParameter object. You would utilize it like the following:
Public Sub Main() Dim csp As New CspParameters ' set parameters Dim rsa As RSACryptoServiceProvider Set rsa = Cor.NewRSACryptoServiceProvider(, csp) ' rest of the code End Sub
When you type Cor. you will see a lot of additional constructors. This is how VBCorLib simulates having constructor parameters. So if you are attempting to create an object with parameters, this will be the first place to look.
I hope this helps get you going in the right direction!
Thanks, kelly
|
|
|
Post by thinker on Apr 23, 2008 12:56:37 GMT -5
Thanks a lot, Kelly.
Where would the documentation on "Cor" be located?
Darrell
|
|
|
Post by Kelly Ethridge on Apr 23, 2008 13:07:17 GMT -5
You can navigate to the online documentation from the main page or the compile help file that is included with the downloads: Here is a direct link for the online documentation: www.kellyethridge.com/vbcorlib/doc/VBCorLib.htmlAll of the Cor. methods are located in the Constructors/b] link on that page.
Kelly
|
|
|
Post by thinker on Apr 23, 2008 13:38:05 GMT -5
Thanks, again.
I am trying to implement a VB6 version of a component that I have working fine in VB 2005. The problem that I am currently experiencing occurs when I try to export the parameters into an RSAParameters object. There are several fields on the RSACryptoServiceProvider that are not populated (KeyExchangeAlgorithm, CSPKeyContainerInfo has invalid algorithm specified, etc.) The same code works fine in .Net. Any suggestions on what I am doing incorrectly?
Thanks
Dim i As Integer Dim j As Integer Dim encryptedData() As Byte Dim decryptedData() As Byte Dim ByteConverter As New UnicodeEncoding Dim rsa As New RSACryptoServiceProvider Dim cp As CspParameters Dim rsaTypeIn As Long rsaTypeIn = 1 Set cp = Cor.NewCspParameters(rsaTypeIn) cp.KeyContainerName = "RSAKEY" & CInt(Int((10000 * Rnd()) + 1)) Set rsa = Cor.NewRSACryptoServiceProvider(1, cp) Dim RSAParams As RSAParameters RSAParams = rsa.ExportParameters(False) Dim sendlength As Integer Dim expLen As Integer Dim modLen As Integer expLen = UBound(RSAParams.Exponent) - LBound(RSAParams.Exponent) + 1 modLen = UBound(RSAParams.Modulus) - LBound(RSAParams.Modulus) + 1 sendlength = expLen + modLen Dim sendPublicKey() As Byte ReDim sendPublicKey(sendlength)
For i = 0 To expLen sendPublicKey(j) = RSAParams.Exponent(i) j = j + 1 Next For i = 0 To modLen ' sendPublicKey(j) = RSAParams.Modulus(i) j = j + 1 Next
|
|
|
Post by Kelly Ethridge on Apr 23, 2008 14:05:00 GMT -5
Hello,
You're right that the algorithm is not correct in your case. What you can do is set the KeyNumber to 1 in your cp instance. Also, you need to change
Set rsa = Cor.NewRSACryptoServiceProvider(1, cp)
to
Set rsa = Cor.NewRSACryptoServiceProvider(, cp) ' or Set rsa = Cor.NewRSACryptoServiceProvider(ValidKeySize, cp)
and you need to change
RSAParams = rsa.ExportParameters(False)
to
Set RSAParams = rsa.ExportParameters(False)
And don't forget that you can't directly access a returned byte array's elements like you have at the end. You'll need to assign them to a local variable first.
Kelly
|
|
|
Post by thinker on Apr 23, 2008 14:16:04 GMT -5
Hi,
I made the changes that you mentioned, but I am still receiving an "Invalid Algorithm Specified" error on the CspKeyContainerInfo field. Is there a way to set the Algorithm?
Thanks in advance.
Dim i As Integer Dim j As Integer Dim encryptedData() As Byte Dim decryptedData() As Byte Dim ByteConverter As New UnicodeEncoding Dim rsa As New RSACryptoServiceProvider Dim cp As CspParameters Dim rsaTypeIn As Long rsaTypeIn = 1 Set cp = Cor.NewCspParameters(rsaTypeIn) cp.KeyContainerName = "RSAKEY" & CInt(Int((10000 * Rnd()) + 1)) Set rsa = Cor.NewRSACryptoServiceProvider(1024, cp) Dim RSAParams As RSAParameters Set RSAParams = rsa.ExportParameters(False) Dim sendlength As Integer Dim expLen As Integer Dim modLen As Integer expLen = UBound(RSAParams.Exponent) - LBound(RSAParams.Exponent) + 1 modLen = UBound(RSAParams.Modulus) - LBound(RSAParams.Modulus) + 1 sendlength = expLen + modLen Dim sendPublicKey() As Byte ReDim sendPublicKey(sendlength) Dim holdExp() As Byte holdExp = RSAParams.Exponent For i = 0 To expLen sendPublicKey(j) = holdExp(i) j = j + 1 Next Dim holdMod() As Byte holdMod = RSAParams.Modulus For i = 0 To modLen sendPublicKey(j) = holdMod(i) j = j + 1 Next
|
|
|
Post by Kelly Ethridge on Apr 23, 2008 14:23:52 GMT -5
You still aren't setting the KeyNumber property on your cp instance:
cp.KeyNumber = 1
Kelly
|
|