use of java.security.spec.X509EncodedKeySpec in project OpenAttestation by OpenAttestation.
the class RsaUtil method decodeDerPublicKey.
public static PublicKey decodeDerPublicKey(byte[] publicKeyBytes) throws CryptographyException {
try {
// throws NoSuchAlgorithmException
KeyFactory factory = KeyFactory.getInstance("RSA");
// throws InvalidKeySpecException
PublicKey publicKey = factory.generatePublic(new X509EncodedKeySpec(publicKeyBytes));
return publicKey;
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new CryptographyException(e);
}
}
use of java.security.spec.X509EncodedKeySpec in project OpenAttestation by OpenAttestation.
the class X509Util method decodeDerPublicKey.
/**
* @deprecated use decodeDerPublicKey in RsaUtil instead
* @param publicKeyBytes
* @return
* @throws CryptographyException
*/
public static PublicKey decodeDerPublicKey(byte[] publicKeyBytes) throws CryptographyException {
try {
// throws NoSuchAlgorithmException
KeyFactory factory = KeyFactory.getInstance("RSA");
// throws InvalidKeySpecException
PublicKey publicKey = factory.generatePublic(new X509EncodedKeySpec(publicKeyBytes));
return publicKey;
} catch (Exception e) {
throw new CryptographyException(e);
}
}
use of java.security.spec.X509EncodedKeySpec in project SeriesGuide by UweTrottmann.
the class Security 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
*/
public 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) {
throw new RuntimeException(e);
} catch (InvalidKeySpecException e) {
Timber.e("Invalid key specification.");
throw new IllegalArgumentException(e);
} catch (Base64DecoderException e) {
Timber.e("Base64 decoding failed.");
throw new IllegalArgumentException(e);
}
}
use of java.security.spec.X509EncodedKeySpec in project platformlayer by platformlayer.
the class RsaUtils method deserializePublicKey.
public static PublicKey deserializePublicKey(byte[] keyData) {
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(keyData);
KeyFactory keyFactory = getKeyFactory();
PublicKey pubKey;
try {
pubKey = keyFactory.generatePublic(pubKeySpec);
} catch (InvalidKeySpecException e) {
throw new IllegalArgumentException("Error deserializing public key", e);
}
return pubKey;
}
use of java.security.spec.X509EncodedKeySpec in project xabber-android by redsolution.
the class AccountTable method write.
public long write(Long id, AccountItem accountItem) {
ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
ContentValues values = new ContentValues();
values.put(Fields.PROTOCOL, connectionSettings.getProtocol().name());
values.put(Fields.CUSTOM, connectionSettings.isCustomHostAndPort() ? 1 : 0);
values.put(Fields.HOST, connectionSettings.getHost());
values.put(Fields.PORT, connectionSettings.getPort());
values.put(Fields.SERVER_NAME, connectionSettings.getServerName());
values.put(Fields.USER_NAME, connectionSettings.getUserName());
String password = connectionSettings.getPassword();
if (!accountItem.isStorePassword()) {
password = AccountItem.UNDEFINED_PASSWORD;
}
values.put(Fields.PASSWORD, password);
values.put(Fields.RESOURCE, connectionSettings.getResource());
values.put(Fields.COLOR_INDEX, accountItem.getColorIndex());
values.put(Fields.PRIORITY, accountItem.getPriority());
values.put(Fields.STATUS_MODE, accountItem.getRawStatusMode().ordinal());
values.put(Fields.STATUS_TEXT, accountItem.getStatusText());
values.put(Fields.ENABLED, accountItem.isEnabled() ? 1 : 0);
values.put(Fields.SASL_ENABLED, connectionSettings.isSaslEnabled() ? 1 : 0);
values.put(Fields.TLS_MODE, connectionSettings.getTlsMode().ordinal());
values.put(Fields.COMPRESSION, connectionSettings.useCompression() ? 1 : 0);
values.put(Fields.PROXY_TYPE, connectionSettings.getProxyType().ordinal());
values.put(Fields.PROXY_HOST, connectionSettings.getProxyHost());
values.put(Fields.PROXY_PORT, connectionSettings.getProxyPort());
values.put(Fields.PROXY_USER, connectionSettings.getProxyUser());
values.put(Fields.PROXY_PASSWORD, connectionSettings.getProxyPassword());
values.put(Fields.SYNCABLE, accountItem.isSyncable() ? 1 : 0);
values.put(Fields.STORE_PASSWORD, accountItem.isStorePassword() ? 1 : 0);
final KeyPair keyPair = accountItem.getKeyPair();
if (keyPair == null) {
values.putNull(Fields.PUBLIC_KEY);
values.putNull(Fields.PRIVATE_KEY);
} else {
PublicKey publicKey = keyPair.getPublic();
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
PrivateKey privateKey = keyPair.getPrivate();
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
byte[] publicKeyBytes = x509EncodedKeySpec.getEncoded();
byte[] privateKeyBytes = pkcs8EncodedKeySpec.getEncoded();
values.put(Fields.PUBLIC_KEY, publicKeyBytes);
values.put(Fields.PRIVATE_KEY, privateKeyBytes);
}
if (accountItem.getLastSync() == null) {
values.putNull(Fields.LAST_SYNC);
} else {
values.put(Fields.LAST_SYNC, accountItem.getLastSync().getTime());
}
values.put(Fields.ARCHIVE_MODE, accountItem.getArchiveMode().ordinal());
SQLiteDatabase db = databaseManager.getWritableDatabase();
if (id == null) {
return db.insert(NAME, Fields.USER_NAME, values);
}
db.update(NAME, values, Fields._ID + " = ?", new String[] { String.valueOf(id) });
return id;
}
Aggregations