List of usage examples for java.security KeyPairGenerator getInstance
public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:MainClass.java
public static KeyPair generateKeyPair(long seed) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA"); SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN"); rng.setSeed(seed);//www. j a v a 2 s. co m keyGenerator.initialize(1024, rng); return (keyGenerator.generateKeyPair()); }
From source file:br.edu.ufcg.lsd.commune.network.signature.Util.java
public static KeyPair generateKeyPair() { KeyPairGenerator keyGen;/*ww w . j a v a 2 s. c o m*/ try { keyGen = KeyPairGenerator.getInstance(SignatureConstants.KEY_GEN_ALGORITHM); } catch (NoSuchAlgorithmException e) { //We're assuming that we are always using a valid algorithm throw new CommuneRuntimeException(e); } keyGen.initialize(SignatureConstants.KEYSIZE); return keyGen.genKeyPair(); }
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 ww .ja va 2 s . c om KeyPair pair = generator.generateKeyPair(); publicKey = pair.getPublic(); privateKey = pair.getPrivate(); }
From source file:com.axelor.apps.account.ebics.certificate.KeyUtil.java
/** * Generates a <code>KeyPair</code> in RSA format. * * @param keyLen - key size//from w w w . ja va2 s. com * @return KeyPair the key pair * @throws NoSuchAlgorithmException */ public static KeyPair makeKeyPair(int keyLen) throws NoSuchAlgorithmException { KeyPairGenerator keyGen; keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(keyLen, new SecureRandom()); KeyPair keypair = keyGen.generateKeyPair(); return keypair; }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * ?/*from w w w. j a v a 2 s . co m*/ * * @return Map Map */ public static Map<String, Object> initKey() throws Exception { // ? KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM); // ?? keyPairGenerator.initialize(KEY_SIZE); // ? KeyPair keyPair = keyPairGenerator.generateKeyPair(); // RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // ? RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // map Map<String, Object> keyMap = new HashMap<String, Object>(); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; }
From source file:com.github.ibole.infrastructure.security.ECDSA.java
public static void jdkECDSA() { try {/* w w w . ja v a 2 s .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.znsx.util.licence.LicenceUtil.java
/** * ??// www. j a v a 2 s .c o m * * @param seed * ?? * @return * @throws Exception */ public static Map<String, String> generateKey(String seed) throws Exception { Map<String, String> map = new HashMap<String, String>(2); KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA"); SecureRandom random = new SecureRandom(); random.setSeed(seed.getBytes("utf8")); keygen.initialize(1024, random); KeyPair keyPair = keygen.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); Base64 base64 = new Base64(); String publicKeyString = new String(base64.encode(publicKey.getEncoded()), "utf8"); String privateKeyString = new String(base64.encode(privateKey.getEncoded()), "utf8"); // BASE64Encoder encoder = new BASE64Encoder(); // map.put("public", encoder.encode(publicKey.getEncoded())); // map.put("private", encoder.encode(privateKey.getEncoded())); map.put("public", publicKeyString); map.put("private", privateKeyString); System.out.println("publicKey: " + map.get("public")); System.out.println("privateKey: " + map.get("private")); return map; }
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 a v a 2 s. 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:hh.learnj.test.license.test.rsa.RSATest.java
/** * ?/* w w w . j av a 2 s . co m*/ * * @throws Exception */ static void generatorKeyPair() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); byte[] keyBs = rsaPublicKey.getEncoded(); publicKey = encodeBase64(keyBs); System.out.println("?\r\n" + publicKey); RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); keyBs = rsaPrivateKey.getEncoded(); privateKey = encodeBase64(keyBs); System.out.println("??\r\n" + privateKey); }
From source file:aiai.apps.gen_keys.GenerateKeys.java
public GenerateKeys(int keylength) throws NoSuchAlgorithmException { this.keyGen = KeyPairGenerator.getInstance("RSA"); this.keyGen.initialize(keylength); }