Example usage for java.security KeyPairGenerator getInstance

List of usage examples for java.security KeyPairGenerator getInstance

Introduction

In this page you can find the example usage for java.security KeyPairGenerator getInstance.

Prototype

public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a KeyPairGenerator object that generates public/private key pairs for the specified algorithm.

Usage

From source file:edu.vt.middleware.crypt.signature.SignatureAlgorithmTest.java

/**
 * @return  Test data./*from ww w.j  a v  a 2  s . c  o m*/
 *
 * @throws  Exception  On test data generation failure.
 */
@DataProvider(name = "testdata")
public Object[][] createTestData() throws Exception {
    final KeyPairGenerator rsaKeyGen = KeyPairGenerator.getInstance("RSA");
    final KeyPair rsaKeys = rsaKeyGen.generateKeyPair();

    final KeyPairGenerator dsaKeyGen = KeyPairGenerator.getInstance("DSA");
    final KeyPair dsaKeys = dsaKeyGen.generateKeyPair();

    return new Object[][] { { new DSASignature(), dsaKeys, null, },
            { new DSASignature(), dsaKeys, new Base64Converter(), },
            { new DSASignature(new SHA512()), dsaKeys, new HexConverter(), },
            { new DSASignature(new Tiger()), dsaKeys, new HexConverter(), },
            { new RSASignature(), rsaKeys, null, }, { new RSASignature(), rsaKeys, new Base64Converter(), },
            { new RSASignature(new MD5()), rsaKeys, new HexConverter(), },
            { new RSASignature(new Whirlpool()), rsaKeys, new HexConverter(), }, };
}

From source file:com.streamsets.lib.security.util.DataSignature.java

public KeyPair generateKeyPair() throws GeneralSecurityException {
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA");
    SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN");
    rng.setSeed(System.currentTimeMillis());
    keyGenerator.initialize(1024, rng);//from  ww w  .  j  av  a 2  s .c o  m
    return keyGenerator.generateKeyPair();
}

From source file:unit.test.be.e_contract.mycarenet.ehealth.common.WSSecuritySOAPHandlerTest.java

@Test
public void testHandleMessage() throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    PrivateKey privateKey = keyPair.getPrivate();

    String samlAssertion = "<Assertion xmlns=\"urn:oasis:names:tc:SAML:1.0:assertion\""
            + " AssertionID=\"_42e7a00652420d86ee884f295a3fbf02\">" + "</Assertion>";

    WSSecuritySOAPHandler testedInstance = new WSSecuritySOAPHandler();
    testedInstance.setPrivateKey(privateKey);
    testedInstance.setAssertion(samlAssertion);

    SOAPMessageContext mockSoapMessageContext = EasyMock.createMock(SOAPMessageContext.class);
    EasyMock.expect(mockSoapMessageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)).andReturn(true);

    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
    SOAPBody soapBody = soapEnvelope.getBody();
    soapBody.addBodyElement(new QName("http://www.example.com", "Test"));

    EasyMock.expect(mockSoapMessageContext.getMessage()).andReturn(soapMessage);

    // prepare/* w w  w  .  j av a  2 s  . co  m*/
    EasyMock.replay(mockSoapMessageContext);

    // operate
    testedInstance.handleMessage(mockSoapMessageContext);

    // verify
    EasyMock.verify(mockSoapMessageContext);
    LOG.debug(toString(soapPart));
}

From source file:com.zxy.commons.codec.rsa.RSAUtils.java

/**
 * <p>/*w  w w.java 2s. c om*/
 * ?(?)
 * </p>
 * 
 * @return Map<String, Object>
 * @throws Exception Exception
 */
public static Map<String, Object> genKeyPair() throws Exception {
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    keyPairGen.initialize(1024);
    KeyPair keyPair = keyPairGen.generateKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    Map<String, Object> keyMap = new HashMap<String, Object>(2);
    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);
    return keyMap;
}

From source file:com.buzzcoders.security.cryptoutils.asymmetric.AbstractAsymmetricEncryptionModule.java

