none
Closed Question RRS feed

Answers

  • Hi MuzafferAnlas, 

    Thank you for posting here.

    According to your description, I make a test on my side and reproduce your problem.

    I find that you create two ‘RSACryptoServiceProvider’ in your code which cause the problem.

    Please use the same ‘RSACryptoServiceProvider’ when encypting and decrypting data.

    For example:

        Private fileName As String = "your path"
        Private encoding As Encoding = New UTF8Encoding()
        Private rsa As RSACryptoServiceProvider = New RSACryptoServiceProvider()
        Private plainText As Byte()
        Private cypherText As Byte()
    
        Public Sub Main()
            If fileName <> "" Then
                Dim text = "CREATED CODE"
                plainText = encoding.GetBytes(text)
                cypherText = Encrypt(plainText, rsa.ExportParameters(False), False)
                File.WriteAllBytes(fileName, cypherText)
                Dim bt As Byte() = File.ReadAllBytes(fileName)
                Dim decryptedText As Byte() = Decrypt(bt, rsa.ExportParameters(True), False)
                Dim s As String = encoding.GetString(decryptedText)
                Console.WriteLine(s)
            Else
                Console.WriteLine("Not Found Config File !", "Alert")
            End If
    
            Console.ReadLine()
        End Sub
    
        Public Function Encrypt(ByVal data As Byte(), ByVal rsakey As RSAParameters, ByVal doPadding As Boolean) As Byte()
            Try
    
                Using rsa As RSACryptoServiceProvider = New RSACryptoServiceProvider()
                    rsa.ImportParameters(rsakey)
                    Dim endData = rsa.Encrypt(data, doPadding)
                    Return endData
                End Using
    
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Return Nothing
            End Try
        End Function
    
        Public Function Decrypt(ByVal data As Byte(), ByVal rsakey As RSAParameters, ByVal doPadding As Boolean) As Byte()
            Dim decryptData As Byte()
    
            Try
    
                Using rsa As RSACryptoServiceProvider = New RSACryptoServiceProvider()
                    rsa.ImportParameters(rsakey)
                    decryptData = rsa.Decrypt(data, doPadding)
                    Return decryptData
                End Using
    
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Return Nothing
            End Try
        End Function
    

    Result of my test:


    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Anonimersder Tuesday, December 31, 2019 10:58 AM
    Tuesday, December 31, 2019 7:30 AM
    Moderator
  • Hi MuzafferAnlas, 

    Thanks for your feedback.

    You can refer to the following steps to encrypt in one program and decrypt in another.

    First of all, you need to use following code to generate public and private key.

            Dim cspParams As CspParameters = New CspParameters()
            cspParams.KeyContainerName = "XML_ENC_RSA_KEY"
            Dim rsaKey As RSACryptoServiceProvider = New RSACryptoServiceProvider(cspParams)
            Dim keyXML As String = rsaKey.ToXmlString(True)
            File.WriteAllText(path, keyXML)

    Then open the file and select your public key like this:

    <RSAKeyValue><Modulus>YOURMODULUS</Modulus><Exponent>YOUREXP</Exponent></RSAKeyValue>

    Right click your encrypt project => Properties => Settings, name ‘PublicKeyXml’ and copy your public key string to ‘Value’.

    For example:

    Then copy all string and paste it in your decrypt project settings:

    Finally, use the following code to encrypt and decrypt data.

    Encrypt:

    Module Module1
        Private fileName As String = "your file path"
        Private RsaSec As Rsa_Encrypt = New Rsa_Encrypt()
        Dim encoding As Encoding = New UTF8Encoding
        Dim rsa As RSACryptoServiceProvider = New RSACryptoServiceProvider()
        Dim plainText As Byte()
        Dim cypherText As Byte()
        Sub Main()
            If fileName <> "" Then
                Dim text = "CREATED CODE"
    
                If (Not String.IsNullOrEmpty(text)) Then
                    plainText = encoding.GetBytes(text)
                    cypherText = RsaSec.Encrypt(plainText, True)
                    File.WriteAllBytes(fileName, cypherText)
                End If
            Else
                MsgBox("Not Found Config File !", "Alert")
            End If
        End Sub
    End Module
    
    Public Class Rsa_Encrypt
        Public Function Encrypt(ByVal data() As Byte, ByVal doPadding As Boolean) As Byte()
            Try
                Using rsa As New RSACryptoServiceProvider()
                    rsa.FromXmlString(My.MySettings.Default.PublicKeyXml)
                    Dim endData = rsa.Encrypt(data, doPadding)
                    Return endData
                End Using
    
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Return Nothing
            End Try
        End Function
    End Class

    Decrypt:

    Module Module1
        Private fileName As String = "your file path"
        Private RsaSec As Rsa_Decrypt = New Rsa_Decrypt()
        Dim encoding As Encoding = New UTF8Encoding
        Dim Result
        Sub Main()
            If fileName <> "" Then
                Result = File.ReadAllBytes(fileName)
                Dim decryptedText As Byte() = RsaSec.Decrypt(Result, True)
                Console.WriteLine(" Decrypted Text : " & encoding.GetString(decryptedText))
                Console.ReadLine()
            End If
        End Sub
    End Module
    
    
    Public Class Rsa_Decrypt
        Public Function Decrypt(ByVal data() As Byte, ByVal doPadding As Boolean) As Byte()
            Dim decryptData As Byte()
            Try
                Using rsa As New RSACryptoServiceProvider()
                    rsa.FromXmlString(My.MySettings.Default.PrivateKeyXml)
                    decryptData = rsa.Decrypt(data, doPadding)
                    Return decryptData
                End Using
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Return Nothing
            End Try
        End Function
    End Class

    Result:


    Best Regards,

    Xingyu Zhao





    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 1, 2020 4:19 AM
    Moderator

