List of usage examples for java.security KeyPair getPublic
public PublicKey getPublic()
From source file:com.java.demo.RsaDemo.java
public static void Init() { try {/*from w w w . ja v a 2s .c o m*/ KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(512); KeyPair kp = keyPairGenerator.generateKeyPair(); rSAPublicKey = (RSAPublicKey) kp.getPublic(); rSAPrivateKey = (RSAPrivateKey) kp.getPrivate(); } catch (Exception ex) { System.out.println("com.Java.Demo.RsaDemo.Init()" + ex.getMessage()); } }
From source file:love.sola.netsupport.util.RSAUtil.java
public static void genKeyPair() { try {//from w w w. jav a 2s .c o m KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024); KeyPair kp = kpg.genKeyPair(); publicKey = kp.getPublic(); privateKey = kp.getPrivate(); publicKey_s = Base64.encodeBase64String(publicKey.getEncoded()); privateKey_s = Base64.encodeBase64String(privateKey.getEncoded()); } catch (Exception e) { e.printStackTrace(); } }
From source file:MainClass.java
public static PKCS10CertificationRequest generateRequest(KeyPair pair) throws Exception { return new PKCS10CertificationRequest("SHA256withRSA", new X500Principal("CN=Requested Test Certificate"), pair.getPublic(), null, pair.getPrivate()); }
From source file:MainClass.java
static byte[] performSigning(String s, String alg, KeyPair keyPair) throws Exception { Signature sign = Signature.getInstance(alg); PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); sign.initSign(privateKey);//w w w. j a va 2 s . c o m sign.update(s.getBytes()); return sign.sign(); }
From source file:org.cloudfoundry.identity.uaa.oauth.jwk.JsonWebKeyHelper.java
public static JsonWebKey fromPEMPrivateKey(String key) { KeyPair pair = KeyInfo.parseKeyPair(key); RSAPublicKey rsaKey = (RSAPublicKey) pair.getPublic(); BigInteger modulus = rsaKey.getModulus(); BigInteger exponent = rsaKey.getPublicExponent(); Map<String, Object> properties = new HashMap(); properties.put("n", base64.encodeAsString(modulus.toByteArray())); properties.put("e", base64.encodeAsString(exponent.toByteArray())); properties.put("kty", "RSA"); properties.put("use", sig.name()); properties.put("value", KeyInfo.pemEncodePublicKey(rsaKey)); return new JsonWebKey(properties); }
From source file:com.vimukti.accounter.developer.api.PublicKeyGenerator.java
private static void generate() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, KeyStoreException, CertificateException, IOException, URISyntaxException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed("VimTech".getBytes("UTF-8")); keyGen.initialize(1024, random);//from w w w.j av a 2s. com KeyPair pair = keyGen.generateKeyPair(); PrivateKey priv = pair.getPrivate(); PublicKey pub = pair.getPublic(); System.out.println(priv); System.out.println(pub); byte[] encoded = pub.getEncoded(); byte[] encodeBase64 = Base64.encodeBase64(encoded); System.out.println("Public Key:" + new String(encodeBase64)); byte[] encodedPrv = priv.getEncoded(); byte[] encodeBase64Prv = Base64.encodeBase64(encodedPrv); System.out.println("Private Key:" + new String(encodeBase64Prv)); byte[] decodeBase64 = Base64.decodeBase64(encodeBase64); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(decodeBase64); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); System.out.println(keyFactory.generatePublic(pubKeySpec).equals(pub)); }
From source file:com.github.ibole.infrastructure.security.ECDSA.java
public static void jdkECDSA() { try {/* w w w . j a v a2 s . co m*/ // 1.? KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC"); keyPairGenerator.initialize(256); KeyPair keyPair = keyPairGenerator.generateKeyPair(); ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic(); ECPrivateKey ecPrivateKey = (ECPrivateKey) keyPair.getPrivate(); // 2.?? PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("EC"); PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Signature signature = Signature.getInstance("SHA256withECDSA"); signature.initSign(privateKey); signature.update(src.getBytes()); byte[] res = signature.sign(); System.out.println("??" + Base64.encodeBase64String(res)); // 3.??? X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded()); keyFactory = KeyFactory.getInstance("EC"); PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); signature = Signature.getInstance("SHA256withECDSA"); signature.initVerify(publicKey); signature.update(src.getBytes()); boolean bool = signature.verify(res); System.out.println("?" + bool); } catch (Exception e) { e.printStackTrace(); } }
From source file:MainClass.java
public static void createKey() throws Exception { KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman"); kpg.initialize(512);//from w w w. j a v a2s. c o m KeyPair kp = kpg.generateKeyPair(); KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman"); DHPublicKeySpec kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), DHPublicKeySpec.class); }
From source file:com.aqnote.shared.cryptology.asymmetric.dsa.DSAKeyPairGenTest.java
public static void generator() throws NoSuchAlgorithmException, FileNotFoundException, IOException { KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM); // ???/*from ww w.j a va 2 s . co m*/ SecureRandom secrand = new SecureRandom(); secrand.setSeed(seed); // ?? // ??, ??keysize ?. 512 1024 64 ? keygen.initialize(512, secrand); // ?pubkey?prikey KeyPair keys = keygen.generateKeyPair(); // ? PublicKey pubkey = keys.getPublic(); PrivateKey prikey = keys.getPrivate(); byte[] pubkeyByteArray = Base64.encodeBase64(pubkey.getEncoded()); OutputStream os = new FileOutputStream(new File(PUBKEY_FILE_NAME)); ByteArrayInputStream bais = new ByteArrayInputStream(pubkeyByteArray); StreamUtil.io(bais, os); bais.close(); os.close(); byte[] prikeyByteArray = Base64.encodeBase64(prikey.getEncoded()); os = new FileOutputStream(new File(PRIKEY_FILE_NAME)); bais = new ByteArrayInputStream(prikeyByteArray); StreamUtil.io(bais, os); bais.close(); os.close(); }
From source file:MainClass.java
public static void createSpecificKey(BigInteger p, BigInteger g) throws Exception { KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman"); DHParameterSpec param = new DHParameterSpec(p, g); kpg.initialize(param);//from www. j av a2 s. c o m KeyPair kp = kpg.generateKeyPair(); KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman"); DHPublicKeySpec kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), DHPublicKeySpec.class); }