use of java.security.spec.X509EncodedKeySpec in project xabber-android by redsolution.
the class AccountRealm method setKeyPair.
public void setKeyPair(KeyPair keyPair) {
if (keyPair == null) {
publicKeyBytes = null;
privateKeyBytes = null;
} else {
PublicKey publicKey = keyPair.getPublic();
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
PrivateKey privateKey = keyPair.getPrivate();
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
this.publicKeyBytes = x509EncodedKeySpec.getEncoded();
this.privateKeyBytes = pkcs8EncodedKeySpec.getEncoded();
}
}
use of java.security.spec.X509EncodedKeySpec 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("公钥数据为空");
}
}
use of java.security.spec.X509EncodedKeySpec in project Klyph by jonathangerbaud.
the class LicenseChecker method generatePublicKey.
/**
* Generates a PublicKey instance from a string containing the
* Base64-encoded public key.
*
* @param encodedPublicKey Base64-encoded public key
* @throws IllegalArgumentException if encodedPublicKey is invalid
*/
private static PublicKey generatePublicKey(String encodedPublicKey) {
try {
byte[] decodedKey = Base64.decode(encodedPublicKey);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
} catch (NoSuchAlgorithmException e) {
// This won't happen in an Android-compatible environment.
throw new RuntimeException(e);
} catch (Base64DecoderException e) {
Log.e(TAG, "Could not decode from Base64.");
throw new IllegalArgumentException(e);
} catch (InvalidKeySpecException e) {
Log.e(TAG, "Invalid key specification.");
throw new IllegalArgumentException(e);
}
}
use of java.security.spec.X509EncodedKeySpec in project JGroups by belaban.
the class ASYM_ENCRYPT method generatePubKey.
/**
* Used to reconstitute public key sent in byte form from peer
*/
protected PublicKey generatePubKey(byte[] encodedKey) {
PublicKey pubKey = null;
try {
KeyFactory KeyFac = KeyFactory.getInstance(getAlgorithm(asym_algorithm));
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(encodedKey);
pubKey = KeyFac.generatePublic(x509KeySpec);
} catch (Exception e) {
e.printStackTrace();
}
return pubKey;
}
use of java.security.spec.X509EncodedKeySpec in project bnd by bndtools.
the class Settings method initKeys.
/*
* Initialize the keys.
*/
private void initKeys() throws Exception {
check();
if (privateKey != null)
return;
if (data.id == null || data.secret == null) {
generate();
} else {
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(data.secret);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
privateKey = keyFactory.generatePrivate(privateKeySpec);
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(data.id);
publicKey = keyFactory.generatePublic(publicKeySpec);
}
}
Aggregations