All replies

  • Hi

    You will need to show the calling code.It looks like a straight copy/paste set of code you have posted but you do not show how you are trying to use it.


    Regards Les, Livingston, Scotland

    Monday, December 30, 2019 3:57 PM
  • Hi MuzafferAnlas, 

    Thank you for posting here.

    According to your description, I make a test on my side and reproduce your problem.

    I find that you create two ‘RSACryptoServiceProvider’ in your code which cause the problem.

    Please use the same ‘RSACryptoServiceProvider’ when encypting and decrypting data.

    For example:

        Private fileName As String = "your path"
        Private encoding As Encoding = New UTF8Encoding()
        Private rsa As RSACryptoServiceProvider = New RSACryptoServiceProvider()
        Private plainText As Byte()
        Private cypherText As Byte()
    
        Public Sub Main()
            If fileName <> "" Then
                Dim text = "CREATED CODE"
                plainText = encoding.GetBytes(text)
                cypherText = Encrypt(plainText, rsa.ExportParameters(False), False)
                File.WriteAllBytes(fileName, cypherText)
                Dim bt As Byte() = File.ReadAllBytes(fileName)
                Dim decryptedText As Byte() = Decrypt(bt, rsa.ExportParameters(True), False)
                Dim s As String = encoding.GetString(decryptedText)
                Console.WriteLine(s)
            Else
                Console.WriteLine("Not Found Config File !", "Alert")
            End If
    
            Console.ReadLine()
        End Sub
    
        Public Function Encrypt(ByVal data As Byte(), ByVal rsakey As RSAParameters, ByVal doPadding As Boolean) As Byte()
            Try
    
                Using rsa As RSACryptoServiceProvider = New RSACryptoServiceProvider()
                    rsa.ImportParameters(rsakey)
                    Dim endData = rsa.Encrypt(data, doPadding)
                    Return endData
                End Using
    
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Return Nothing
            End Try
        End Function
    
        Public Function Decrypt(ByVal data As Byte(), ByVal rsakey As RSAParameters, ByVal doPadding As Boolean) As Byte()
            Dim decryptData As Byte()
    
            Try
    
                Using rsa As RSACryptoServiceProvider = New RSACryptoServiceProvider()
                    rsa.ImportParameters(rsakey)
                    decryptData = rsa.Decrypt(data, doPadding)
                    Return decryptData
                End Using
    
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Return Nothing
            End Try
        End Function
    

    Result of my test:


    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Anonimersder Tuesday, December 31, 2019 10:58 AM
    Tuesday, December 31, 2019 7:30 AM
    Moderator
  • It stops somewhere in your code. But without knowing where people can only give you a general answer which every kid can find on Internet. 


    Success
    Cor

    Tuesday, December 31, 2019 8:38 AM
  • Hi MuzafferAnlas, 

    Thanks for your feedback.

    You can refer to the following steps to encrypt in one program and decrypt in another.

    First of all, you need to use following code to generate public and private key.

            Dim cspParams As CspParameters = New CspParameters()
            cspParams.KeyContainerName = "XML_ENC_RSA_KEY"
            Dim rsaKey As RSACryptoServiceProvider = New RSACryptoServiceProvider(cspParams)
            Dim keyXML As String = rsaKey.ToXmlString(True)
            File.WriteAllText(path, keyXML)

    Then open the file and select your public key like this:

    <RSAKeyValue><Modulus>YOURMODULUS</Modulus><Exponent>YOUREXP</Exponent></RSAKeyValue>

    Right click your encrypt project => Properties => Settings, name ‘PublicKeyXml’ and copy your public key string to ‘Value’.

    For example:

    Then copy all string and paste it in your decrypt project settings:

    Finally, use the following code to encrypt and decrypt data.

    Encrypt:

    Module Module1
        Private fileName As String = "your file path"
        Private RsaSec As Rsa_Encrypt = New Rsa_Encrypt()
        Dim encoding As Encoding = New UTF8Encoding
        Dim rsa As RSACryptoServiceProvider = New RSACryptoServiceProvider()
        Dim plainText As Byte()
        Dim cypherText As Byte()
        Sub Main()
            If fileName <> "" Then
                Dim text = "CREATED CODE"
    
                If (Not String.IsNullOrEmpty(text)) Then
                    plainText = encoding.GetBytes(text)
                    cypherText = RsaSec.Encrypt(plainText, True)
                    File.WriteAllBytes(fileName, cypherText)
                End If
            Else
                MsgBox("Not Found Config File !", "Alert")
            End If
        End Sub
    End Module
    
    Public Class Rsa_Encrypt
        Public Function Encrypt(ByVal data() As Byte, ByVal doPadding As Boolean) As Byte()
            Try
                Using rsa As New RSACryptoServiceProvider()
                    rsa.FromXmlString(My.MySettings.Default.PublicKeyXml)
                    Dim endData = rsa.Encrypt(data, doPadding)
                    Return endData
                End Using
    
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Return Nothing
            End Try
        End Function
    End Class

    Decrypt:

    Module Module1
        Private fileName As String = "your file path"
        Private RsaSec As Rsa_Decrypt = New Rsa_Decrypt()
        Dim encoding As Encoding = New UTF8Encoding
        Dim Result
        Sub Main()
            If fileName <> "" Then
                Result = File.ReadAllBytes(fileName)
                Dim decryptedText As Byte() = RsaSec.Decrypt(Result, True)
                Console.WriteLine(" Decrypted Text : " & encoding.GetString(decryptedText))
                Console.ReadLine()
            End If
        End Sub
    End Module
    
    
    Public Class Rsa_Decrypt
        Public Function Decrypt(ByVal data() As Byte, ByVal doPadding As Boolean) As Byte()
            Dim decryptData As Byte()
            Try
                Using rsa As New RSACryptoServiceProvider()
                    rsa.FromXmlString(My.MySettings.Default.PrivateKeyXml)
                    decryptData = rsa.Decrypt(data, doPadding)
                    Return decryptData
                End Using
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Return Nothing
            End Try
        End Function
    End Class

    Result:


    Best Regards,

    Xingyu Zhao





    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 1, 2020 4:19 AM
    Moderator
  • Hello Xingyu Zhao,

    Thank you so much. ^^  

    I currently understood and running applications.

    <RSAKeyValue><Modulus>qYc0toGInY0u48gTNRIXUZoTw1InJ58kgZFRqJzkX5jfgI6AKG3TyE1szXHmikw0qik0TH1R1dLrXT5GwQWwRr38c3DwF3ntHyz7iBxFGfLddU0f+QnpGJXiQOV3SI3haL0VZFNA/wVgdSeTCz9f3azReIJqkpMSZPD+LZfwq8k=</Modulus><Exponent>AQAB</Exponent><P>zwnMxLWlrlplDH7mvQ85rybqru/AH7bH9rHQxMpjui6vM0MNaQBNmhg0o2mrPF5Y1IhgJ4waHhV7bC9V4tA1Vw==</P><Q>0Z6GJZcif8zm49yfx2dD3sGokL+gjbRCc/4DFaC+aJaL/T/z9Y2VFf9/oleZoXvZNdyaHJepgfk3Q06P+5FT3w==</Q><DP>B9dFLAX9GldOEGUV8/8JnjqJFMr7qbfQZuVhFLLdiAWQmQ5nCZz7U2SrSa0M0SJ5lsL7cUP7ETybCgPY6tgbzQ==</DP><DQ>bd1lN9x/w3X0aecfg1aXVvAvVjZpgvvAfKTcnwBR6+VD1+cmCUQ8qHvyPHcnWWDdFoLZ7zR6LlodYdSSWGryBw==</DQ><InverseQ>Ury6arDRbsMlJTLQMmhBx58FyoHRjJO/tkWFkI1mr1MND5N8urKXNU4pjMR5YZhHO804LHLUfKvt3b12AjX3Gw==</InverseQ><D>GF1ZQx2sz8/5vD0GHauC9HRrgLfu7q/dpE2Jae1amS60eJqCas0WEgSmrYRAOQIo04fmf1PHji6pKxaHPridk+FaNO6EzCG8xdZBvNjflfSgiVibJRqt90VYEvnyfhcIzrcUVuqKT3kmYqpR5uwfNQK8CwzSSm38ywLE+AoAO10=</D></RSAKeyValue>

    Just I don't understand what these tags mean ^^

    " ( <P></P>

    <Q></Q>

    <DP></DP>

    <DQ></DQ>

    <InverseQ></InverseQ>

    <D></D> ) " 


    Thursday, January 2, 2020 12:01 AM
  • Hi MigrationUser 1,

    The public key consists of the modulus n and the public (or encryption) exponent e. The private key consists of the modulus n and the private (or decryption) exponent d, which must be kept secret.

    p and q in the private key are the primes from the key generation.

    DP is d (mod p − 1), DQ is d (mod q− 1).

    p, q, and φ(n) must also be kept secret because they can be used to calculate d.

    All of this are related to the Chinese remainder algorithm.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 2, 2020 1:42 AM
    Moderator
  • Hello Xingyu Zhao,

    Thank you so much. ^^  

    Thursday, January 2, 2020 6:12 AM