use of java.security.spec.X509EncodedKeySpec in project xabber-android by redsolution.
the class AccountTable method getKeyPair.
static KeyPair getKeyPair(Cursor cursor) {
byte[] publicKeyBytes = cursor.getBlob(cursor.getColumnIndex(Fields.PUBLIC_KEY));
byte[] privateKeyBytes = cursor.getBlob(cursor.getColumnIndex(Fields.PRIVATE_KEY));
if (privateKeyBytes == null || publicKeyBytes == null) {
return null;
}
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
PublicKey publicKey;
PrivateKey privateKey;
KeyFactory keyFactory;
try {
keyFactory = KeyFactory.getInstance("DSA");
publicKey = keyFactory.generatePublic(publicKeySpec);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new RuntimeException(e);
}
return new KeyPair(publicKey, privateKey);
}
use of java.security.spec.X509EncodedKeySpec in project robovm by robovm.
the class OpenSSLECKeyFactory method engineTranslateKey.
@Override
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
if (key == null) {
throw new InvalidKeyException("key == null");
}
if ((key instanceof OpenSSLECPublicKey) || (key instanceof OpenSSLECPrivateKey)) {
return key;
} else if (key instanceof ECPublicKey) {
ECPublicKey ecKey = (ECPublicKey) key;
ECPoint w = ecKey.getW();
ECParameterSpec params = ecKey.getParams();
try {
return engineGeneratePublic(new ECPublicKeySpec(w, params));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else if (key instanceof ECPrivateKey) {
ECPrivateKey ecKey = (ECPrivateKey) key;
BigInteger s = ecKey.getS();
ECParameterSpec params = ecKey.getParams();
try {
return engineGeneratePrivate(new ECPrivateKeySpec(s, params));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else if ((key instanceof PrivateKey) && ("PKCS#8".equals(key.getFormat()))) {
byte[] encoded = key.getEncoded();
if (encoded == null) {
throw new InvalidKeyException("Key does not support encoding");
}
try {
return engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else if ((key instanceof PublicKey) && ("X.509".equals(key.getFormat()))) {
byte[] encoded = key.getEncoded();
if (encoded == null) {
throw new InvalidKeyException("Key does not support encoding");
}
try {
return engineGeneratePublic(new X509EncodedKeySpec(encoded));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else {
throw new InvalidKeyException("Key must be EC public or private key; was " + key.getClass().getName());
}
}
use of java.security.spec.X509EncodedKeySpec in project robovm by robovm.
the class OpenSSLKey method getPublicKey.
static PublicKey getPublicKey(X509EncodedKeySpec keySpec, int type) throws InvalidKeySpecException {
X509EncodedKeySpec x509KeySpec = (X509EncodedKeySpec) keySpec;
final OpenSSLKey key;
try {
key = new OpenSSLKey(NativeCrypto.d2i_PUBKEY(x509KeySpec.getEncoded()));
} catch (Exception e) {
throw new InvalidKeySpecException(e);
}
if (NativeCrypto.EVP_PKEY_type(key.getPkeyContext()) != type) {
throw new InvalidKeySpecException("Unexpected key type");
}
try {
return key.getPublicKey();
} catch (NoSuchAlgorithmException e) {
throw new InvalidKeySpecException(e);
}
}
use of java.security.spec.X509EncodedKeySpec in project cas by apereo.
the class PublicKeyFactoryBean method createInstance.
@Override
protected PublicKey createInstance() throws Exception {
LOGGER.debug("Creating public key instance from [{}] using [{}]", this.resource.getFilename(), this.algorithm);
try (InputStream pubKey = this.resource.getInputStream()) {
final byte[] bytes = new byte[(int) this.resource.contentLength()];
pubKey.read(bytes);
final X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(bytes);
final KeyFactory factory = KeyFactory.getInstance(this.algorithm);
return factory.generatePublic(pubSpec);
}
}
use of java.security.spec.X509EncodedKeySpec in project dq-easy-cloud by dq-open-cloud.
the class DqRSAUtils method verify.
/**
* RSA验签名检查
* @param content 待签名数据
* @param sign 签名值
* @param publicKey 公钥
* @param signAlgorithms 签名算法
* @param characterEncoding 编码格式
* @return 布尔值
*/
public static boolean verify(String content, String sign, String publicKey, String signAlgorithms, String characterEncoding) {
try {
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
byte[] encodedKey = DqBase64Utils.decode(publicKey);
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
java.security.Signature signature = java.security.Signature.getInstance(signAlgorithms);
signature.initVerify(pubKey);
signature.update(content.getBytes(characterEncoding));
return signature.verify(DqBase64Utils.decode(sign));
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Aggregations