List of usage examples for java.security KeyPair getPublic
public PublicKey getPublic()
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);/* w w w.ja va2 s . c o m*/ KeyPair keypair = keyGen.genKeyPair(); DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate(); DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic(); DSAParams dsaParams = privateKey.getParams(); BigInteger p = dsaParams.getP(); BigInteger q = dsaParams.getQ(); BigInteger g = dsaParams.getG(); BigInteger x = privateKey.getX(); BigInteger y = publicKey.getY(); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String password = "password"; KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024);//from w w w .j a v a 2 s .c o m KeyPair keyPair = keyPairGenerator.genKeyPair(); String publicKeyFilename = "public"; byte[] publicKeyBytes = keyPair.getPublic().getEncoded(); FileOutputStream fos = new FileOutputStream(publicKeyFilename); fos.write(publicKeyBytes); fos.close(); String privateKeyFilename = "privateKeyFilename"; byte[] privateKeyBytes = keyPair.getPrivate().getEncoded(); byte[] encryptedPrivateKeyBytes = passwordEncrypt(password.toCharArray(), privateKeyBytes); fos = new FileOutputStream(privateKeyFilename); fos.write(encryptedPrivateKeyBytes); fos.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);// w w w. j ava 2 s .co m KeyPair keypair = keyGen.genKeyPair(); DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate(); DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic(); DSAParams dsaParams = privateKey.getParams(); BigInteger p = dsaParams.getP(); BigInteger q = dsaParams.getQ(); BigInteger g = dsaParams.getG(); BigInteger x = privateKey.getX(); BigInteger y = publicKey.getY(); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g); PublicKey publicKey1 = keyFactory.generatePublic(publicKeySpec); }
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);/*from w w w. j a va 2 s .c o m*/ KeyPair keypair = keyGen.genKeyPair(); DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate(); DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic(); DSAParams dsaParams = privateKey.getParams(); BigInteger p = dsaParams.getP(); BigInteger q = dsaParams.getQ(); BigInteger g = dsaParams.getG(); BigInteger x = privateKey.getX(); BigInteger y = publicKey.getY(); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g); PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);// ww w. ja v a 2s. c o m Key blowfishKey = keyGenerator.generateKey(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); KeyPair keyPair = keyPairGenerator.genKeyPair(); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic()); byte[] blowfishKeyBytes = blowfishKey.getEncoded(); System.out.println(new String(blowfishKeyBytes)); byte[] cipherText = cipher.doFinal(blowfishKeyBytes); System.out.println(new String(cipherText)); cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate()); byte[] decryptedKeyBytes = cipher.doFinal(cipherText); System.out.println(new String(decryptedKeyBytes)); SecretKey newBlowfishKey = new SecretKeySpec(decryptedKeyBytes, "Blowfish"); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); DHParameterSpec dhParams = new DHParameterSpec(p512, g512); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH", "BC"); keyGen.initialize(dhParams, new SecureRandom()); KeyAgreement aKeyAgree = KeyAgreement.getInstance("DH", "BC"); KeyPair aPair = keyGen.generateKeyPair(); KeyAgreement bKeyAgree = KeyAgreement.getInstance("DH", "BC"); KeyPair bPair = keyGen.generateKeyPair(); aKeyAgree.init(aPair.getPrivate());/* w w w. ja va2 s. co m*/ bKeyAgree.init(bPair.getPrivate()); aKeyAgree.doPhase(bPair.getPublic(), true); bKeyAgree.doPhase(aPair.getPublic(), true); MessageDigest hash = MessageDigest.getInstance("SHA1", "BC"); System.out.println(new String(hash.digest(aKeyAgree.generateSecret()))); System.out.println(new String(hash.digest(bKeyAgree.generateSecret()))); }
From source file:Main.java
public static void main(String[] args) throws Exception { //Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); DHParameterSpec dhParams = new DHParameterSpec(p512, g512); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH", "BC"); keyGen.initialize(dhParams, new SecureRandom()); KeyAgreement aKeyAgree = KeyAgreement.getInstance("DH", "BC"); KeyPair aPair = keyGen.generateKeyPair(); KeyAgreement bKeyAgree = KeyAgreement.getInstance("DH", "BC"); KeyPair bPair = keyGen.generateKeyPair(); aKeyAgree.init(aPair.getPrivate());// w w w .j av a 2 s . c o m bKeyAgree.init(bPair.getPrivate()); aKeyAgree.doPhase(bPair.getPublic(), true); bKeyAgree.doPhase(aPair.getPublic(), true); MessageDigest hash = MessageDigest.getInstance("SHA1", "BC"); System.out.println(new String(hash.digest(aKeyAgree.generateSecret()))); System.out.println(new String(hash.digest(bKeyAgree.generateSecret()))); }
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);/*from w w w .j ava 2 s .com*/ KeyPair keypair = keyGen.genKeyPair(); DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate(); DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic(); DSAParams dsaParams = privateKey.getParams(); BigInteger p = dsaParams.getP(); BigInteger q = dsaParams.getQ(); BigInteger g = dsaParams.getG(); BigInteger x = privateKey.getX(); BigInteger y = publicKey.getY(); // Create the DSA key factory KeyFactory keyFactory = KeyFactory.getInstance("DSA"); // Create the DSA private key KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g); PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec); byte[] buffer = new byte[1024]; Signature sig = Signature.getInstance(privateKey1.getAlgorithm()); sig.initSign(privateKey1); sig.update(buffer, 0, buffer.length); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { int eValue = 79; int bitLength = 1024; // KeySize BigInteger e = e = new BigInteger(Integer.toString(eValue)); KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(bitLength);//from ww w. j av a2 s.c o m KeyPair kp = kpg.generateKeyPair(); KeyFactory kfactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign"); e = new BigInteger(Integer.toString(eValue)); System.out.println("e =" + e); RSAKeyGenParameterSpec param = new RSAKeyGenParameterSpec(bitLength, e); kpg.initialize(param); kp = kpg.generateKeyPair(); kfactory = KeyFactory.getInstance("RSA", "SunRsaSign"); kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); }
From source file:com.emc.ecs.s3.sample.ECSS3Factory.java
public static void main(String[] args) { try {/* ww w. j a v a 2 s . c o m*/ KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA"); keyGenerator.initialize(1024, new SecureRandom()); KeyPair myKeyPair = keyGenerator.generateKeyPair(); // Serialize. byte[] pubKeyBytes = myKeyPair.getPublic().getEncoded(); byte[] privKeyBytes = myKeyPair.getPrivate().getEncoded(); String pubKeyStr = new String(Base64.encodeBase64(pubKeyBytes, false), "US-ASCII"); String privKeyStr = new String(Base64.encodeBase64(privKeyBytes, false), "US-ASCII"); System.out.println("Public Key: " + pubKeyStr); System.out.println("Private Key: " + privKeyStr); } catch (Exception e) { e.printStackTrace(); } }