List of usage examples for java.security KeyPairGenerator initialize
public void initialize(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException
From source file:com.github.ibole.infrastructure.security.ECDSA.java
public static void jdkECDSA() { try {//from w w w .j a v a2 s. co 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:love.sola.netsupport.util.RSAUtil.java
public static void genKeyPair() { try {/*from w ww . j a v a2 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:net.sf.zekr.common.util.CryptoUtils.java
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException { // Generate a 1024-bit Digital Signature Algorithm (DSA) key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024); KeyPair keypair = keyGen.genKeyPair(); return keypair; }
From source file:com.leclercb.commons.api.license.LicenseManager.java
public static KeyPair generateKeys(String algorithm, int keySize) throws NoSuchAlgorithmException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm); keyGen.initialize(keySize); return keyGen.genKeyPair(); }
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 va 2s . 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:models.logic.CipherDecipher.java
public static KeyPair publicPrivateKeyGenerator() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { //Generation//from w ww .jav a 2 s.co m KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(2048); return kpg.genKeyPair(); }
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); return keyPairGenerator.generateKeyPair(); }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.M) private static void generateNewKey() { try {//from w w w. j ava2s. co m KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, KEY_PROVIDER); keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(KEY_ALIAS, KeyProperties.PURPOSE_DECRYPT) .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP).build()); keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } }
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); KeyPair pair = generator.generateKeyPair(); publicKey = pair.getPublic();//from w w w .j a v a 2 s . c o m privateKey = pair.getPrivate(); }
From source file:hh.learnj.test.license.test.rsa.RSATest.java
/** * ?// w w w .j a v a 2 s . c o 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); }