private static PublicKey toDSAPublicKey(KEYBase r) // throws IOException, GeneralSecurityException, MalformedKeyException { DNSInput @in = new DNSInput(r.getKey()); int t = @in.readU8(); if (t > 8) { throw new MalformedKeyException(r); } Org.BouncyCastle.Math.BigInteger q = Helpers.readBigInteger(@in, 20); Org.BouncyCastle.Math.BigInteger p = Helpers.readBigInteger(@in, 64 + t * 8); Org.BouncyCastle.Math.BigInteger g = Helpers.readBigInteger(@in, 64 + t * 8); Org.BouncyCastle.Math.BigInteger y = Helpers.readBigInteger(@in, 64 + t * 8); /* * KeyFactory factory = KeyFactory.getInstance("DSA"); * return factory.generatePublic(new DSAPublicKeySpec(y, p, q, g)); */ Org.BouncyCastle.Crypto.Parameters.DsaParameters para = new Org.BouncyCastle.Crypto.Parameters.DsaParameters(p, q, g); Org.BouncyCastle.Crypto.Parameters.DsaPublicKeyParameters dp = new Org.BouncyCastle.Crypto.Parameters.DsaPublicKeyParameters(y, para); return(PublicKey.CreateInstance(dp)); }
/* * // https://csharp.hotexamples.com/examples/Org.BouncyCastle.Crypto.Parameters/ECPrivateKeyParameters/-/php-ecprivatekeyparameters-class-examples.html * public ECDiffieHellmanBc(Int32 keySize) * { * Org.BouncyCastle.Asn1.X9.X9ECParameters ecParams; * switch (keySize) { * case 256: * ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256r1"); * break; * case 384: * ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp384r1"); * break; * case 521: * ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp521r1"); * break; * default: */ // https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml // https://tools.ietf.org/html/rfc5933 private static PublicKey toECGOSTPublicKey(KEYBase r, ECKeyInfo keyinfo) // throws IOException, GeneralSecurityException { DNSInput @in = new DNSInput(r.getKey()); Org.BouncyCastle.Math.BigInteger x = Helpers.readBigIntegerLittleEndian(@in, keyinfo.length); Org.BouncyCastle.Math.BigInteger y = Helpers.readBigIntegerLittleEndian(@in, keyinfo.length); // OID to be found in Org.BouncyCastle.Security.GeneratorUtilities.GetKeyPairGenerator("ECGOST3410"); // Org.BouncyCastle.Crypto.Parameters.ECDomainParameters domain = Org.BouncyCastle.Asn1.CryptoPro.ECGost3410NamedCurves.GetByOid(Org.BouncyCastle.Asn1.CryptoPro.CryptoProObjectIdentifiers.GostR3410x94CryptoProA); // Org.BouncyCastle.Math.EC.ECCurve c = domain.Curve; // Org.BouncyCastle.Math.EC.ECPoint q = new Org.BouncyCastle.Math.EC.FpPoint(c, c.FromBigInteger(x), c.FromBigInteger(y)); // Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain); Org.BouncyCastle.Math.EC.ECPoint q = keyinfo.curve.CreatePoint(x, y); Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, keyinfo.spec); // Org.BouncyCastle.Crypto.Signers.ECGost3410Signer // Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(new Org.BouncyCastle.Asn1.X509.SubjectPublicKeyInfo()) /* * ECPoint q = new ECPoint(x, y); * * KeyFactory factory = KeyFactory.getInstance("ECGOST3410"); * return factory.generatePublic(new ECPublicKeySpec(q, keyinfo.spec)); */ return(PublicKey.CreateInstance(publicParams)); }
private static PublicKey toECDSAPublicKey(KEYBase r, ECKeyInfo keyinfo) // throws IOException, GeneralSecurityException { DNSInput @in = new DNSInput(r.getKey()); // RFC 6605 Section 4 Org.BouncyCastle.Math.BigInteger x = Helpers.readBigInteger(@in, keyinfo.length); Org.BouncyCastle.Math.BigInteger y = Helpers.readBigInteger(@in, keyinfo.length); // Org.BouncyCastle.Asn1.X9.X9ECParameters ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp521r1"); // Org.BouncyCastle.Asn1.X9.X9ECParameters ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256r1"); // Org.BouncyCastle.Asn1.X9.X9ECParameters p = Org.BouncyCastle.Asn1.X9.X962NamedCurves.GetByOid(Org.BouncyCastle.Asn1.X9.X9ObjectIdentifiers.Prime239v1); // Org.BouncyCastle.Asn1.X9.X9ECParameters p = Org.BouncyCastle.Asn1.X9.X962NamedCurves.GetByOid(Org.BouncyCastle.Asn1.X9.X9ObjectIdentifiers.ECDsaWithSha512); // Org.BouncyCastle.Asn1.X9.X9ECParameters ecP = Org.BouncyCastle.Asn1.Nist.NistNamedCurves.GetByName("curveName"); // Org.BouncyCastle.Math.EC.FpCurve c = (Org.BouncyCastle.Math.EC.FpCurve)ecP.Curve; // Org.BouncyCastle.Asn1.X9.X9ECParameters p = Org.BouncyCastle.Asn1.X9.X962NamedCurves.GetByOid(Org.BouncyCastle.Asn1.X9.X9ObjectIdentifiers.ECDsaWithSha512); // Org.BouncyCastle.Math.EC.ECCurve c = p.Curve; // Org.BouncyCastle.Crypto.Parameters.ECDomainParameters domain = new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(c, p.G, p.N, p.H); // Org.BouncyCastle.Math.EC.ECPoint q = new Org.BouncyCastle.Math.EC.FpPoint(c, c.FromBigInteger(x), c.FromBigInteger(y)); // Org.BouncyCastle.Math.EC.ECPoint q = c.CreatePoint(x, y); // Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain); Org.BouncyCastle.Math.EC.ECPoint q = keyinfo.curve.CreatePoint(x, y); Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, keyinfo.spec); // Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(Org.BouncyCastle.X509.SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicParams)); // Org.BouncyCastle.Security.PrivateKeyFactory.CreateKey(publicParams); // Org.BouncyCastle.Security.PrivateKeyFactory // Org.BouncyCastle.Security.PublicKeyFactory // Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(new Org.BouncyCastle.Asn1.X509.SubjectPublicKeyInfo()) /* * ECPoint q = new ECPoint(x, y); * * KeyFactory factory = KeyFactory.getInstance("EC"); * return factory.generatePublic(new ECPublicKeySpec(q, keyinfo.spec)); */ return(PublicKey.CreateInstance(publicParams)); }
private static PublicKey toRSAPublicKey(KEYBase r) // throws IOException, GeneralSecurityException { DNSInput @in = new DNSInput(r.getKey()); int exponentLength = @in.readU8(); if (exponentLength == 0) { exponentLength = @in.readU16(); } Org.BouncyCastle.Math.BigInteger exponent = Helpers.readBigInteger(@in, exponentLength); Org.BouncyCastle.Math.BigInteger modulus = Helpers.readBigInteger(@in); /* * KeyFactory factory = KeyFactory.getInstance("RSA"); * return factory.generatePublic(new RSAPublicKeySpec(modulus, exponent)); */ Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters kp = new Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters(false, modulus, exponent); return(PublicKey.CreateInstance(kp)); }