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:de.pawlidi.openaletheia.utils.CipherUtils.java

/**
 * //from   ww w  .  j  a v  a2s  .com
 * @param data
 * @return
 */
public static RSAPublicKey buildPublicKey(final String key) {
    if (StringUtils.isNotEmpty(key)) {
        try {
            byte[] bytes = Converter.toBytes(key);
            KeyFactory keyFactory = KeyFactory.getInstance(CIPHER_ALGORITHM);
            X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(bytes);
            return (RSAPublicKey) keyFactory.generatePublic(pubSpec);
        } catch (Exception e) {
            throw new RuntimeException("Cannot create " + CIPHER_ALGORITHM + " public key from " + key, e);
        }
    }
    return null;
}

From source file:com.shenit.commons.codec.RsaUtils.java

/**
 * RSA??//from w  w  w .  java2 s  .  co m
 * 
 * @param content
 *            ???
 * @param sign
 *            ??
 * @param publicKey
 *            ?
 * @param inputCharset
 *            ??
 * @return 
 */
public static boolean verify(String content, String sign, String publicKey, String algorithm,
        String inputCharset) {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(CODEC_RSA);
        byte[] encodedKey = Base64.decodeBase64(publicKey);
        PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));

        java.security.Signature signature = java.security.Signature.getInstance(algorithm);

        signature.initVerify(pubKey);
        signature.update(content.getBytes(inputCharset));

        boolean bverify = signature.verify(Base64.decodeBase64(sign));
        return bverify;

    } catch (Exception e) {
        if (LOG.isWarnEnabled())
            LOG.warn("[verify] verify with exception", e);
    }

    return false;
}

From source file:org.springframework.security.oauth.common.signature.RSAKeySecret.java

/**
 * Creates a public key from the X509-encoded value of the given bytes.
 *
 * @param publicKey The X509-encoded public key bytes.
 * @return The public key.// w  w  w. j a v  a  2 s. co m
 */
public static PublicKey createPublicKey(byte[] publicKey) {
    if (publicKey == null) {
        return null;
    }

    try {
        KeyFactory fac = KeyFactory.getInstance("RSA");
        EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
        return fac.generatePublic(spec);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.security.ch08_rsa.RSACoderTextKey.java

/**
 * /*from   ww  w .  j a va  2  s.  c  o  m*/
 *
 * @param data
 *            ?
 * @param key
 *            
 * @return byte[] ?
 * @throws Exception
 */
private static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception {

    // ?
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);

    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

    PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);

    // ?
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

    cipher.init(Cipher.ENCRYPT_MODE, publicKey);

    return cipher.doFinal(data);
}

From source file:org.parelon.pskc.CryptManager.java

/**
 * Initialize RSA Keys and Ciphers. All RSA keys MUST be Base64Decode first!
 *
 * @param privateKeyFragment1 First half of the Private Key
 * @param privateKeyFragment2 Second half of the Private Key
 * @param publicKey Public Key/*from w w w .  ja v  a2 s  .  c om*/
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.security.spec.InvalidKeySpecException
 * @throws javax.crypto.NoSuchPaddingException
 * @throws java.security.InvalidKeyException
 * @throws com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException
 */
public void initRsaOperations(byte[] privateKeyFragment1, byte[] privateKeyFragment2, byte[] publicKey)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException,
        Base64DecodingException, UnsupportedEncodingException, DestroyFailedException {
    initPrivateKey(privateKeyFragment1, privateKeyFragment2);
    this.rsaPublicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKey));
    this.rsaDecryptCipher = Cipher.getInstance("RSA");
    this.rsaDecryptCipher.init(Cipher.DECRYPT_MODE, this.rsaPrivateKey);
    this.rsaEncryptCipher = Cipher.getInstance("RSA");
    this.rsaEncryptCipher.init(Cipher.ENCRYPT_MODE, this.rsaPublicKey);
}

From source file:com.vexsoftware.votifier.util.rsa.RSAIO.java

