miketri
miketri

Reputation: 13

Creating public RSA key with RSACng: specified key is not a valid size for this algorithm

I'm currently trying to create a RSA encryption key as part of a web service authentication for Tableau.

I'm an noob when it comes to cryptography and can't quite figure out how to generate the public key.

I have a modulus that is 256 bytes and the exponent is 5 bytes.

Imports System.Security.Cryptography

Sub CreateKey() 

    Dim modulus As String= "bba199e30e41fc23078687dfea032c41bc354518115887f3f59c07ee51628c8d354243d2484a7b7278a2b47e92dd5df7c815ad96b5b59f4e202452b5045587d2eca12fb72aba39bb80048f4a9c83fb1e0187e3d73c323ee35f9437828e5abecb0b894dac45217722eb1d6bbab7ff6661b5f728ca1b0be01f138c6f5f1db5a177"

    Dim exponent As String = "10001"

    Dim key = New RSAParameters
    key.Exponent = Encoding.UTF8.GetBytes(exponent)
    key.Modulus = Encoding.UTF8.GetBytes(modulus)

    Dim rsa As RSACng = New RSACng()
    rsa.ImportParameters(key) 'exception thrown here

End Sub

Upvotes: 1

Views: 1204

Answers (1)

Iridium
Iridium

Reputation: 23721

Your modulus and exponent are hex-encoded values. Performing Encoding.UTF8.GetBytes(...) does not do the necessary decoding to convert the string into the correct modulus/exponent.

You will need to either:

  • Write the necessary code to decode the hex string into the appropriate byte array and use this instead of Encoding.UTF8.GetBytes(...) (there are plenty of examples of such a function on StackOverflow and elsewhere)
  • Just use byte-arrays instead of strings, e.g.:

Replace:

Dim modulus As String = "bba199..."

With:

Dim modulus As Byte() = New Byte() { &HBB, &HA1, &H99, ...}

Upvotes: 1

Related Questions