Example usage for java.security.spec RSAPublicKeySpec RSAPublicKeySpec

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

Introduction

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

Prototype

public RSAPublicKeySpec(BigInteger modulus, BigInteger publicExponent) 

Source Link

Document

Creates a new RSAPublicKeySpec.

Usage

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 www  . j  av a 2 s.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:me.brjannc.plugins.sushi.AuthorizedKeysDecoder.java

public PublicKey decodePublicKey(String keyLine) throws Exception {
    bytes = null;/*from   ww  w  . j  a v a2s .co  m*/
    pos = 0;

    // look for the Base64 encoded part of the line to decode
    // both ssh-rsa and ssh-dss begin with "AAAA" due to the length bytes
    for (String part : keyLine.split(" ")) {
        if (part.startsWith("AAAA")) {
            bytes = Base64.decodeBase64(part);
            break;
        }
    }
    if (bytes == null) {
        throw new IllegalArgumentException("no Base64 part to decode");
    }

    String type = decodeType();
    if (type.equals("ssh-rsa")) {
        BigInteger e = decodeBigInt();
        BigInteger m = decodeBigInt();
        RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e);
        return KeyFactory.getInstance("RSA").generatePublic(spec);
    } else if (type.equals("ssh-dss")) {
        BigInteger p = decodeBigInt();
        BigInteger q = decodeBigInt();
        BigInteger g = decodeBigInt();
        BigInteger y = decodeBigInt();
        DSAPublicKeySpec spec = new DSAPublicKeySpec(y, p, q, g);
        return KeyFactory.getInstance("DSA").generatePublic(spec);
    } else {
        throw new IllegalArgumentException("unknown type " + type);
    }
}

From source file:com.tasktop.c2c.server.internal.profile.crypto.OpenSSHPublicKeyReader.java

public SshPublicKey readPublicKey(String keySpec) {
    keySpec = keySpec.trim();//from  ww w .j  av  a 2  s .  c o  m
    String[] parts = keySpec.split(" ");
    if (parts.length >= 2) {
        String algorithm = parts[0];
        String base64Data = parts[1];
        if (algorithm.equals("ssh-rsa")) {
            SshPublicKey sshPublicKey = new SshPublicKey();
            sshPublicKey.setAlgorithm("RSA");
            byte[] decodedData = Base64.decodeBase64(StringUtils.getBytesUtf8(base64Data));

            Rfc4253Reader reader = new Rfc4253Reader(decodedData, 0);

            try {
                byte[] format = reader.readBytes();
                byte[] exponent = reader.readBytes();
                byte[] modulus = reader.readBytes();

                if (Arrays.equals(FORMAT, format)) {
                    BigInteger exp = new BigInteger(exponent);
                    BigInteger mod = new BigInteger(modulus);
                    RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(mod, exp);
                    try {
                        PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(rsaPublicKeySpec);
                        sshPublicKey.setKeyData(publicKey.getEncoded());
                        return sshPublicKey;
                    } catch (InvalidKeySpecException t) {
                        getLogger().warn("Invalid key spec: " + t.getMessage(), t);
                    } catch (NoSuchAlgorithmException t) {
                        getLogger().warn("Invalid algorithm: " + t.getMessage(), t);
                    }
                }

            } catch (IOException e) {
                // ignore
            }
        }
    }
    return null;
}

From source file:license.TestWakeLicense.java

