Exception with RsaKeyParameters constructor

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Exception with RsaKeyParameters constructor

sesteve
Hi,

I am trying to create a public RSA key from known modulus and exponent values.
I execute this line:


   byte[] Mod = { 0x8C, 0x20, ... };
   byte[] Exp = { 0x01, 0x00, 0x01 };
   var pubKey = new RsaKeyParameters(false, new BigInteger(Mod), new BigInteger(Exp));


This was being executed in BouncyCastel version 1.4.0.0 without troubles, but when I execute it in version 1.8.1.0 I get the exeption:


ex
{"Not a valid RSA modulus\r\nParameter name: modulus"}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: 0x80070057
    HelpLink: null
    InnerException: null
    Message: "Not a valid RSA modulus\r\nParameter name: modulus"
    ParamName: "modulus"
    Source: "crypto"
    StackTrace: "   at Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters..ctor(Boolean isPrivate, BigInteger modulus, BigInteger exponent) in C:\\Projects\\BouncyCastle\\C#\\bccrypto-csharp-1.8.1\\Crypto\\src\\crypto\\parameters\\RsaKeyParameters.cs:line 25\r\n..."
    TargetSite: {Void .ctor(Boolean, Org.BouncyCastle.Math.BigInteger, Org.BouncyCastle.Math.BigInteger)}


It seems to be that "new BigInteger(Mod)" is returning with sign as -1. Does anyone know something about this?
I have seen in BouncyCastle source code:


        public RsaKeyParameters(
            bool isPrivate,
            BigInteger modulus,
            BigInteger exponent)
                        : base(isPrivate)
        {
                        if (modulus == null)
                                throw new ArgumentNullException("modulus");
                        if (exponent == null)
                                throw new ArgumentNullException("exponent");
                        if (modulus.SignValue <= 0)
                                throw new ArgumentException("Not a valid RSA modulus", "modulus");
                        if (exponent.SignValue <= 0)
                                throw new ArgumentException("Not a valid RSA exponent", "exponent");

                        this.modulus = modulus;
                        this.exponent = exponent;
        }


The If statements aren't in previous version. So, Is this a trouble in BC or a change in RSA?

Thanks a lot.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Exception with RsaKeyParameters constructor

Peter Dettman-3
The new checks were added to catch exactly this sort of mistake early.

When constructing a BigInteger from an unsigned value in an array of
bytes, you should be using e.g.:

    new BigInteger(1, Mod), new BigInteger(1, Exp)

Regards,
Pete Dettman


On 25/10/2016 3:49 PM, sesteve wrote:

> Hi,
>
> I am trying to create a public RSA key from known modulus and exponent
> values.
> I execute this line:
>
>
>    byte[] Mod = { 0x8C, 0x20, ... };
>    byte[] Exp = { 0x01, 0x00, 0x01 };
>    var pubKey = new RsaKeyParameters(false, new BigInteger(Mod), new
> BigInteger(Exp));
>
>
> This was being executed in BouncyCastel version 1.4.0.0 without troubles,
> but when I execute it in version 1.8.1.0 I get the exeption:
>
> ex
> {"Not a valid RSA modulus\r\nParameter name: modulus"}
>     Data: {System.Collections.ListDictionaryInternal}
>     HResult: 0x80070057
>     HelpLink: null
>     InnerException: null
>     Message: "Not a valid RSA modulus\r\nParameter name: modulus"
>     ParamName: "modulus"
>     Source: "crypto"
>     StackTrace: "   at
> Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters..ctor(Boolean isPrivate,
> BigInteger modulus, BigInteger exponent) in
> C:\\Projects\\BouncyCastle\\C#\\bccrypto-csharp-1.8.1\\Crypto\\src\\crypto\\parameters\\RsaKeyParameters.cs:line
> 25\r\n..."
>     TargetSite: {Void .ctor(Boolean, Org.BouncyCastle.Math.BigInteger,
> Org.BouncyCastle.Math.BigInteger)}
>
>
> It seems to be that "new BigInteger(Mod)" is returning with sign as -1. Does
> anyone know something about this?
> I have seen in BouncyCastle source code:
>
>
> public RsaKeyParameters(
>             bool isPrivate,
>             BigInteger modulus,
>             BigInteger exponent)
> : base(isPrivate)
>         {
> if (modulus == null)
> throw new ArgumentNullException("modulus");
> if (exponent == null)
> throw new ArgumentNullException("exponent");
> if (modulus.SignValue <= 0)
> throw new ArgumentException("Not a valid RSA modulus", "modulus");
> if (exponent.SignValue <= 0)
> throw new ArgumentException("Not a valid RSA exponent", "exponent");
>
> this.modulus = modulus;
> this.exponent = exponent;
>         }
>
>
> The If statements aren't in previous version. So, Is this a trouble in BC or
> a change in RSA?
>
> Thanks a lot.
>
>
>
>
>
> --
> View this message in context: http://bouncy-castle.1462172.n4.nabble.com/Exception-with-RsaKeyParameters-constructor-tp4658378.html
> Sent from the Bouncy Castle - Dev mailing list archive at Nabble.com.
>


Loading...