Search in sources :

Example 86 with RSAPublicKey

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

the class JWTAuthContextInfoProvider 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)

Example 87 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project carat by amplab.

the class SamplingLibrary method getSignatures.

public static List<String> getSignatures(PackageInfo pak) {
    List<String> sigList = new LinkedList<String>();
    String[] pmInfos = pak.requestedPermissions;
    if (pmInfos != null) {
        byte[] bytes = getPermissionBytes(pmInfos);
        String hexB = convertToHex(bytes);
        sigList.add(hexB);
    }
    Signature[] sigs = pak.signatures;
    for (Signature s : sigs) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(s.toByteArray());
            byte[] dig = md.digest();
            // Add SHA-1
            sigList.add(convertToHex(dig));
            CertificateFactory fac = CertificateFactory.getInstance("X.509");
            if (fac == null)
                continue;
            X509Certificate cert = (X509Certificate) fac.generateCertificate(new ByteArrayInputStream(s.toByteArray()));
            if (cert == null)
                continue;
            PublicKey pkPublic = cert.getPublicKey();
            if (pkPublic == null)
                continue;
            String al = pkPublic.getAlgorithm();
            if (al.equals("RSA")) {
                md = MessageDigest.getInstance("SHA-256");
                RSAPublicKey rsa = (RSAPublicKey) pkPublic;
                byte[] data = rsa.getModulus().toByteArray();
                if (data[0] == 0) {
                    byte[] copy = new byte[data.length - 1];
                    System.arraycopy(data, 1, copy, 0, data.length - 1);
                    md.update(copy);
                } else
                    md.update(data);
                dig = md.digest();
                // Add SHA-256 of modulus
                sigList.add(convertToHex(dig));
            } else if (al.equals("DSA")) {
                DSAPublicKey dsa = (DSAPublicKey) pkPublic;
                md = MessageDigest.getInstance("SHA-256");
                byte[] data = dsa.getY().toByteArray();
                if (data[0] == 0) {
                    byte[] copy = new byte[data.length - 1];
                    System.arraycopy(data, 1, copy, 0, data.length - 1);
                    md.update(copy);
                } else
                    md.update(data);
                dig = md.digest();
                // Add SHA-256 of public key (DSA)
                sigList.add(convertToHex(dig));
            } else {
                Log.e("SamplingLibrary", "Weird algorithm: " + al + " for " + pak.packageName);
            }
        } catch (NoSuchAlgorithmException e) {
        // Do nothing
        } catch (CertificateException e) {
        // Do nothing
        }
    }
    return sigList;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateFactory(java.security.cert.CertificateFactory) LinkedList(java.util.LinkedList) X509Certificate(java.security.cert.X509Certificate) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Signature(android.content.pm.Signature) MessageDigest(java.security.MessageDigest)

Example 88 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project gerrit by GerritCodeReview.

the class SshUtil method toOpenSshPublicKey.

/**
 * Convert an RFC 4716 style key to an OpenSSH style key.
 *
 * @param keyStr the key string to convert.
 * @return {@code keyStr} if conversion failed; otherwise the converted key, in OpenSSH key
 *     format.
 */
public static String toOpenSshPublicKey(String keyStr) {
    try {
        final StringBuilder strBuf = new StringBuilder();
        final BufferedReader br = new BufferedReader(new StringReader(keyStr));
        // BEGIN SSH2 line...
        String line = br.readLine();
        if (line == null || !line.equals("---- BEGIN SSH2 PUBLIC KEY ----")) {
            return keyStr;
        }
        while ((line = br.readLine()) != null) {
            if (line.indexOf(':') == -1) {
                strBuf.append(line);
                break;
            }
        }
        while ((line = br.readLine()) != null) {
            if (line.startsWith("---- ")) {
                break;
            }
            strBuf.append(line);
        }
        final PublicKey key = new ByteArrayBuffer(BaseEncoding.base64().decode(strBuf.toString())).getRawPublicKey();
        if (key instanceof RSAPublicKey) {
            strBuf.insert(0, KeyPairProvider.SSH_RSA + " ");
        } else if (key instanceof DSAPublicKey) {
            strBuf.insert(0, KeyPairProvider.SSH_DSS + " ");
        } else {
            return keyStr;
        }
        strBuf.append(' ');
        strBuf.append("converted-key");
        return strBuf.toString();
    } catch (IOException | RuntimeException e) {
        return keyStr;
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException) ByteArrayBuffer(org.apache.sshd.common.util.buffer.ByteArrayBuffer) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 89 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project tech by ffyyhh995511.

the class RASUtil method initKey.

/**
 * 初始化
 * @return
 * @throws Exception
 */
public static Map<String, Object> initKey() throws Exception {
    // 获得对象 KeyPairGenerator 参数 RSA 1024个字节
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    keyPairGen.initialize(1024);
    // 通过对象 KeyPairGenerator 获取对象KeyPair
    KeyPair keyPair = keyPairGen.generateKeyPair();
    // 通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKey
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    // 公私钥对象存入map中
    Map<String, Object> keyMap = new HashMap<String, Object>(2);
    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);
    return keyMap;
}
Also used : KeyPair(java.security.KeyPair) RSAPublicKey(java.security.interfaces.RSAPublicKey) HashMap(java.util.HashMap) KeyPairGenerator(java.security.KeyPairGenerator) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 90 with RSAPublicKey

use of java.security.interfaces.RSAPublicKey in project tech by ffyyhh995511.

the class RASUtil method loadPublicKeyByStr.

/**
 * 公钥字符串转换为公钥对象
 * @param publicKeyStr
 * @return
 * @throws Exception
 */
public static RSAPublicKey loadPublicKeyByStr(String publicKeyStr) throws Exception {
    try {
        byte[] buffer = decryptBASE64(publicKeyStr);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
        return (RSAPublicKey) keyFactory.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("无此算法");
    } catch (InvalidKeySpecException e) {
        throw new Exception("公钥非法");
    } catch (NullPointerException e) {
        throw new Exception("公钥数据为空");
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

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