Example usage for java.security KeyFactory getInstance

List of usage examples for java.security KeyFactory getInstance

Introduction

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

Prototype

public static KeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a KeyFactory object that converts public/private keys of the specified algorithm.

Usage

From source file:com.java.demo.RsaDemo.java

public static boolean Verify(String str, byte[] enstr) {
    try {/*  www .  j  a  v  a 2 s.  c  o  m*/
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rSAPublicKey.getEncoded());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initVerify(publicKey);
        signature.update(str.getBytes());
        boolean result = signature.verify(enstr);
        return result;
    } catch (Exception e) {
        System.out.println("com.Java.Demo.RsaDemo.Verify()" + e.getMessage());
        return false;
    }
}

From source file:aiai.apps.commons.utils.SecUtils.java

public static PrivateKey getPrivateKey(String keyBase64)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] keyBytes = Base64.decodeBase64(keyBase64);
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePrivate(spec);
}

From source file:org.casbah.provider.KeyHelper.java

public static PrivateKey readKey(String keypass, byte[] keyData) throws CAProviderException {
    try {//from  w  ww . ja v a2 s . c  om
        EncryptedPrivateKeyInfo pkInfo = new EncryptedPrivateKeyInfo(keyData);
        PBEKeySpec keySpec = new PBEKeySpec(keypass.toCharArray());
        SecretKeyFactory pbeKeyFactory = SecretKeyFactory.getInstance(pkInfo.getAlgName());
        PKCS8EncodedKeySpec encodedKeySpec = pkInfo.getKeySpec(pbeKeyFactory.generateSecret(keySpec));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (RSAPrivateCrtKey) keyFactory.generatePrivate(encodedKeySpec);
    } catch (Exception e) {
        throw new CAProviderException("Could not decode private key", e);
    }

}

From source file:license.rsa.WakeRSA.java

/**
 * ?/*from  ww w  .  j  a va  2s  .  co m*/
 * 
 * @param keyFileName
 * @return
 * @throws Exception
 */
static PublicKey readPublicKeyFromFile() throws Exception {
    ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(KeyData.publicKey));
    try {
        BigInteger m = (BigInteger) oin.readObject();
        BigInteger e = (BigInteger) oin.readObject();
        System.out.println();
        System.out.println();
        System.out.println("=======mmm=====" + m);
        System.out.println("=======eeee=====" + e);
        System.out.println();
        System.out.println();
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        return fact.generatePublic(keySpec);
    } finally {
        oin.close();
    }
}

From source file:org.hypoport.jwt.common.Toolbox.java

public static ECPrivateKey readECDSAPrivateKey(Reader keyReader) throws Exception {
    return (ECPrivateKey) KeyFactory.getInstance("EC")
            .generatePrivate(new PKCS8EncodedKeySpec(readPemFile(keyReader)));
}

From source file:com.github.ibole.infrastructure.security.key.PemUtils.java

private static PublicKey getPublicKey(byte[] keyBytes, String algorithm) {
    PublicKey publicKey = null;/*from   w  ww .jav  a  2s .  co m*/
    try {
        KeyFactory kf = KeyFactory.getInstance(algorithm);
        EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        publicKey = kf.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        MoreThrowables.throwIfUnchecked(e);
    }
    return publicKey;
}

From source file:net.oauth.jsontoken.crypto.MagicRsaPublicKey.java

private static PublicKey parseKey(String magicKey) {
    String[] pieces = magicKey.split(Pattern.quote("."));
    if (pieces.length != 3) {
        throw new IllegalStateException("not a valid magic key: " + magicKey);
    }/*from   w  ww .  j a  va 2s . c om*/

    if (!pieces[0].equals("RSA")) {
        throw new IllegalStateException("unkown key type for magic key: " + pieces[0]);
    }

    String modulusString = pieces[1];
    String exponentString = pieces[2];

    byte[] modulusBytes = Base64.decodeBase64(modulusString);
    byte[] exponentBytes = Base64.decodeBase64(exponentString);

    BigInteger modulus = new BigInteger(modulusBytes);
    BigInteger exponent = new BigInteger(exponentBytes);

    RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent);
    KeyFactory fac;
    try {
        fac = KeyFactory.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("RSA key factory missing on platform", e);
    }
    try {
        return fac.generatePublic(spec);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException("bad key in descripor doc: " + magicKey, e);
    }
}

From source file:com.zf.decipher.DataEn.java

private static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception {
    KeyFactory keyFactory = KeyFactory.getInstance("DH");
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
    PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
    PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
    KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm());
    keyAgree.init(priKey);/* ww  w .j  a va2 s. co  m*/
    keyAgree.doPhase(pubKey, true);
    return keyAgree.generateSecret(AES).getEncoded();
}

From source file:org.casbah.provider.PKCS1EncodedKeyTest.java

@Before
public void setup() throws GeneralSecurityException {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateCrtKeySpec keyspec = new RSAPrivateCrtKeySpec(MODULUS, PUBLIC_EXPONENT, PRIVATE_EXPONENT, PRIME1,
            PRIME2, EXPONENT1, EXPONENT2, COEFFICIENT);
    wrappedKey = (RSAPrivateCrtKey) kf.generatePrivate(keyspec);
}

From source file:org.opentravel.schemacompiler.security.GenerateEncryptionKeys.java

/**
 * Generates the public and private key files used for encrypting and decrypting passwords.
 *//*  w w  w.  ja v a 2  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());
}