Example usage for java.security.spec X509EncodedKeySpec X509EncodedKeySpec

List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec

Introduction

In this page you can find the example usage for java.security.spec X509EncodedKeySpec X509EncodedKeySpec.

Prototype

public X509EncodedKeySpec(byte[] encodedKey) 

Source Link

Document

Creates a new X509EncodedKeySpec with the given encoded key.

Usage

From source file:org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper.java

public static PublicKey getPublicKey(Context context, String deviceId) throws Exception {
    try {//w w w.  java2  s .c om
        SharedPreferences settings = context.getSharedPreferences(deviceId, Context.MODE_PRIVATE);
        byte[] publicKeyBytes = Base64.decode(settings.getString("publicKey", ""), 0);
        PublicKey publicKey = KeyFactory.getInstance("RSA")
                .generatePublic(new X509EncodedKeySpec(publicKeyBytes));
        return publicKey;
    } catch (Exception e) {
        throw e;
    }
}

From source file:com.alliander.osgp.shared.security.CertificateHelper.java

public static PublicKey createPublicKeyFromBase64(final String keyBase64, final String keyType,
        final String provider)
        throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, NoSuchProviderException {
    final byte[] key = Base64.decodeBase64(keyBase64);

    final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(key);
    final KeyFactory publicKeyFactory = KeyFactory.getInstance(keyType, provider);
    return publicKeyFactory.generatePublic(publicKeySpec);
}

From source file:org.kaaproject.kaa.common.endpoint.security.KeyUtil.java

/**
 * Saves public and private keys to specified streams.
 *
 * @param keyPair          the key pair// w  ww  . j  a va  2  s  . c  o m
 * @param privateKeyOutput the private key output stream
 * @param publicKeyOutput  the public key output stream
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void saveKeyPair(KeyPair keyPair, OutputStream privateKeyOutput, OutputStream publicKeyOutput)
        throws IOException {
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();

    // Store Public Key.
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
    publicKeyOutput.write(x509EncodedKeySpec.getEncoded());

    // Store Private Key.
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
    privateKeyOutput.write(pkcs8EncodedKeySpec.getEncoded());
}

From source file:net.arccotangent.pacchat.filesystem.KeyManager.java

public static KeyPair loadRSAKeys() {
    try {//from w w w . j a va2 s .c  o m
        km_log.i("Loading RSA key pair from disk.");
        byte[] privEncoded = Files.readAllBytes(privkeyFile.toPath());
        byte[] pubEncoded = Files.readAllBytes(pubkeyFile.toPath());

        X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64.decodeBase64(pubEncoded));
        PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privEncoded));

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PublicKey pubkey = keyFactory.generatePublic(pubSpec);
        PrivateKey privkey = keyFactory.generatePrivate(privSpec);

        return new KeyPair(pubkey, privkey);
    } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
        km_log.e("Error while loading keypair!");
        e.printStackTrace();
    }
    return null;
}

From source file:net.nicholaswilliams.java.licensing.encryption.KeyFileUtilities.java

public static PublicKey readEncryptedPublicKey(byte[] fileContents, char[] passphrase) {
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Encryptor.decryptRaw(fileContents, passphrase));

    try {/*w  w  w  . j  a v  a 2  s  . c  om*/
        return KeyFactory.getInstance(KeyFileUtilities.keyAlgorithm).generatePublic(publicKeySpec);
    } catch (NoSuchAlgorithmException e) {
        throw new AlgorithmNotSupportedException(KeyFileUtilities.keyAlgorithm, e);
    } catch (InvalidKeySpecException e) {
        throw new InappropriateKeySpecificationException(e);
    }
}

From source file:cn.mrdear.pay.util.RSAUtils.java

/**
 * ?/*www  .  j  a va2s . co m*/
 * 
 * @param encodedKey
 *            ?
 * @return 
 */
public static PublicKey generatePublicKey(byte[] encodedKey) {
    Assert.notNull(encodedKey);

    try {
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM, PROVIDER);
        return keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:org.javaweb.utils.RSAUtils.java

/**
 * //from w ww. ja v  a2 s  . co  m
 *
 * @param publicKey
 * @return
 * @throws Exception
 */
public static PublicKey getPublicKey(String publicKey) throws Exception {
    byte[] keyBytes = Base64.decodeBase64(publicKey);
    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    return keyFactory.generatePublic(spec);
}

From source file:com.xinferin.licensing.LicenceGenerator.java

/**
 * Creates a new private and public key and at the same time encodes the public key as XML to be used by the .NET client
 * @param size/*from w  w  w  . j  a va 2 s.co m*/
 * @param productId
 *
 */
private void firstTimeInitialisation(int size) {
    try {

        // Get Key Pair Generator for RSA.
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(size);

        KeyPair keypair = keyGen.genKeyPair();
        privateKey = keypair.getPrivate();
        publicKey = keypair.getPublic();

        // Get the bytes of the public and private keys
        byte[] privateKeyBytes = privateKey.getEncoded();
        byte[] publicKeyBytes = publicKey.getEncoded();

        // store temporarily witht he public key for the lifetime of this class.
        encodedPrivateKey = new Base64().encode(privateKeyBytes);

        // Generate the Private Key, Public Key and Public Key in XML format.
        KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
        KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKeyBytes));
        RSAPublicKey rsaPublicKey = (RSAPublicKey) KeyFactory.getInstance("RSA")
                .generatePublic(new X509EncodedKeySpec(publicKeyBytes));

        // Store the public key in XML string to make compatible .Net public key file
        encodedToXMLPublicKey = getRSAPublicKeyAsXMLString(rsaPublicKey);

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:com.kuya.cordova.plugin.util.Security.java

/**
 * Generates a PublicKey instance from a string containing the
 * Base64-encoded public key./*w w w . j a v  a2  s  .c  om*/
 *
 * @param encodedPublicKey Base64-encoded public key
 * @throws IllegalArgumentException if encodedPublicKey is invalid
 */
public static PublicKey generatePublicKey(String encodedPublicKey) {
    Log.d(TAG, "generatePublicKey " + encodedPublicKey);
    try {
        byte[] decodedKey = Base64.decode(encodedPublicKey);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
        return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        Log.e(TAG, "Invalid key specification.");
        throw new IllegalArgumentException(e);
    } catch (Base64DecoderException e) {
        Log.e(TAG, "Base64 decoding failed.");
        throw new IllegalArgumentException(e);
    }
}

From source file:com.tenduke.example.scribeoauth.JwtLoginServlet.java

/**
 * Initializes this servlet.// ww  w  .jav  a 2s .c  o  m
 * @param config Servlet configuration.
 * @throws ServletException For errors during init.
 */
@Override
public void init(final ServletConfig config) throws ServletException {
    //
    super.init(config);
    //
    final JSONObject jwtPublicKey = readConfiguration("idp.jwt.publickey.json", config.getServletContext());
    try {
        //
        byte[] publicKeyDecoded = Base64.decodeBase64(jwtPublicKey.getString("publicKey"));
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyDecoded);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        publicKey = keyFactory.generatePublic(keySpec);
    } catch (InvalidKeySpecException | NoSuchAlgorithmException ex) {
        //
        throw new ServletException("No way, basic RSA based key generation failed...", ex);
    }
}