Java 如何从该方法中仅获取公钥的值

Java 如何从该方法中仅获取公钥的值,java,Java,密钥生成方法 public static String generateKeys(String keyAlgorithm, int numBits) { try { // Get the public/private key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyAlgorithm); keyGen.initialize(numBits);

密钥生成方法

public static String generateKeys(String keyAlgorithm, int numBits) {

    try {
        // Get the public/private key pair
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyAlgorithm);
        keyGen.initialize(numBits);
        KeyPair keyPair = keyGen.genKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();

         System.out.println("\n" + "Generating key/value pair using " + privateKey.getAlgorithm() + " algorithm");

        // Get the bytes of the public and private keys
        byte[] privateKeyBytes = privateKey.getEncoded();
        byte[] publicKeyBytes = publicKey.getEncoded();

        // Get the formats of the encoded bytes
        String formatPrivate = privateKey.getFormat(); // PKCS#8
        String formatPublic = publicKey.getFormat(); // X.509

        String pv=String.valueOf(privateKeyBytes);

        System.out.println("Private Key : " + Base64.encode(pv));

        String pb=String.valueOf(publicKeyBytes);
        System.out.println("Public Key : " + Base64.encode(pb));
           //  return pb;

            // System.out.println("Private Key : " + Base64.encode(String.valueOf(privateKeyBytes)));
            // System.out.println("Public Key : " + Base64.encode(String.valueOf(publicKeyBytes)));

        // The bytes can be converted back to public and private key objects
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
        EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec);

        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
        PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec);

        // The original and new keys are the same
            // System.out.println("  Are both private keys equal? " + privateKey.equals(privateKey2));
            // System.out.println("  Are both public keys equal? " + publicKey.equals(publicKey2));
    } catch (InvalidKeySpecException specException) {
        System.out.println("Exception");
        System.out.println("Invalid Key Spec Exception");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception");
        System.out.println("No such algorithm: " + keyAlgorithm);
    }
        return null;
    }
}
这是我的密钥生成代码 我只想从上述方法中获取公钥的值,并使用序列化将其发送到服务器

public static String generateKeys(String keyAlgorithm, int numBits) {

    try {
        // Get the public/private key pair
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyAlgorithm);
        keyGen.initialize(numBits);
        KeyPair keyPair = keyGen.genKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();

         System.out.println("\n" + "Generating key/value pair using " + privateKey.getAlgorithm() + " algorithm");

        // Get the bytes of the public and private keys
        byte[] privateKeyBytes = privateKey.getEncoded();
        byte[] publicKeyBytes = publicKey.getEncoded();

        // Get the formats of the encoded bytes
        String formatPrivate = privateKey.getFormat(); // PKCS#8
        String formatPublic = publicKey.getFormat(); // X.509

        String pv=String.valueOf(privateKeyBytes);

        System.out.println("Private Key : " + Base64.encode(pv));

        String pb=String.valueOf(publicKeyBytes);
        System.out.println("Public Key : " + Base64.encode(pb));
           //  return pb;

            // System.out.println("Private Key : " + Base64.encode(String.valueOf(privateKeyBytes)));
            // System.out.println("Public Key : " + Base64.encode(String.valueOf(publicKeyBytes)));

        // The bytes can be converted back to public and private key objects
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
        EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec);

        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
        PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec);

        // The original and new keys are the same
            // System.out.println("  Are both private keys equal? " + privateKey.equals(privateKey2));
            // System.out.println("  Are both public keys equal? " + publicKey.equals(publicKey2));
    } catch (InvalidKeySpecException specException) {
        System.out.println("Exception");
        System.out.println("Invalid Key Spec Exception");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception");
        System.out.println("No such algorithm: " + keyAlgorithm);
    }
        return null;
    }
}
 String pb=String.valueOf(publicKeyBytes);
 System.out.println("Public Key : " + Base64.encode(pb));
但是我的方法显示为null
如何通过密钥存储库或任何其他方法将公钥/私钥存储在文件中

您应该能够通过运行以下命令获取公钥字符串:

public static String generateKeys(String keyAlgorithm, int numBits) {

    try {
        // Get the public/private key pair
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyAlgorithm);
        keyGen.initialize(numBits);
        KeyPair keyPair = keyGen.genKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();

         System.out.println("\n" + "Generating key/value pair using " + privateKey.getAlgorithm() + " algorithm");

        // Get the bytes of the public and private keys
        byte[] privateKeyBytes = privateKey.getEncoded();
        byte[] publicKeyBytes = publicKey.getEncoded();

        // Get the formats of the encoded bytes
        String formatPrivate = privateKey.getFormat(); // PKCS#8
        String formatPublic = publicKey.getFormat(); // X.509

        String pv=String.valueOf(privateKeyBytes);

        System.out.println("Private Key : " + Base64.encode(pv));

        String pb=String.valueOf(publicKeyBytes);
        System.out.println("Public Key : " + Base64.encode(pb));
           //  return pb;

            // System.out.println("Private Key : " + Base64.encode(String.valueOf(privateKeyBytes)));
            // System.out.println("Public Key : " + Base64.encode(String.valueOf(publicKeyBytes)));

        // The bytes can be converted back to public and private key objects
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
        EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec);

        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
        PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec);

        // The original and new keys are the same
            // System.out.println("  Are both private keys equal? " + privateKey.equals(privateKey2));
            // System.out.println("  Are both public keys equal? " + publicKey.equals(publicKey2));
    } catch (InvalidKeySpecException specException) {
        System.out.println("Exception");
        System.out.println("Invalid Key Spec Exception");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception");
        System.out.println("No such algorithm: " + keyAlgorithm);
    }
        return null;
    }
}

System.out.println(新字符串(java.util.Base64.getEncoder().encode(publicKeyBytes))

你必须在你的标签上更加明确。这是关于密码学的吗?请更具体一点。这只是关于如何将其放入文件的问题吗?那么纯粹的IO问题?或者还有更多吗?我不明白你想做什么,但是你的
generateKeys()
方法总是返回null。
public static String generateKeys(String keyAlgorithm, int numBits) {

    try {
        // Get the public/private key pair
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyAlgorithm);
        keyGen.initialize(numBits);
        KeyPair keyPair = keyGen.genKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();

         System.out.println("\n" + "Generating key/value pair using " + privateKey.getAlgorithm() + " algorithm");

        // Get the bytes of the public and private keys
        byte[] privateKeyBytes = privateKey.getEncoded();
        byte[] publicKeyBytes = publicKey.getEncoded();

        // Get the formats of the encoded bytes
        String formatPrivate = privateKey.getFormat(); // PKCS#8
        String formatPublic = publicKey.getFormat(); // X.509

        String pv=String.valueOf(privateKeyBytes);

        System.out.println("Private Key : " + Base64.encode(pv));

        String pb=String.valueOf(publicKeyBytes);
        System.out.println("Public Key : " + Base64.encode(pb));
           //  return pb;

            // System.out.println("Private Key : " + Base64.encode(String.valueOf(privateKeyBytes)));
            // System.out.println("Public Key : " + Base64.encode(String.valueOf(publicKeyBytes)));

        // The bytes can be converted back to public and private key objects
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
        EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec);

        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
        PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec);

        // The original and new keys are the same
            // System.out.println("  Are both private keys equal? " + privateKey.equals(privateKey2));
            // System.out.println("  Are both public keys equal? " + publicKey.equals(publicKey2));
    } catch (InvalidKeySpecException specException) {
        System.out.println("Exception");
        System.out.println("Invalid Key Spec Exception");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception");
        System.out.println("No such algorithm: " + keyAlgorithm);
    }
        return null;
    }
}