List of usage examples for java.security KeyPair getPrivate
public PrivateKey getPrivate()
From source file:com.vexsoftware.votifier.crypto.RSAIO.java
/** * Saves the key pair to the disk./* w w w .j av a 2 s . co m*/ * * @param directory * The directory to save to * @param keyPair * The key pair to save * @throws Exception * If an error occurs */ public static void save(File directory, KeyPair keyPair) throws Exception { PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // Store the public key. X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicKey.getEncoded()); FileOutputStream out = new FileOutputStream(directory + "/public.key"); out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded()).getBytes()); out.close(); // Store the private key. PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); out = new FileOutputStream(directory + "/private.key"); out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded()).getBytes()); out.close(); }
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:com.java.demo.RsaDemo.java
public static void Init() { try {/*w ww . ja v a 2s. co 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 {/* w ww .jav a 2 s . 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:com.floreantpos.license.FiveStarPOSKeyGenerator.java
public static void createKeys(String publicKeyUri, String privateKeyUri) throws NoSuchAlgorithmException, IOException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024, new SecureRandom()); KeyPair keyPair = keyGen.generateKeyPair(); IOUtils.write(keyPair.getPublic().getEncoded(), new FileOutputStream(publicKeyUri)); IOUtils.write(keyPair.getPrivate().getEncoded(), new FileOutputStream(privateKeyUri)); }
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 w ww .j ava 2s .c o 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:com.github.ibole.infrastructure.security.ECDSA.java
public static void jdkECDSA() { try {//from ww w.j a v a2s .c o 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:com.vexsoftware.votifier.util.rsa.RSAIO.java
/** * Saves the key pair to the disk./* w ww . jav a2 s. c om*/ * * @param directory * The directory to save to * @param keyPair * The key pair to save * @throws Exception * If an error occurs */ public static void save(File directory, KeyPair keyPair) throws Exception { PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // Store the public key. X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicKey.getEncoded()); FileOutputStream out = null; try { out = new FileOutputStream(directory + "/public.key"); out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded()).getBytes()); } finally { try { out.close(); } catch (Exception exception) { // ignore } } // Store the private key. PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); try { out = new FileOutputStream(directory + "/private.key"); out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded()).getBytes()); } finally { try { out.close(); } catch (Exception exception) { // ignore } } }
From source file:com.vmware.identity.rest.core.test.RequestSignerTest.java
@BeforeClass public static void setup() throws NoSuchAlgorithmException { KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048);/*from w w w . j a v a 2 s . co m*/ KeyPair pair = generator.generateKeyPair(); publicKey = pair.getPublic(); privateKey = pair.getPrivate(); }
From source file:com.aqnote.shared.cryptology.cert.util.KeyStoreUtil.java
public static KeyStore getPKCS12KeyStore(String alias, Certificate[] certChain, KeyPair keyPair, char[] passwd) throws Exception { PKCS12BagAttributeCarrier bagAttr = (PKCS12BagAttributeCarrier) keyPair.getPrivate(); bagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString(alias)); SubjectKeyIdentifier pubKeyId = new JcaX509ExtensionUtils().createSubjectKeyIdentifier(keyPair.getPublic()); bagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, pubKeyId); KeyStore store = KeyStore.getInstance(KEY_STORE_TYPE, JCE_PROVIDER); store.load(null, null);/*from www .ja va 2s . c o m*/ store.setKeyEntry(alias, keyPair.getPrivate(), passwd, certChain); return store; }