List of usage examples for java.security KeyPairGenerator getInstance
public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
public static void main(String[] argv) throws Exception { String algorithm = "DSA"; // or RSA, DH, etc. // Generate a 1024-bit Digital Signature Algorithm (DSA) key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm); keyGen.initialize(1024);// w w w . j av a 2 s .c om KeyPair keypair = keyGen.genKeyPair(); PrivateKey privateKey = keypair.getPrivate(); PublicKey publicKey = keypair.getPublic(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String algorithm = "DSA"; // or RSA, DH, etc. // Generate a 1024-bit Digital Signature Algorithm (DSA) key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm); keyGen.initialize(1024);//from w w w .j a va 2s .c o m KeyPair keypair = keyGen.genKeyPair(); PrivateKey privateKey = keypair.getPrivate(); PublicKey publicKey = keypair.getPublic(); String format = privateKey.getFormat(); // PKCS#8 format = publicKey.getFormat(); // X.509 }
From source file:Main.java
public static void main(String[] argv) throws Exception { String algorithm = "DSA"; // or RSA, DH, etc. // Generate a 1024-bit Digital Signature Algorithm (DSA) key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm); keyGen.initialize(1024);//from ww w . j a v a2 s.c o m KeyPair keypair = keyGen.genKeyPair(); PrivateKey privateKey = keypair.getPrivate(); PublicKey publicKey = keypair.getPublic(); byte[] privateKeyBytes = privateKey.getEncoded(); byte[] publicKeyBytes = publicKey.getEncoded(); }
From source file:cn.lynx.emi.license.GenerateKeyPairs.java
public static void main(String[] args) throws NoSuchAlgorithmException { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); SecureRandom secrand = new SecureRandom(); secrand.setSeed("cn.lynx.emi".getBytes()); keygen.initialize(4096, secrand);/*w ww.j a v a 2 s . com*/ KeyPair keys = keygen.genKeyPair(); PublicKey pubkey = keys.getPublic(); PrivateKey prikey = keys.getPrivate(); String myPubKey = Base64.encodeBase64String(pubkey.getEncoded()); String myPriKey = Base64.encodeBase64String(prikey.getEncoded()); System.out.println(prikey); System.out.println("pubKey=" + myPubKey); System.out.println("priKey=" + myPriKey); }
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);/* www.j a v a 2 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:MainClass.java
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException { KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA"); kpg.initialize(512);/*w w w . jav a2 s . c o m*/ KeyPair keys = kpg.genKeyPair(); PrivateKey priKey = keys.getPrivate(); PublicKey pubKey = keys.getPublic(); KeyFactory kf = KeyFactory.getInstance("DSA"); DSAPrivateKeySpec dsaPriKeySpec = (DSAPrivateKeySpec) kf.getKeySpec(priKey, DSAPrivateKeySpec.class); DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec) kf.getKeySpec(pubKey, DSAPublicKeySpec.class); System.out.println("\nDSA Private Key"); System.out.println("\nx = " + dsaPriKeySpec.getX()); System.out.println("\nDSA Public Key"); System.out.println("\ng = " + dsaPubKeySpec.getG()); System.out.println("\np = " + dsaPubKeySpec.getP()); System.out.println("\nq = " + dsaPubKeySpec.getQ()); System.out.println("\ny = " + dsaPubKeySpec.getY()); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);//from w w w. j av 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: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 2s . 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 { String password = "password"; KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024);//ww w . j a v a2 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 { String algorithm = "DSA"; // or RSA, DH, etc. // Generate a 1024-bit Digital Signature Algorithm (DSA) key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm); keyGen.initialize(1024);/*from w ww. j a v a2 s .c o m*/ KeyPair keypair = keyGen.genKeyPair(); PrivateKey privateKey = keypair.getPrivate(); PublicKey publicKey = keypair.getPublic(); byte[] privateKeyBytes = privateKey.getEncoded(); byte[] publicKeyBytes = publicKey.getEncoded(); KeyFactory keyFactory = KeyFactory.getInstance(algorithm); EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes); PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec); EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes); PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec); // The orginal and new keys are the same boolean same = privateKey.equals(privateKey2); same = publicKey.equals(publicKey2); }