src/main/java/toy/crypto/PkiUtils.java
/**
     * Generates key pair using ECDSA with the secp256k1 curve.
     * @param outputPath the path where the signatures will be generated
     * @throws NoSuchProviderException
     * @throws NoSuchAlgorithmException
     * @throws InvalidAlgorithmParameterException
     * @throws IOException
     */
Generates key pair using ECDSA with the secp256k1 curve.
Generates key pair using ECDSA with the secp256k1 curve .
static public void generateKeyPair(String outputPath) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECDSA", "BC");
        keyPairGenerator.initialize(new ECGenParameterSpec("secp256k1"), new SecureRandom());
        java.security.KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        System.out.println("privateKey\n-----------");
        System.out.println(new String(Base64.getEncoder().encode(privateKey.getEncoded())));
        System.out.println("-----------");
        PublicKey publicKey = keyPair.getPublic();
        System.out.println("publicKey\n-----------");
        System.out.println(new String(Base64.getEncoder().encode(publicKey.getEncoded())));
        System.out.println("-----------");
        Files.createDirectories(Paths.get(outputPath));
        Path privPath = Paths.get(outputPath, "priv.key");
        Files.write(privPath, new String(Base64.getEncoder().encode(privateKey.getEncoded())).getBytes());
        Path pubPath = Paths.get(outputPath, "pub.key");
        Files.write(pubPath, new String(Base64.getEncoder().encode(publicKey.getEncoded())).getBytes());
    }
static public void generateKeyPair ( String outputPath ) throws NoSuchProviderException , NoSuchAlgorithmException , InvalidAlgorithmParameterException , IOException { KeyPairGenerator keyPairGenerator = KeyPairGenerator . getInstance ( "ECDSA" , "BC" ) ; keyPairGenerator . initialize ( new ECGenParameterSpec ( "secp256k1" ) , new SecureRandom ( ) ) ; java . security . KeyPair keyPair = keyPairGenerator . generateKeyPair ( ) ; PrivateKey privateKey = keyPair . getPrivate ( ) ; System . out . println ( "privateKey\n-----------" ) ; System . out . println ( new String ( Base64 . getEncoder ( ) . encode ( privateKey . getEncoded ( ) ) ) ) ; System . out . println ( "-----------" ) ; PublicKey publicKey = keyPair . getPublic ( ) ; System . out . println ( "publicKey\n-----------" ) ; System . out . println ( new String ( Base64 . getEncoder ( ) . encode ( publicKey . getEncoded ( ) ) ) ) ; System . out . println ( "-----------" ) ; Files . createDirectories ( Paths . get ( outputPath ) ) ; Path privPath = Paths . get ( outputPath , "priv.key" ) ; Files . write ( privPath , new String ( Base64 . getEncoder ( ) . encode ( privateKey . getEncoded ( ) ) ) . getBytes ( ) ) ; Path pubPath = Paths . get ( outputPath , "pub.key" ) ; Files . write ( pubPath , new String ( Base64 . getEncoder ( ) . encode ( publicKey . getEncoded ( ) ) ) . getBytes ( ) ) ; }
Generates key pair using ECDSA with the secp256k1 curve.
Generates key pair using ECDSA with the secp256k1 curve .