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));
}
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;
}
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;
}
}
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;
}
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("公钥数据为空");
}
}
Aggregations