Java Code Examples for sun.security.rsa.RSAKeyPairGenerator

Following code examples demonstrate how to use sun.security.rsa.RSAKeyPairGeneratorfrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use sun.security.rsa.RSAKeyPairGeneratorand various code implementation of this class.

	public static void main(String[] args) {
		RSAKeyPairGenerator rsa = new RSAKeyPairGenerator();
		System.out.println(rsa.generateKeyPair().getPrivate().getAlgorithm());
		byte[] b =rsa.generateKeyPair().getPrivate().getEncoded();
		byte[] a =rsa.generateKeyPair().getPublic().getEncoded();
		System.out.println(new BASE64Encoder().encode(b));
		System.out.println(new BASE64Encoder().encode(a));
	} 


    public static void main(String[] args) throws Exception {
        RSAKeyPairGenerator generator = new RSAKeyPairGenerator();

        KeyPair keyPair = generator.generateKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();

        
        Signature signature = Signature.getInstance(privateKey.getAlgorithm());
        signature.initSign(privateKey);
        signature.update("aulang".getBytes());
        byte[] sign = signature.sign();

        
        signature = Signature.getInstance(publicKey.getAlgorithm());
        signature.initVerify(publicKey);
        signature.update("aulang".getBytes());
        System.out.println(signature.verify(sign));

        
        X500Name issuer = new X500Name(CAROOT);
        X500Name subject = issuer;
        Calendar calendar = Calendar.getInstance();
        BigInteger serial = BigInteger.valueOf(System.currentTimeMillis());
        Date notBefore = calendar.getTime();
        calendar.add(Calendar.YEAR, 20);
        Date notAfter = calendar.getTime();

        JcaX509v3CertificateBuilder certBuilder = new JcaX509v3CertificateBuilder(issuer, serial, notBefore, notAfter,
                subject, publicKey);

        BasicConstraints basicConstraints = new BasicConstraints(false);
        KeyUsage keyUsage = new KeyUsage(KeyUsage.digitalSignature | KeyUsage.dataEncipherment);
        ExtendedKeyUsage extendedKeyUsage = new ExtendedKeyUsage(KeyPurposeId.id_kp_timeStamping);

        
        certBuilder.addExtension(Extension.basicConstraints, true, basicConstraints);
        certBuilder.addExtension(Extension.extendedKeyUsage, true, extendedKeyUsage);
        certBuilder.addExtension(Extension.keyUsage, true, keyUsage);

        JcaContentSignerBuilder signerBuilder = new JcaContentSignerBuilder("SHA256WITHRSA");
        X509CertificateHolder holder = certBuilder.build(signerBuilder.build(privateKey));

        JcaX509CertificateConverter converter = new JcaX509CertificateConverter();
        X509Certificate cert = converter.getCertificate(holder);

        
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null);

        keyStore.setKeyEntry("aulang", privateKey, "123456".toCharArray(), new Certificate[]{cert});

        FileOutputStream kfos = new FileOutputStream("E:/Document/Keystore/aulang.jks");
        keyStore.store(kfos, "123456".toCharArray());

        
        FileOutputStream cfos = new FileOutputStream("E:/Document/Keystore/aulang.cer");
        cfos.write(cert.getEncoded());
        cfos.close();
    } 

  public static void main(String[] args) {
    UserUtils userUtils=new UserUtils("3433434");
    System.out.println(userUtils.token(1L));
    System.out.println(userUtils.member(userUtils.token(1L)));
    Jwts.builder().signWith(SignatureAlgorithm.RS512, base64key);

    RSAKeyPairGenerator rsaKeyPairGenerator=new RSAKeyPairGenerator();
    rsaKeyPairGenerator.generateKeyPair();
  } 

    public static RSAKey createNewKey() {
        RSAKeyPairGenerator generator = new RSAKeyPairGenerator();
        KeyPair keyPair = generator.generateKeyPair();
        return new RSAKey(keyPair);
    } 

    public Keys() {

        RSAKeyPairGenerator rsaKeyPairGenerator = new RSAKeyPairGenerator();
        rsaKeyPairGenerator.initialize(512, new SecureRandom());
        KeyPair keyPair = rsaKeyPairGenerator.generateKeyPair();

        setPublicKey(keyPair.getPublic());
        setPrivateKey(keyPair.getPrivate());
    } 

    public static void main(String[] args) throws InterruptedException {

        RSAKeyPairGenerator keyGen = new RSAKeyPairGenerator();
        KeyPair keyPair = keyGen.generateKeyPair();
        final PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        final PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();




        ServerBootstrap bootstrap = new ServerBootstrap()
                .channel(NioServerSocketChannel.class)
                .group(new NioEventLoopGroup())
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new SecondClientHandler(publicKey, privateKey));
                    }
                });


    } 

    public static KeyPair generateKey(int bitcnt) {
        RSAKeyPairGenerator rsakpg = new RSAKeyPairGenerator();

        rsakpg.initialize(bitcnt, secureRandom);
        return rsakpg.generateKeyPair();
    } 

    public static void main(String[] args) {

        RSAKeyPairGenerator keyGen = new RSAKeyPairGenerator();
        KeyPair keyPair = keyGen.generateKeyPair();
        final PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        final PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

        Security.addProvider(new BouncyCastleProvider());


        ServerBootstrap bootstrap = new ServerBootstrap()
                .channel(NioServerSocketChannel.class)
                .group(new NioEventLoopGroup())
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new ServerHandler(publicKey, privateKey));
                    }
                });

        Channel channel = bootstrap.bind(new InetSocketAddress(11111)).channel();
    } 

Advertisement
Javadoc
RSA keypair generation. Standard algorithm, minimum key length 512 bit. We generate two random prime

s until we find two where phi is relative prime to the public exponent. Default exponent is 65537. It has only bit 0 and bit 4 set, which makes it particularly efficient. @since 1.5 @author Andreas Sterbenz

Read More
Advertisement