Search in sources :

Example 81 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project Gradle-demo by Arisono.

the class RSAUtils method generateKeyBytes.

/**
 * 生成密钥对。注意这里是生成密钥对KeyPair,再由密钥对获取公私钥
 * 生成RSA的公钥和私钥
 * @return
 */
public static Map<String, byte[]> generateKeyBytes() {
    try {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        keyPairGenerator.initialize(KEY_SIZE);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        Map<String, byte[]> keyMap = new HashMap<String, byte[]>();
        keyMap.put(PUBLIC_KEY, publicKey.getEncoded());
        keyMap.put(PRIVATE_KEY, privateKey.getEncoded());
        return keyMap;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : KeyPair(java.security.KeyPair) RSAPublicKey(java.security.interfaces.RSAPublicKey) HashMap(java.util.HashMap) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 82 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project zeppelin by apache.

the class KnoxJwtRealm method validateSignature.

protected boolean validateSignature(SignedJWT jwtToken) {
    boolean valid = false;
    if (JWSObject.State.SIGNED == jwtToken.getState()) {
        if (jwtToken.getSignature() != null) {
            try {
                RSAPublicKey publicKey = parseRSAPublicKey(publicKeyPath);
                JWSVerifier verifier = new RSASSAVerifier(publicKey);
                if (jwtToken.verify(verifier)) {
                    valid = true;
                }
            } catch (Exception e) {
                LOGGER.info("Exception in validateSignature", e);
            }
        }
    }
    return valid;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSASSAVerifier(com.nimbusds.jose.crypto.RSASSAVerifier) JWSVerifier(com.nimbusds.jose.JWSVerifier) ServletException(javax.servlet.ServletException) ParseException(java.text.ParseException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 83 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project incubator-atlas by apache.

the class AtlasKnoxSSOAuthenticationFilter method parseRSAPublicKey.

/*
     * public static RSAPublicKey getPublicKeyFromFile(String filePath) throws
	 * IOException, CertificateException {
	 * FileUtils.readFileToString(new File(filePath));
	 * getPublicKeyFromString(pemString); }
	 */
public static RSAPublicKey parseRSAPublicKey(String pem) throws CertificateException, UnsupportedEncodingException, ServletException {
    String PEM_HEADER = "-----BEGIN CERTIFICATE-----\n";
    String PEM_FOOTER = "\n-----END CERTIFICATE-----";
    String fullPem = PEM_HEADER + pem + PEM_FOOTER;
    PublicKey key = null;
    try {
        CertificateFactory fact = CertificateFactory.getInstance("X.509");
        ByteArrayInputStream is = new ByteArrayInputStream(fullPem.getBytes("UTF8"));
        X509Certificate cer = (X509Certificate) fact.generateCertificate(is);
        key = cer.getPublicKey();
    } catch (CertificateException ce) {
        String message = null;
        if (pem.startsWith(PEM_HEADER)) {
            message = "CertificateException - be sure not to include PEM header " + "and footer in the PEM configuration element.";
        } else {
            message = "CertificateException - PEM may be corrupt";
        }
        throw new ServletException(message, ce);
    } catch (UnsupportedEncodingException uee) {
        throw new ServletException(uee);
    }
    return (RSAPublicKey) key;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 84 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project cloudstack by apache.

the class RSAHelper method encryptWithSSHPublicKey.

public static String encryptWithSSHPublicKey(String sshPublicKey, String content) {
    String returnString = null;
    try {
        RSAPublicKey publicKey = readKey(sshPublicKey);
        Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", BouncyCastleProvider.PROVIDER_NAME);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey, new SecureRandom());
        byte[] encrypted = cipher.doFinal(content.getBytes());
        returnString = Base64.encodeBase64String(encrypted);
    } catch (Exception e) {
        s_logger.info("[ignored]" + "error during public key encryption: " + e.getLocalizedMessage());
    }
    return returnString;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) SecureRandom(java.security.SecureRandom) Cipher(javax.crypto.Cipher) IOException(java.io.IOException)

Example 85 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project tomee by apache.

the class MoviesMPJWTConfigurationProvider method getOptionalContextInfo.

@Produces
Optional<JWTAuthConfiguration> getOptionalContextInfo() throws NoSuchAlgorithmException, InvalidKeySpecException {
    final String pemEncoded = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlivFI8qB4D0y2jy0CfEq" + "Fyy46R0o7S8TKpsx5xbHKoU1VWg6QkQm+ntyIv1p4kE1sPEQO73+HY8+Bzs75XwR" + "TYL1BmR1w8J5hmjVWjc6R2BTBGAYRPFRhor3kpM6ni2SPmNNhurEAHw7TaqszP5e" + "UF/F9+KEBWkwVta+PZ37bwqSE4sCb1soZFrVz/UT/LF4tYpuVYt3YbqToZ3pZOZ9" + "AX2o1GCG3xwOjkc4x0W7ezbQZdC9iftPxVHR8irOijJRRjcPDtA6vPKpzLl6CyYn" + "sIYPd99ltwxTHjr3npfv/3Lw50bAkbT4HeLFxTx4flEoZLKO/g0bAoV2uqBhkA9x" + "nQIDAQAB";
    byte[] encodedBytes = Base64.getDecoder().decode(pemEncoded);
    final X509EncodedKeySpec spec = new X509EncodedKeySpec(encodedBytes);
    final KeyFactory kf = KeyFactory.getInstance("RSA");
    final RSAPublicKey pk = (RSAPublicKey) kf.generatePublic(spec);
    return Optional.of(JWTAuthConfiguration.authConfiguration(pk, "https://server.example.com", false));
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory) Produces(javax.enterprise.inject.Produces)

Aggregations

RSAPublicKey (java.security.interfaces.RSAPublicKey)240 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)65 PublicKey (java.security.PublicKey)50 KeyPair (java.security.KeyPair)48 BigInteger (java.math.BigInteger)44 IOException (java.io.IOException)39 KeyPairGenerator (java.security.KeyPairGenerator)39 KeyFactory (java.security.KeyFactory)37 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)31 ECPublicKey (java.security.interfaces.ECPublicKey)30 X509Certificate (java.security.cert.X509Certificate)29 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)28 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)27 Test (org.junit.Test)27 PrivateKey (java.security.PrivateKey)26 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)26 CertificateException (java.security.cert.CertificateException)24 DSAPublicKey (java.security.interfaces.DSAPublicKey)24 InvalidKeyException (java.security.InvalidKeyException)22 ByteArrayInputStream (java.io.ByteArrayInputStream)21