/**
 * Loads an RSA key pair from a directory. The directory must have the files
 * "public.key" and "private.key"./*  w w w . j  a va 2s.c o m*/
 * 
 * @param directory
 *            The directory to load from
 * @return The key pair
 * @throws Exception
 *             If an error occurs
 */
public static KeyPair load(File directory) throws Exception {
    // Read the public key file.
    File publicKeyFile = new File(directory + "/public.key");
    FileInputStream in = null;
    byte[] encodedPublicKey;
    try {
        in = new FileInputStream(directory + "/public.key");
        encodedPublicKey = new byte[(int) publicKeyFile.length()];
        in.read(encodedPublicKey);
        encodedPublicKey = DatatypeConverter.parseBase64Binary(new String(encodedPublicKey));
    } finally {
        try {
            in.close();
        } catch (Exception exception) {
            // ignore
        }
    }

    // Read the private key file.
    File privateKeyFile = new File(directory + "/private.key");
    byte[] encodedPrivateKey;
    try {
        in = new FileInputStream(directory + "/private.key");
        encodedPrivateKey = new byte[(int) privateKeyFile.length()];
        in.read(encodedPrivateKey);
        encodedPrivateKey = DatatypeConverter.parseBase64Binary(new String(encodedPrivateKey));
    } finally {
        try {
            in.close();
        } catch (Exception exception) {
            // ignore
        }
    }

    // Instantiate and return the key pair.
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
    return new KeyPair(publicKey, privateKey);
}

From source file:com.ddubyat.develop.jhawtcode.util.PropertyUtil.java

private boolean validLicense(String email, String licenseCode) throws Exception {

    Resource res = applicationContext.getResource("classpath:jhc-public.der");
    InputStream is = res.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] buffer = new byte[4096];
    byte[] pkey;//from  ww  w. java  2  s  .c  o m
    int stream;
    while ((stream = is.read(buffer, 0, buffer.length)) != -1) {
        baos.write(buffer, 0, stream);
    }

    pkey = baos.toByteArray();

    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pkey);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey mypk = keyFactory.generatePublic(keySpec);

    Signature instance = Signature.getInstance("SHA1withRSA");
    instance.initVerify(mypk);
    instance.update(email.getBytes());

    //BASE64Decoder decoder = new BASE64Decoder();
    //byte[] decodedBytes = decoder.decodeBuffer(licenseCode);

    return instance.verify(DatatypeConverter.parseBase64Binary(licenseCode));
}

From source file:org.apache.usergrid.security.sso.ApigeeSSO2Provider.java

public PublicKey getPublicKey(String keyUrl) {

    if (keyUrl != null && !keyUrl.isEmpty()) {
        try {/*from   w w  w . j ava  2s.  c  om*/
            Map<String, Object> publicKey = client.target(keyUrl).request().get(Map.class);
            String ssoPublicKey = publicKey.get(RESPONSE_PUBLICKEY_VALUE).toString().split("----\n")[1]
                    .split("\n---")[0];
            byte[] publicBytes = decodeBase64(ssoPublicKey);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey pubKey = keyFactory.generatePublic(keySpec);
            return pubKey;
        } catch (Exception e) {
            throw new IllegalArgumentException("error getting public key");
        }
    }

    return null;
}

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

/**
 * <p>//  ww w . j a  v a  2 s .  c  om
 * ??
 * </p>
 * 
 * @param data ?
 * @param publicKey (BASE64?)
 * @param sign ??
 * 
 * @return boolean
 * @throws Exception Exception
 * 
 */
public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
    byte[] keyBytes = Base64.decodeBase64(publicKey);
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    PublicKey publicK = keyFactory.generatePublic(keySpec);
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initVerify(publicK);
    signature.update(data);
    return signature.verify(Base64.decodeBase64(sign));
}

From source file:hudson.model.UsageStatistics.java

private Cipher getCipher() {
    try {/*w  w  w.ja v a2 s  . c om*/
        if (key == null) {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            key = keyFactory.generatePublic(new X509EncodedKeySpec(Util.fromHexString(keyImage)));
        }

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher;
    } catch (GeneralSecurityException e) {
        throw new Error(e); // impossible
    }
}