drgmak drgmak - 1 year ago 158
Vb.net Question

Public Overloads Sub New() is obsolete

I have created a function in C# which Converts bouncy castle AsymmetricKeyParameter to RSA.

public static RSA AsymmetricKeyParameterToRSA(AsymmetricKeyParameter key)
// corresponding private key
PrivateKeyInfo info = PrivateKeyInfoFactory.CreatePrivateKeyInfo(key);

var seq = (Asn1Sequence)Asn1Object.FromByteArray(info.PrivateKey.GetDerEncoded());
if (seq.Count != 9)
throw new PemException("Malformed sequence in RSA private key.");

var rsa = new RsaPrivateKeyStructure(seq);
RsaPrivateCrtKeyParameters rsaparams = new RsaPrivateCrtKeyParameters(
rsa.Modulus, rsa.PublicExponent, rsa.PrivateExponent, rsa.Prime1, rsa.Prime2, rsa.Exponent1, rsa.Exponent2, rsa.Coefficient);

return DotNetUtilities.ToRSA(rsaparams);

When I convert the same method to VB:

Public Shared Function AsymmetricKeyParameterToRSA(key As AsymmetricKeyParameter) As RSA
' corresponding private key
Dim info As PrivateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(key)

Dim seq = DirectCast(Asn1Object.FromByteArray(info.PrivateKey.GetDerEncoded()), Asn1Sequence)
If seq.Count <> 9 Then
Throw New PemException("Malformed sequence in RSA private key.")
End If

Dim rsa = New RsaPrivateKeyStructure(seq)
Dim rsaparams As New RsaPrivateCrtKeyParameters(rsa.Modulus, rsa.PublicExponent, rsa.PrivateExponent, rsa.Prime1, rsa.Prime2, rsa.Exponent1,
rsa.Exponent2, rsa.Coefficient)

Return DotNetUtilities.ToRSA(rsaparams)
End Function

I get a warning about
Dim rsa = New RsaPrivateKeyStructure(seq)
which says that

Warning BC40000 'Public Overloads Sub New(seq As Asn1Sequence)' is obsolete: 'Use 'GetInstance' method(s) instead'.

What could i be missing?

Answer Source

I am not familiar with the specific library you're using, but the warning is very clear - don't use New, use getInstance:

Dim rsa = RsaPrivateKeyStructure.getInstance(seq)

A quick Google search for term "RsaPrivateKeyStructure" revealed this documentation, which confirms existence of a suitable getInstance overload.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download