/**
 * ?//www  . ja  va2s . co  m
 * @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();
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        return fact.generatePublic(keySpec);
    } finally {
        oin.close();
    }
}

From source file:com.jaspersoft.jasperserver.jaxrs.client.core.EncryptionUtils.java

private static PublicKey getPublicKey(String n, String e) throws Exception {
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    int radix = 16;
    BigInteger modulus = new BigInteger(n, radix);
    BigInteger publicExponent = new BigInteger(e, radix);
    RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);
    return keyFactory.generatePublic(publicKeySpec);
}

From source file:de.mpg.escidoc.services.aa.crypto.RSAEncoder.java

public static Key readKeyFromFile(String keyFileName, boolean publ) throws Exception {
    InputStream in = ResourceUtil.getResourceAsStream(keyFileName, RSAEncoder.class.getClassLoader());
    ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
    try {//from  w  w  w  . ja v a  2  s .  c o  m
        BigInteger m = (BigInteger) oin.readObject();
        BigInteger e = (BigInteger) oin.readObject();
        KeySpec keySpec;
        if (publ) {
            keySpec = new RSAPublicKeySpec(m, e);
        } else {
            keySpec = new RSAPrivateKeySpec(m, e);
        }
        KeyFactory fact = KeyFactory.getInstance("RSA");
        if (publ) {
            PublicKey pubKey = fact.generatePublic(keySpec);
            return pubKey;
        } else {
            PrivateKey privKey = fact.generatePrivate(keySpec);
            return privKey;
        }
    } catch (Exception e) {
        throw new RuntimeException("Error reading key from file", e);
    } finally {
        oin.close();
    }
}

From source file:org.infoscoop.util.RSAKeyManager.java

public RSAKeyManager(BigInteger modulus, BigInteger exponent)
        throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {

    if (log.isInfoEnabled())
        log.info("### Using RSAProvider is " + keyFactory.getProvider().getClass());

    RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(modulus, exponent);
    privateKey = (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);

    RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, RSAKeyGenParameterSpec.F4);
    publicKey = (RSAPublicKey) keyFactory.generatePublic(publicKeySpec);

}

From source file:com.github.woki.payments.adyen.action.CSEUtil.java

public static Cipher rsaCipher(final String cseKeyText) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidKeyException, InvalidKeySpecException, IllegalArgumentException {
    String[] cseKeyParts = cseKeyText.split("\\|");
    if (cseKeyParts.length != 2) {
        throw new InvalidKeyException("Invalid CSE Key: " + cseKeyText);
    }//from w w w . j  av a  2  s  .  c o m
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    BigInteger keyComponent1, keyComponent2;
    try {
        keyComponent1 = new BigInteger(cseKeyParts[1].toLowerCase(Locale.getDefault()), 16);
        keyComponent2 = new BigInteger(cseKeyParts[0].toLowerCase(Locale.getDefault()), 16);
    } catch (NumberFormatException e) {
        throw new InvalidKeyException("Invalid CSE Key: " + cseKeyText);
    }
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(keyComponent1, keyComponent2);
    PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

    Cipher result = Cipher.getInstance("RSA/None/PKCS1Padding");
    result.init(Cipher.ENCRYPT_MODE, pubKey);
    return result;
}

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

/**
 * Returns an encryption cipher that is based on the public encryption key file located on the
 * application's classpath.//  w  ww. jav  a2 s .  c om
 * 
 * @return Cipher
 * @throws GeneralSecurityException
 *             thrown if encryption key is not valid
 * @throws IOException
 *             thrown if the contents of the public key file cannot be loaded
 */
private static Cipher loadEncryptionCipher() throws GeneralSecurityException, IOException {
    BigInteger[] keyComponents = loadKeyFile(PUBLIC_KEYFILE);
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(keyComponents[0], keyComponents[1]);
    KeyFactory factory = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
    PublicKey publicKey = factory.generatePublic(keySpec);
    Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);

    cipher.init(Cipher.PUBLIC_KEY, publicKey);
    return cipher;
}

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

/**
 * RSA???/*from  w  w w.  j av a2 s. c om*/
 * <dl>
 * <dt>?
 * <dd>RSA?????????????
 * </dl>
 * @param modulus 
 * @param exponent ??
 * @return RSA?
 */
public static RSAPublicKey createPublicKey(final BigInteger modulus, final BigInteger exponent) {
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (RSAPublicKey) keyFactory.generatePublic(new RSAPublicKeySpec(modulus, exponent));
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        throw new StandardRuntimeException(e);
    }
}