use of java.security.spec.InvalidParameterSpecException in project XobotOS by xamarin.
the class JCERSACipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
AlgorithmParameterSpec paramSpec = null;
if (params != null) {
try {
paramSpec = params.getParameterSpec(OAEPParameterSpec.class);
} catch (InvalidParameterSpecException e) {
throw new InvalidAlgorithmParameterException("cannot recognise parameters: " + e.toString(), e);
}
}
engineParams = params;
engineInit(opmode, key, paramSpec, random);
}
use of java.security.spec.InvalidParameterSpecException in project jdk8u_jdk by JetBrains.
the class TestNonexpanding method runTest.
public void runTest(String algo, String mo, String pad) throws Exception {
Cipher ci = null;
SecretKey key = null;
try {
// Initialization
Random rdm = new Random();
byte[] plainText = new byte[128];
rdm.nextBytes(plainText);
ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
kg.init(KEY_LENGTH);
key = kg.generateKey();
// encrypt
ci.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
ci.doFinal(cipherText, offset);
// Comparison
if (!(plainText.length == cipherText.length)) {
// authentication tag and cipher text.
if (mo.equalsIgnoreCase("GCM")) {
GCMParameterSpec spec = ci.getParameters().getParameterSpec(GCMParameterSpec.class);
int cipherTextLength = cipherText.length - spec.getTLen() / 8;
if (plainText.length == cipherTextLength) {
return;
}
}
System.out.println("Original length: " + plainText.length);
System.out.println("Cipher text length: " + cipherText.length);
throw new RuntimeException("Test failed!");
}
} catch (NoSuchAlgorithmException e) {
//CFB7 and OFB150 are for negative testing
if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
throw new RuntimeException("Test failed!");
}
} catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidParameterSpecException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
System.out.println("Test failed!");
throw e;
}
}
use of java.security.spec.InvalidParameterSpecException in project android_frameworks_base by AOSPA.
the class AndroidKeyStoreUnauthenticatedAESCipherSpi method engineGetParameters.
@Nullable
@Override
protected final AlgorithmParameters engineGetParameters() {
if (!mIvRequired) {
return null;
}
if ((mIv != null) && (mIv.length > 0)) {
try {
AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
params.init(new IvParameterSpec(mIv));
return params;
} catch (NoSuchAlgorithmException e) {
throw new ProviderException("Failed to obtain AES AlgorithmParameters", e);
} catch (InvalidParameterSpecException e) {
throw new ProviderException("Failed to initialize AES AlgorithmParameters with an IV", e);
}
}
return null;
}
use of java.security.spec.InvalidParameterSpecException in project android_frameworks_base by AOSPA.
the class AndroidKeyStoreUnauthenticatedAESCipherSpi method initAlgorithmSpecificParameters.
@Override
protected final void initAlgorithmSpecificParameters(AlgorithmParameters params) throws InvalidAlgorithmParameterException {
if (!mIvRequired) {
if (params != null) {
throw new InvalidAlgorithmParameterException("Unsupported parameters: " + params);
}
return;
}
// IV is used
if (params == null) {
if (!isEncrypting()) {
// IV must be provided by the caller
throw new InvalidAlgorithmParameterException("IV required when decrypting" + ". Use IvParameterSpec or AlgorithmParameters to provide it.");
}
return;
}
if (!"AES".equalsIgnoreCase(params.getAlgorithm())) {
throw new InvalidAlgorithmParameterException("Unsupported AlgorithmParameters algorithm: " + params.getAlgorithm() + ". Supported: AES");
}
IvParameterSpec ivSpec;
try {
ivSpec = params.getParameterSpec(IvParameterSpec.class);
} catch (InvalidParameterSpecException e) {
if (!isEncrypting()) {
// IV must be provided by the caller
throw new InvalidAlgorithmParameterException("IV required when decrypting" + ", but not found in parameters: " + params, e);
}
mIv = null;
return;
}
mIv = ivSpec.getIV();
if (mIv == null) {
throw new InvalidAlgorithmParameterException("Null IV in AlgorithmParameters");
}
}
use of java.security.spec.InvalidParameterSpecException in project sling by apache.
the class TopologyRequestValidator method encodeMessage.
/**
* Encodes a request returning the encoded body
*
* @param body
* @return the encoded body.
* @throws IOException
*/
public String encodeMessage(String body) throws IOException {
checkActive();
if (encryptionEnabled) {
try {
JsonObjectBuilder json = Json.createObjectBuilder();
JsonArrayBuilder array = Json.createArrayBuilder();
for (String value : encrypt(body)) {
array.add(value);
}
json.add("payload", array);
StringWriter writer = new StringWriter();
Json.createGenerator(writer).write(json.build()).close();
return writer.toString();
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (IllegalBlockSizeException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (BadPaddingException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (UnsupportedEncodingException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (NoSuchPaddingException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (JsonException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (InvalidKeySpecException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (InvalidParameterSpecException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
}
}
return body;
}
Aggregations