List of usage examples for java.security KeyPairGenerator generateKeyPair
public KeyPair generateKeyPair()
From source file:com.github.ibole.infrastructure.security.ECDSA.java
public static void jdkECDSA() { try {//from ww w . ja v a2 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:Main.java
static KeyPair generateRsaKeyPair(int keySizeInBits) { if (keySizeInBits == 0 || keySizeInBits % 8 != 0) return null; try {// www . ja v a 2 s .com KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "SC"); generator.initialize(keySizeInBits, new SecureRandom()); KeyPair pair = generator.generateKeyPair(); // Log.d("","public : "+keyToBase64(pair.getPublic())); // Log.d("","private: "+keyToBase64(pair.getPrivate())); return pair; } catch (Exception e) { e.printStackTrace(); } return null; }
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);/*ww w. j a va2 s .c om*/ keyGenerator.initialize(1024, rng); return (keyGenerator.generateKeyPair()); }
From source file:com.boubei.tss.modules.license.LicenseFactory.java
/** * ???//from w ww.j a v a 2 s . c o m * ????hacker??license * @throws Exception */ public static void generateKey() throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyGen.initialize(1024, new SecureRandom()); KeyPair pair = keyGen.generateKeyPair(); PrivateKey priv = pair.getPrivate(); PublicKey pub = pair.getPublic(); log.info("?"); DataOutputStream out = new DataOutputStream(new FileOutputStream(PUBLIC_KEY_FILE)); out.writeBytes(EasyUtils.encodeHex(pub.getEncoded())); out.close(); log.info("??" + PUBLIC_KEY_FILE); out = new DataOutputStream(new FileOutputStream(PRIVATE_KEY_FILE)); out.writeBytes(EasyUtils.encodeHex(priv.getEncoded())); out.close(); log.info("??" + PRIVATE_KEY_FILE); }
From source file:com.google.jenkins.plugins.credentials.oauth.P12ServiceAccountConfigTestUtil.java
public static KeyPair generateKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException { Security.addProvider(new BouncyCastleProvider()); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC"); keyPairGenerator.initialize(1024);/*from w w w.ja v a2s .c o m*/ return keyPairGenerator.generateKeyPair(); }
From source file:com.lingxiang2014.util.RSAUtils.java
public static KeyPair generateKeyPair() { try {/*from www .jav a2 s . c o m*/ KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", PROVIDER); keyPairGenerator.initialize(KEY_SIZE, new SecureRandom()); return keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }
From source file:org.opentravel.schemacompiler.security.GenerateEncryptionKeys.java
/** * Generates the public and private key files used for encrypting and decrypting passwords. *//*from w w w. j a v a2 s. c o m*/ public static void generateKeyFiles() throws Exception { File publicKeyFile = new File(System.getProperty("user.dir"), "/src/main/resources" + PasswordHelper.PUBLIC_KEYFILE); File privateKeyFile = new File(System.getProperty("user.dir"), "/src/main/resources" + PasswordHelper.PRIVATE_KEYFILE); KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(PasswordHelper.ENCRYPTION_ALGORITHM); keyGenerator.initialize(ENCRYPTION_KEY_SIZE); KeyPair keyPair = keyGenerator.generateKeyPair(); KeyFactory fact = KeyFactory.getInstance(PasswordHelper.ENCRYPTION_ALGORITHM); RSAPublicKeySpec publicKeySpec = fact.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class); RSAPrivateKeySpec privateKeySpec = fact.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class); System.out .println("Public Key : " + publicKeySpec.getModulus() + " / " + publicKeySpec.getPublicExponent()); System.out.println( "Private Key: " + privateKeySpec.getModulus() + " / " + privateKeySpec.getPrivateExponent()); writeKeyFile(publicKeyFile, publicKeySpec.getModulus(), publicKeySpec.getPublicExponent()); writeKeyFile(privateKeyFile, privateKeySpec.getModulus(), privateKeySpec.getPrivateExponent()); }
From source file:org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator.java
@VisibleForTesting public static String generate() throws NoSuchAlgorithmException, IOException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); SecureRandom sr = new SecureRandom(); keyGen.initialize(KEY_SIZE, sr);/*w w w. ja va2s . co m*/ KeyPair keypair = keyGen.generateKeyPair(); return toString(keypair.getPrivate()); }
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 va 2 s . co m KeyPair pair = generator.generateKeyPair(); publicKey = pair.getPublic(); privateKey = pair.getPrivate(); }
From source file:com.example.license.RSAUtil.java
public static KeyPair generatorKeyPair(String seed) throws Exception { KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM); SecureRandom random = new SecureRandom(); random.setSeed(seed.getBytes());/*from w w w . ja v a 2 s . c om*/ kpGenerator.initialize(2014, random); KeyPair keyPair = kpGenerator.generateKeyPair(); return keyPair; }