以下是使用 Java 生成 RSA 公钥和私钥的示例代码:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

public class KeyGenerator {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        // 生成 RSA 密钥对
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance('RSA');
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        // 获取公钥和私钥
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        // 输出公钥和私钥
        System.out.println('Public Key: ' + publicKey);
        System.out.println('Private Key: ' + privateKey);
    }
}

该代码使用了 Java 的 KeyPairGenerator 类来生成 RSA 密钥对,并使用 getPublic() 和 getPrivate() 方法获取公钥和私钥。在输出时,我们将公钥和私钥分别打印出来。需要注意的是,此代码将生成 2048 位的 RSA 密钥对。

Java 生成 RSA 公钥和私钥代码示例

原文地址: https://www.cveoy.top/t/topic/lMFy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录