none
RSA public key Error RRS feed

  • Question

  • Hello,

    Im using RSA encryption, and when I want to generate the paramethers I get an exception of stack over flow.

    This is the beggining of the code:

    public string GenerateRSA()
            {
                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048))
                {
                    PublicKey = rsa.ExportParameters(false);
                    PrivateKey = rsa.ExportParameters(true);
                    SymmetricKey = null;// To be generated by the client.
    
                    string stringPublicKey = rsa.ToXmlString(false);
                    return stringPublicKey;
                }
            }  
    
    

    When I get to that line: 

    PublicKey = rsa.ExportParameters(false);

    I get an exception here:

    public RSAParameters PublicKey
            {
                get { return PublicKey; }
                set { PublicKey = value; }
            }

    The SET statement sends the exception of stack over flow.

    Can someone help me with that?

    Sunday, April 22, 2018 10:50 AM

Answers

  • Change this:

    public RSAParameters PublicKey
    {
       
    get { return PublicKey; }
       
    set { PublicKey = value; }
    }

    To this:

    public RSAParameters PublicKey { get; set; }
           


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Proposed as answer by Fei HuModerator Monday, April 23, 2018 2:56 AM
    • Marked as answer by Roy Jiny Monday, April 23, 2018 8:56 PM
    Sunday, April 22, 2018 4:46 PM
    Moderator
  • Hello Roy,

    >>The SET statement sends the exception of stack over flow.

    When setting the PublicKey property, you invoke the setter, which invokes the setter which invokes the setter, etc.. Finally the memory is consumed over and the exceptions occurs. You could use auto properties as BonnieB suggested.

       private RSAParameters _publickey;
    
            public RSAParameters PublicKey
            {
                get { return _publickey; }
                set { _publickey = value; }
            }
    

    And then used it as below.

     using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048))
                {
                    _publickey = rsa.ExportParameters(false);
                ...
                }

    Best Regards,

    Neil Hu


    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 Roy Jiny Monday, April 23, 2018 8:57 PM
    Monday, April 23, 2018 3:03 AM
    Moderator

All replies

  • Change this:

    public RSAParameters PublicKey
    {
       
    get { return PublicKey; }
       
    set { PublicKey = value; }
    }

    To this:

    public RSAParameters PublicKey { get; set; }
           


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Proposed as answer by Fei HuModerator Monday, April 23, 2018 2:56 AM
    • Marked as answer by Roy Jiny Monday, April 23, 2018 8:56 PM
    Sunday, April 22, 2018 4:46 PM
    Moderator
  • Hello Roy,

    >>The SET statement sends the exception of stack over flow.

    When setting the PublicKey property, you invoke the setter, which invokes the setter which invokes the setter, etc.. Finally the memory is consumed over and the exceptions occurs. You could use auto properties as BonnieB suggested.

       private RSAParameters _publickey;
    
            public RSAParameters PublicKey
            {
                get { return _publickey; }
                set { _publickey = value; }
            }
    

    And then used it as below.

     using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048))
                {
                    _publickey = rsa.ExportParameters(false);
                ...
                }

    Best Regards,

    Neil Hu


    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 Roy Jiny Monday, April 23, 2018 8:57 PM
    Monday, April 23, 2018 3:03 AM
    Moderator