use of java.security.spec.KeySpec in project j2objc by google.
the class SubjectPublicKeyInfo method getPublicKey.
/**
* Returns the PublicKey corresponding to this SubjectPublicKeyInfo
* instance.
*/
public PublicKey getPublicKey() {
if (publicKey == null) {
final byte[] encoded = getEncoded();
final KeySpec keySpec = new X509EncodedKeySpec(encoded);
/* Try using the algorithm name first. */
final String algName = algorithmID.getAlgorithmName();
publicKey = generateKeyForAlgorithm(keySpec, algName);
/*
* Fall back to using the algorithm OID if it's not the same as the
* algorithm name.
*/
final String algOid = algorithmID.getAlgorithm();
if (publicKey == null && !algOid.equals(algName)) {
publicKey = generateKeyForAlgorithm(keySpec, algOid);
}
/*
* Encode this as an X.509 public key since we didn't have any
* KeyFactory that could handle this algorithm name or OID. Perhaps
* the thing that's using this can decode it.
*/
if (publicKey == null) {
publicKey = new X509PublicKey(algOid, encoded, subjectPublicKey);
}
}
return publicKey;
}
use of java.security.spec.KeySpec in project bigbluebutton by bigbluebutton.
the class RSA_SHA1 method getPublicKeyFromPem.
private PublicKey getPublicKeyFromPem(String pem) throws GeneralSecurityException, IOException {
InputStream stream = new ByteArrayInputStream(pem.getBytes("UTF-8"));
PEMReader reader = new PEMReader(stream);
byte[] bytes = reader.getDerBytes();
PublicKey pubKey;
if (PEMReader.PUBLIC_X509_MARKER.equals(reader.getBeginMarker())) {
KeySpec keySpec = new X509EncodedKeySpec(bytes);
KeyFactory fac = KeyFactory.getInstance("RSA");
pubKey = fac.generatePublic(keySpec);
} else if (PEMReader.CERTIFICATE_X509_MARKER.equals(reader.getBeginMarker())) {
pubKey = getPublicKeyFromDerCert(bytes);
} else {
throw new IOException("Invalid PEM fileL: Unknown marker for " + " public key or cert " + reader.getBeginMarker());
}
return pubKey;
}
use of java.security.spec.KeySpec in project bigbluebutton by bigbluebutton.
the class RSA_SHA1 method getPrivateKeyFromPem.
private PrivateKey getPrivateKeyFromPem(String pem) throws GeneralSecurityException, IOException {
InputStream stream = new ByteArrayInputStream(pem.getBytes("UTF-8"));
PEMReader reader = new PEMReader(stream);
byte[] bytes = reader.getDerBytes();
KeySpec keySpec;
if (PEMReader.PRIVATE_PKCS1_MARKER.equals(reader.getBeginMarker())) {
keySpec = (new PKCS1EncodedKeySpec(bytes)).getKeySpec();
} else if (PEMReader.PRIVATE_PKCS8_MARKER.equals(reader.getBeginMarker())) {
keySpec = new PKCS8EncodedKeySpec(bytes);
} else {
throw new IOException("Invalid PEM file: Unknown marker " + "for private key " + reader.getBeginMarker());
}
KeyFactory fac = KeyFactory.getInstance("RSA");
return fac.generatePrivate(keySpec);
}
use of java.security.spec.KeySpec in project pulsar by yahoo.
the class SecurityUtility method loadPrivateKeyFromPemFile.
public static PrivateKey loadPrivateKeyFromPemFile(String keyFilePath) throws KeyManagementException {
PrivateKey privateKey = null;
if (keyFilePath == null || keyFilePath.isEmpty()) {
return privateKey;
}
try (BufferedReader reader = new BufferedReader(new FileReader(keyFilePath))) {
StringBuilder sb = new StringBuilder();
String previousLine = "";
String currentLine = null;
// Skip the first line (-----BEGIN RSA PRIVATE KEY-----)
reader.readLine();
while ((currentLine = reader.readLine()) != null) {
sb.append(previousLine);
previousLine = currentLine;
}
// Skip the last line (-----END RSA PRIVATE KEY-----)
KeyFactory kf = KeyFactory.getInstance("RSA");
KeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(sb.toString()));
privateKey = kf.generatePrivate(keySpec);
} catch (GeneralSecurityException | IOException e) {
throw new KeyManagementException("Private key loading error", e);
}
return privateKey;
}
use of java.security.spec.KeySpec in project hutool by looly.
the class SecureUtil method generateDESKey.
/**
* 生成 {@link SecretKey}
*
* @param algorithm DES算法,包括DES、DESede等
* @param key 密钥
* @return {@link SecretKey}
*/
public static SecretKey generateDESKey(String algorithm, byte[] key) {
if (StrUtil.isBlank(algorithm) || false == algorithm.startsWith("DES")) {
throw new CryptoException("Algorithm [{}] is not a DES algorithm!");
}
SecretKey secretKey = null;
if (null == key) {
secretKey = generateKey(algorithm);
} else {
KeySpec keySpec;
try {
if (algorithm.startsWith("DESede")) {
// DESede兼容
keySpec = new DESedeKeySpec(key);
} else {
keySpec = new DESKeySpec(key);
}
} catch (InvalidKeyException e) {
throw new CryptoException(e);
}
secretKey = generateKey(algorithm, keySpec);
}
return secretKey;
}
Aggregations