public KeyPair generateKeyPair(int keyLength) {
    KeyPair kp = null;/*ww  w.  j  a v  a2s. c  o m*/
    try {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(getAlgorithm());
        kpg.initialize(keyLength);
        kp = kpg.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        LOG.error("Error generating the key pair.", e);
    }
    return kp;
}

From source file:cherry.goods.crypto.RSASignatureTest.java

private RSASignature create1() throws Exception {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    keygen.initialize(2048);// w  ww.j  a v  a 2 s. c  om
    KeyPair key = keygen.generateKeyPair();
    RSASignature impl = new RSASignature();
    impl.setAlgorithm("SHA256withRSA");
    impl.setPublicKeyBytes(key.getPublic().getEncoded());
    impl.setPrivateKeyBytes(key.getPrivate().getEncoded());
    return impl;
}

From source file:cherry.goods.crypto.RSACryptoTest.java

private RSACrypto create1() throws Exception {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    keygen.initialize(2048);/*from www  . java2 s.  co  m*/
    KeyPair key = keygen.generateKeyPair();
    RSACrypto impl = new RSACrypto();
    impl.setAlgorithm("RSA/ECB/PKCS1Padding");
    impl.setPublicKeyBytes(key.getPublic().getEncoded());
    impl.setPrivateKeyBytes(key.getPrivate().getEncoded());
    return impl;
}

From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java

/**
 * RSA???//from  w  w w. j av a2  s  .  co m
 * <dl>
 * <dt>?
 * <dd>RSA??????2048??????
 * </dl>
 * @return RSA?
 */
public static KeyPair createKeyPair() {
    try {
        final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(2048);
        final KeyPair pair = generator.generateKeyPair();
        if (LOG.isDebugEnabled()) {
            final RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic();
            final RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate();
            LOG.debug("public-modulus={}", Base64.encodeBase64String(publicKey.getModulus().toByteArray()));
            LOG.debug("public-exponent={}",
                    Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray()));
            LOG.debug("private-modulus={}", Base64.encodeBase64String(privateKey.getModulus().toByteArray()));
            LOG.debug("private-exponent={}",
                    Base64.encodeBase64String(privateKey.getPrivateExponent().toByteArray()));
        }
        return pair;
    } catch (final NoSuchAlgorithmException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:com.bluepixel.security.manager.Server.java

private void generateKey() {
    try {/*w w  w.j  a va  2  s  . c o m*/
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(DEFAULT_ALGORITHM);
        keyGen.initialize(DEFAULT_KEY_LENGTH);
        KeyPair keypair = keyGen.generateKeyPair();
        PublicKey pbKey = keypair.getPublic();
        PrivateKey piKey = keypair.getPrivate();

        publicKey = Base64.encodeWebSafe(pbKey.getEncoded(), false);
        privateKey = Base64.encodeWebSafe(piKey.getEncoded(), false);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

        cipher.init(Cipher.ENCRYPT_MODE, piKey);

        secretKeys = new ConcurrentHashMap<String, String>();
        String[] randomKeys = generateRandomWords(10);
        for (String key : randomKeys) {
            String cipherText = Base64.encodeWebSafe(cipher.doFinal(key.getBytes()), false);
            secretKeys.put(key, cipherText);
        }
    } catch (NoSuchAlgorithmException e) {
    } catch (InvalidKeyException e) {
    } catch (NoSuchPaddingException e) {
    } catch (IllegalBlockSizeException e) {
    } catch (BadPaddingException e) {
    }
}

From source file:no.digipost.api.client.ApiServiceMock.java

static EncryptionKey createFakeEncryptionKey() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (Writer osWriter = new OutputStreamWriter(baos); PEMWriter writer = new PEMWriter(osWriter)) {

        KeyPairGenerator factory = KeyPairGenerator.getInstance("RSA");
        factory.initialize(2048);/*from  w ww . ja v  a  2  s .  c o  m*/
        KeyPair keyPair = factory.generateKeyPair();

        writer.writeObject(keyPair.getPublic());

    } catch (Exception e) {
        throw new RuntimeException("Failed creation of fake encryption key.", e);
    }

    EncryptionKey fakeKey = new EncryptionKey();
    fakeKey.setKeyId("fake-hash");
    fakeKey.setValue(new String(baos.toByteArray()));

    return fakeKey;
}