generatePrivate(KeySpec keySpec) { if (!(keySpec is PKCS8EncodedKeySpec)) { throw new net.named_data.jndn.util.InvalidKeySpecException ("RsaKeyFactory.generatePrivate expects a PKCS8EncodedKeySpec"); } try { // Decode the PKCS #8 private key. var parsedNode = DerNode.parse(new ByteBuffer(((PKCS8EncodedKeySpec)keySpec).KeyDer), 0); var pkcs8Children = parsedNode.getChildren(); var algorithmIdChildren = DerNode.getSequence(pkcs8Children, 1).getChildren(); var oidString = ((DerNode.DerOid)algorithmIdChildren[0]).toVal().ToString(); var rsaPrivateKeyDer = ((DerNode)pkcs8Children[2]).getPayload(); if (oidString != RSA_ENCRYPTION_OID) { throw new net.named_data.jndn.util.InvalidKeySpecException ("The PKCS #8 private key is not RSA_ENCRYPTION"); } // Decode the PKCS #1 RSAPrivateKey. parsedNode = DerNode.parse(rsaPrivateKeyDer.buf(), 0); var rsaPrivateKeyChildren = parsedNode.getChildren(); // Copy the parameters. RSAParameters parameters = new RSAParameters(); var modulus = getIntegerArrayWithoutLeadingZero(((DerNode)rsaPrivateKeyChildren[1]).getPayload()); parameters.Modulus = modulus; parameters.Exponent = getIntegerArrayWithoutLeadingZero(((DerNode)rsaPrivateKeyChildren[2]).getPayload()); // RSAParameters expects the integer array of the correct length. parameters.D = getIntegerArrayOfSize(((DerNode)rsaPrivateKeyChildren[3]).getPayload(), modulus.Length); parameters.P = getIntegerArrayOfSize(((DerNode)rsaPrivateKeyChildren[4]).getPayload(), modulus.Length / 2); parameters.Q = getIntegerArrayOfSize(((DerNode)rsaPrivateKeyChildren[5]).getPayload(), modulus.Length / 2); parameters.DP = getIntegerArrayOfSize(((DerNode)rsaPrivateKeyChildren[6]).getPayload(), modulus.Length / 2); parameters.DQ = getIntegerArrayOfSize(((DerNode)rsaPrivateKeyChildren[7]).getPayload(), modulus.Length / 2); parameters.InverseQ = getIntegerArrayOfSize(((DerNode)rsaPrivateKeyChildren[8]).getPayload(), modulus.Length / 2); return(new RsaSecurityPrivateKey(parameters)); } catch (DerDecodingException ex) { throw new net.named_data.jndn.util.InvalidKeySpecException ("RsaKeyFactory.generatePrivate error decoding the private key DER: " + ex); } }
generatePublic(KeySpec keySpec) { if (!(keySpec is X509EncodedKeySpec)) { throw new net.named_data.jndn.util.InvalidKeySpecException ("RsaKeyFactory.generatePublic expects a X509EncodedKeySpec"); } try { // Decode the X.509 public key. var parsedNode = DerNode.parse(new ByteBuffer(((X509EncodedKeySpec)keySpec).KeyDer), 0); var rootChildren = parsedNode.getChildren(); var algorithmIdChildren = DerNode.getSequence(rootChildren, 0).getChildren(); var oidString = ((DerNode.DerOid)algorithmIdChildren[0]).toVal().ToString(); var rsaPublicKeyDerBitString = ((DerNode)rootChildren[1]).getPayload(); if (oidString != RSA_ENCRYPTION_OID) { throw new net.named_data.jndn.util.InvalidKeySpecException ("The PKCS #8 private key is not RSA_ENCRYPTION"); } // Decode the PKCS #1 RSAPublicKey. // Skip the leading 0 byte in the DER BitString. parsedNode = DerNode.parse(rsaPublicKeyDerBitString.buf(), 1); var rsaPublicKeyChildren = parsedNode.getChildren(); // Copy the parameters. RSAParameters parameters = new RSAParameters(); parameters.Modulus = getIntegerArrayWithoutLeadingZero(((DerNode)rsaPublicKeyChildren[0]).getPayload()); parameters.Exponent = getIntegerArrayWithoutLeadingZero(((DerNode)rsaPublicKeyChildren[1]).getPayload()); return(new RsaSecurityPublicKey(parameters)); } catch (DerDecodingException ex) { throw new net.named_data.jndn.util.InvalidKeySpecException ("RsaKeyFactory.generatePublic error decoding the public key DER: " + ex); } }