Reading elliptic curve private key from file with BouncyCastle

前端 未结 3 1171
陌清茗
陌清茗 2020-11-30 12:07

The BouncyCastle cryptography APIs allow for creating and verifying digital signatures using the regular java.security package objects, such as java.secur

相关标签:
3条回答
  • 2020-11-30 12:31

    In addition to the standard JCE approach shown by divanov as long as you give it the correct input (see my comment thereto), or just using JCE in the first place like your selfanswer, BouncyCastle 1.48 up DOES still contain the old PEMReader functionality just organized a bit differently and for this case you can use something like:

    static void SO22963581BCPEMPrivateEC () throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        Reader rdr = new StringReader ("-----BEGIN EC PRIVATE KEY-----\n"
                +"MHQCAQEEIDzESrZFmTaOozu2NyiS8LMZGqkHfpSOoI/qA9Lw+d4NoAcGBSuBBAAK\n"
                +"oUQDQgAE7kIqoSQzC/UUXdFdQ9Xvu1Lri7pFfd7xDbQWhSqHaDtj+XY36Z1Cznun\n"
                +"GDxlA0AavdVDuoGXxNQPIed3FxPE3Q==\n"+"-----END EC PRIVATE KEY-----\n");
        Object parsed = new org.bouncycastle.openssl.PEMParser(rdr).readObject();
        KeyPair pair = new org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter().getKeyPair((org.bouncycastle.openssl.PEMKeyPair)parsed);
        System.out.println (pair.getPrivate().getAlgorithm());
    }
    
    0 讨论(0)
  • 2020-11-30 12:37

    Since I only need this for a quick and dirty demo, I solved it in the following way (in Scala). First, I generate a public private key pair in the REPL and print out its data:

    Security.addProvider(new BouncyCastleProvider)
    
    val SignatureScheme = "some signature scheme, eg ECDSA"
    val RandomAlgorithm = "some random algorithm, eg SHA1PRNG"
    
    val keygen = KeyPairGenerator.getInstance(SignatureScheme)
    val rng = SecureRandom.getInstance(RandomAlgorithm)
    rng.setSeed(seed)
    keygen.initialize(KeySize, rng)
    
    val kp = keygen.generateKeyPair()
    println(kp.getPublic.getEncoded.toSeq) // toSeq so that Scala actually prints it
    println(kp.getPrivate.getEncoded.toSeq)
    

    Then using the generated data,

    val hardcodedPublic = Array[Byte]( /* data */ )
    val hardcodedPrivate = Array[Byte]( /* data */ )
    
    val factory = KeyFactory.getInstance(SignatureScheme)
    
    val publicSpec = new X509EncodedKeySpec(hardcodedPublic)
    val publicKey = factory.generatePublic(publicSpec)
    
    val privateSpec = new PKCS8EncodedKeySpec(hardcodedPrivate)
    val privateKey = factory.generatePrivate(privateSpec)
    

    The key thing you need to know here is that by default public key data uses X509 encoding and private key data uses PKCS8 encoding. It should be possible to get OpenSSL to output these formats and parse them manually, but I did not check how.

    I used information from this blog post about SpongyCastle (which is Android's BouncyCastle alias) quite helpful. It is unfortunate that documentation is fragmented like this, and that BouncyCastle's wiki was down at the time of this question.

    Update: the BouncyCastle wiki is up, and you can find the documentation here.

    0 讨论(0)
  • 2020-11-30 12:39

    In Java this will be pretty much the same code. After striping guarding strings away and decoding Base64 data give it to this utility method:

    public static PrivateKey keyToValue(byte[] pkcs8key)
        throws GeneralSecurityException {
    
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(pkcs8key);
        KeyFactory factory = KeyFactory.getInstance("ECDSA");
        PrivateKey privateKey = factory.generatePrivate(spec);
        return privateKey;
    }
    
    0 讨论(0)
提交回复
热议问题