VB.Net RSA Encrypting text using a given public key with RSACryptoServiceProvider is not possible

0

Well, there is so many examples, but no luck with any of them.

Here is my code first:

Imports System.Security.Cryptography
Imports Org.BouncyCastle.Asn1
Imports Org.BouncyCastle.Crypto.Parameters
Imports Org.BouncyCastle.Security

Public Class Form1

  Dim works = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0 FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/ 3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQAB"
  Dim dont = "MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAJhEArIWom9n7PBxEbRsQC/PGV03t+bGudrio9E1Jje5AgMBAAE="

  Public Shared Function ImportFromKey(ByVal secret_key As String) As RSACryptoServiceProvider

    Dim obj As Asn1Object = Asn1Object.FromByteArray(Convert.FromBase64String(secret_key))

    Dim publicKeySequence As DerSequence = CType(obj, DerSequence)
    Dim encodedPublicKey As DerBitString = CType(publicKeySequence(1), DerBitString)
    Dim publicKey As DerSequence = CType(Asn1Object.FromByteArray(encodedPublicKey.GetBytes()), DerSequence)
    Dim modulus As DerInteger = publicKey(0)
    Dim exponent As DerInteger = publicKey(1)

    Dim keyParameters As RsaKeyParameters = New RsaKeyParameters(False, modulus.PositiveValue, exponent.PositiveValue)

    Dim rsaParams As RSAParameters = DotNetUtilities.ToRSAParameters(keyParameters)

    Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider()
    RSA.ImportParameters(rsaParams)
    Return RSA
  End Function

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim rrsa = ImportFromKey(dont)

  End Sub

End Class

I have a client that provided me only this key ("dont") and give me a task to make this possible in VB.Net.

This code runs all the way up to:

RSA.ImportParameters(rsaParams)

Then it throws an error:

System.Security.Cryptography.CryptographicException
  HResult=0x80090004
  Message=Bad Length.

  Source=mscorlib
  StackTrace:
   at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   at System.Security.Cryptography.Utils._ImportKey(SafeProvHandle hCSP, Int32 keyNumber, CspProviderFlags flags, Object cspObject, SafeKeyHandle& hKey)
   at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters)
   at RSAEncryptVB.Form1.ImportFromKey(String secret_key) in C:\Users\sogrb\source\repos\RSAEncryptVB\RSAEncryptVB\Form1.vb:line 26
   at RSAEncryptVB.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\sogrb\source\repos\RSAEncryptVB\RSAEncryptVB\Form1.vb:line 32
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The key length is 256 bit (I've checked that).

Now there is another key, which I took from another example here on StackOverflow, and that key works with this code. Why "dont" key doesn't work and "works" key does?

.net
vb.net
encryption
cryptography
rsa
asked on Stack Overflow Oct 22, 2020 by Dejan Dozet

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0