Java Code Examples for java.security.interfaces.RSAPublicKey#getEncoded()

The following examples show how to use java.security.interfaces.RSAPublicKey#getEncoded() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SslConfigurer.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
private X509Certificate createCert(KeyPair keyPair, String signatureAlgoritm, String domainName)
  throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, OperatorCreationException, CertificateException, IOException {
  
  RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
  RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
  
  AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find(signatureAlgoritm);
  AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
  BcContentSignerBuilder sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId);
  
  ASN1InputStream publicKeyStream = new ASN1InputStream(rsaPublicKey.getEncoded());
  SubjectPublicKeyInfo pubKey = SubjectPublicKeyInfo.getInstance(publicKeyStream.readObject());
  publicKeyStream.close();
  
  X509v3CertificateBuilder v3CertBuilder = new X509v3CertificateBuilder(
      new X500Name("CN=" + domainName + ", OU=None, O=None L=None, C=None"),
      BigInteger.valueOf(Math.abs(new SecureRandom().nextInt())),
      new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30),
      new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365*10)),
      new X500Name("CN=" + domainName + ", OU=None, O=None L=None, C=None"),
      pubKey);
  
  RSAKeyParameters keyParams = new RSAKeyParameters(true, rsaPrivateKey.getPrivateExponent(), rsaPrivateKey.getModulus());
  ContentSigner contentSigner = sigGen.build(keyParams);
  
  X509CertificateHolder certificateHolder = v3CertBuilder.build(contentSigner);
  
  JcaX509CertificateConverter certConverter = new JcaX509CertificateConverter().setProvider("BC");
  return certConverter.getCertificate(certificateHolder);
}
 
Example 2
Source File: PublicKeySubstitution.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public PublicKeyProxy serialize(RSAPublicKey obj) {
    byte[] encoded = obj.getEncoded();
    PublicKeyProxy proxy = new PublicKeyProxy();
    proxy.setContent(encoded);
    return proxy;
}
 
Example 3
Source File: RSAEncryptCoder.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public RSAEncryptCoder(int size, boolean generatedKeyPair){
	this.size = size;
	this.encryptSize = size/8-11;
	this.dencryptSize = size/8;
	
	if(generatedKeyPair){
		KeyPair kp = generatedKey();
		RSAPublicKey pubkey = (RSAPublicKey)kp.getPublic();
		publicKey = pubkey.getEncoded();
		
		RSAPrivateKey prikey = (RSAPrivateKey)kp.getPrivate();
		privateKey = prikey.getEncoded();
	}
}
 
Example 4
Source File: RSATest.java    From java_security with MIT License 5 votes vote down vote up
/**
 * 
 * @author timliu
 * 说明: 用java的jdk里面相关方法实现rsa的签名及签名验证
 */
public static void jdkRSA()
{
	try {
		// 1.初始化密钥
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		keyPairGenerator.initialize(512);
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
		RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
		
		// 2.进行签名
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
		Signature signature = Signature.getInstance("MD5withRSA");
		signature.initSign(privateKey);
		signature.update(src.getBytes());
		byte[] result = signature.sign();
		System.out.println("jdk rsa sign:" + Hex.encodeHexString(result) );
		
		// 3.验证签名
		X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
		keyFactory = KeyFactory.getInstance("RSA");
		PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
		signature = Signature.getInstance("MD5withRSA");
		signature.initVerify(publicKey);
		signature.update(src.getBytes());
		boolean bool = signature.verify(result);
		System.out.println("jdk rsa verify:" + bool);
	} catch (Exception e) {
		System.out.println(e.toString());
	}
	
}
 
Example 5
Source File: RSATest.java    From java_security with MIT License 4 votes vote down vote up
public static void jdkRSA()
{		
	try 
	{
		// 1.初始化发送方密钥
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		keyPairGenerator.initialize(512);
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
		RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
		System.out.println("Public Key:" + Base64.encodeBase64String(rsaPublicKey.getEncoded()));
		System.out.println("Private Key:" + Base64.encodeBase64String(rsaPrivateKey.getEncoded()));
		
		// 2.私钥加密、公钥解密 ---- 加密
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.ENCRYPT_MODE, privateKey);
		byte[] result = cipher.doFinal(src.getBytes());
		System.out.println("私钥加密、公钥解密 ---- 加密:" + Base64.encodeBase64String(result));
		
		// 3.私钥加密、公钥解密 ---- 解密
		X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
		keyFactory = KeyFactory.getInstance("RSA");
		PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
		cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, publicKey);
		result = cipher.doFinal(result);
		System.out.println("私钥加密、公钥解密 ---- 解密:" + new String(result));
		
		
		
		// 4.公钥加密、私钥解密 ---- 加密
		X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
		KeyFactory keyFactory2 = KeyFactory.getInstance("RSA");
		PublicKey publicKey2 = keyFactory2.generatePublic(x509EncodedKeySpec2);
		Cipher cipher2 = Cipher.getInstance("RSA");
		cipher2.init(Cipher.ENCRYPT_MODE, publicKey2);
		byte[] result2 = cipher2.doFinal(src.getBytes());
		System.out.println("公钥加密、私钥解密 ---- 加密:" + Base64.encodeBase64String(result2));
		
		// 5.私钥解密、公钥加密 ---- 解密
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
		KeyFactory keyFactory5 = KeyFactory.getInstance("RSA");
		PrivateKey privateKey5 = keyFactory5.generatePrivate(pkcs8EncodedKeySpec5);
		Cipher cipher5 = Cipher.getInstance("RSA");
		cipher5.init(Cipher.DECRYPT_MODE, privateKey5);
		byte[] result5 = cipher5.doFinal(result2);
		System.out.println("公钥加密、私钥解密 ---- 解密:" + new String(result5));
		
	} catch (Exception e) {
		
		e.printStackTrace();
	}
	
}