VB.NET RSA Decrypt Data size too long

blinkor12 101 Reputation points
2021-07-01T03:52:06.143+00:00

I'm trying to implement RSA encryption in VB.NET. The encryption method is fine, but the decryption method says that the data to be decrypted is too long. I'm not sure what's wrong.

These is the error I'm getting at decryption:

System.Exception: 'Decrypt(): RSADecrypt(): The data to be decrypted exceeds the maximum for this modulus of 128 bytes.'

CODE TO ENCRYPT/DECRYPT:

'Encrypt String "demo"

        Dim key As String = "demo"

        'Use Public Key
        Dim publicKey As String = "<RSAKeyValue><Modulus>upXnnvLMiY1VDwSfBs/uRAdfmWXWqV/X/+JNLYGnwB74OEm3Au8fxH9fFlpErA+STxLF7OuedQg/ZRa/SAHnIPEaTZNgcTQ6zWbq9WchD/WokTu444qxxnqoUQMnEvOm4DvaJlcfbZ4UEkzVr61p25iBgfBpoeCqYCzaOWROI5k=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
        Dim encryptedKey As RSAResult = RSA.Encrypt(key, publicKey)

        'The encrypted data can be read as base64 value.
        MsgBox(encryptedKey.AsBase64String())


        'DECRYPTION METHOD:

        'The base64 value is decrypted by converting it to bytes.

        Dim vout As Byte() = System.Text.Encoding.Unicode.GetBytes(encryptedKey.AsBase64String())


        'Use the function to decrypt the value with the private key.

        Dim decrypt As RSAResult = RSA.Decrypt(vout, "<RSAKeyValue><Modulus>upXnnvLMiY1VDwSfBs/uRAdfmWXWqV/X/+JNLYGnwB74OEm3Au8fxH9fFlpErA+STxLF7OuedQg/ZRa/SAHnIPEaTZNgcTQ6zWbq9WchD/WokTu444qxxnqoUQMnEvOm4DvaJlcfbZ4UEkzVr61p25iBgfBpoeCqYCzaOWROI5k=</Modulus><Exponent>AQAB</Exponent><P>1U+YvYCJuNo8cJdpAs2E4vMV/yQFjatq9PX2H9z63ER2MoVorBJ6ao3IFotCjfyExUT9Z6S8UmB37zTWBsu3Tw==</P><Q>3+0bdbPnxRvlYBYrlRZnn3d6qw+pt5oEMdRh9tEUkUyzqKMV9U5U4jOahJvZgWYpu7obAwEdgH31i/sQ3km8lw==</Q><DP>phziOVDL72QkBRoj8MbTYVkkHBiVKon/NNwF2zJfOQEnephhtv58zlWzJ7szDRdE3cqn+/pRnWT6gRGISqqCCQ==</DP><DQ>OwrJsr883Tv9vtgZpbXCnMlkDraSiVaSxJC7Q9jdE3Kl7FET+RouR3ZTx6NQNk45ssXfWWtx2+9aTw5j5DiNAQ==</DQ><InverseQ>vg84+HMDdim0TeoFvVKORLjQ/9lPSqNPN3N3kCE/wkgKnadRvJcF/tfebsiKiRSlY/NoKFCA8dvNkWPF7hjdbQ==</InverseQ><D>U4AcjNyZvoEabMMjq/fH6HfoR4z0uQ3ka2ZKJbBdczQo33/VwCFldRmPY0B4dQvu7SY4n2N+oV9Uh5fbeI0pcqfm7unEJwbZynRM+ZMlCEVdnQGCvDbJWnd04h5ptbL+6oRxXN2P9efs+iWpO/S/v2VC/phXWqMZ/32U/KBkrPU=</D></RSAKeyValue>")

        MsgBox(decrypt.AsString)

CLASS "RSA":

Imports System.Security.Cryptography
Imports System.Text
Public Class RSA
    Public Shared Function Encrypt(ByVal Data As String, ByVal Publickey As String) As RSAresult
        Try
            Dim ByteConverter As New UnicodeEncoding()
            Return Encrypt(ByteConverter.GetBytes(Data), Publickey)
        Catch ex As Exception
            Throw New Exception("Encrypt(String): " & ex.Message, ex)
        End Try
    End Function

    Public Shared Function Encrypt(ByVal Data() As Byte, ByVal Publickey As String) As RSAresult
        Try
            Dim RSA As System.Security.Cryptography.RSACryptoServiceProvider = New System.Security.Cryptography.RSACryptoServiceProvider()
            RSA.FromXmlString(Publickey)
            Return New RSAresult(RSAEncrypt(Data, RSA.ExportParameters(False), False))
        Catch ex As Exception
            Throw New Exception("Encrypt(Bytes): " & ex.Message, ex)
        End Try
    End Function

    Public Shared Function Decrypt(ByVal Data() As Byte, ByVal Privatekey As String) As RSAresult
        Try
            Dim RSA As System.Security.Cryptography.RSACryptoServiceProvider = New System.Security.Cryptography.RSACryptoServiceProvider()
            RSA.FromXmlString(Privatekey)
            Dim Result As New RSAresult(RSADecrypt(Data, RSA.ExportParameters(True), False))
            Return Result
        Catch ex As Exception
            Throw New Exception("Decrypt(): " & ex.Message, ex)
        End Try
    End Function

    Private Shared Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            Dim encryptedData() As Byte
            Using RSA As New RSACryptoServiceProvider
                RSA.ImportParameters(RSAKeyInfo)
                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding)
            End Using
            Return encryptedData
        Catch e As CryptographicException
            Throw New Exception("RSAEncrypt(): " & e.Message, e)
        End Try
    End Function

    Private Shared Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            Dim decryptedData() As Byte
            Using RSA As New RSACryptoServiceProvider
                RSA.ImportParameters(RSAKeyInfo)
                decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding)
            End Using
            Return decryptedData
        Catch e As CryptographicException
            Throw New Exception("RSADecrypt(): " & e.Message, e)
        End Try
    End Function
End Class

CLASS "RSAresult":

Imports System.Text
Public Class RSAResult
    Private _Data() As Byte
    Public Sub New(ByVal Data() As Byte)
        _Data = Data
    End Sub
    Public ReadOnly Property AsBytes() As Byte()
        Get
            Return _Data
        End Get
    End Property
    Public ReadOnly Property AsString() As String
        Get
            Dim ByteConverter As New UnicodeEncoding()
            Return ByteConverter.GetString(_Data)
        End Get
    End Property
    Public ReadOnly Property AsBase64String() As String
        Get
            Return Convert.ToBase64String(_Data)
        End Get
    End Property
End Class
Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-07-01T11:53:56.47+00:00

    Try this:

    . . .
    Dim encrypted_string = encryptedKey.AsBase64String()
    MsgBox(encrypted_string)
    Dim vout As Byte() = Convert.FromBase64String(encrypted_string)
    Dim decrypt As RSAResult = RSA.Decrypt(vout, . . .
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.