use of java.security.AlgorithmParameters in project robovm by robovm.
the class AlgorithmParameterGeneratorSpi method engineGenerateParameters.
protected AlgorithmParameters engineGenerateParameters() {
DSAParametersGenerator pGen;
if (strength <= 1024) {
pGen = new DSAParametersGenerator();
} else {
pGen = new DSAParametersGenerator(new SHA256Digest());
}
if (random == null) {
random = new SecureRandom();
}
if (strength == 1024) {
params = new DSAParameterGenerationParameters(1024, 160, 80, random);
pGen.init(params);
} else if (strength > 1024) {
params = new DSAParameterGenerationParameters(strength, 256, 80, random);
pGen.init(params);
} else {
pGen.init(strength, 20, random);
}
DSAParameters p = pGen.generateParameters();
AlgorithmParameters params;
try {
params = AlgorithmParameters.getInstance("DSA", BouncyCastleProvider.PROVIDER_NAME);
params.init(new DSAParameterSpec(p.getP(), p.getQ(), p.getG()));
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return params;
}
use of java.security.AlgorithmParameters in project robovm by robovm.
the class SealedObject method getObject.
/**
* Returns the wrapped object, decrypting it using the specified key.
*
* @param key
* the key to decrypt the data with.
* @return the encapsulated object.
* @throws IOException
* if deserialization fails.
* @throws ClassNotFoundException
* if deserialization fails.
* @throws NoSuchAlgorithmException
* if the algorithm to decrypt the data is not available.
* @throws InvalidKeyException
* if the specified key cannot be used to decrypt the data.
*/
public final Object getObject(Key key) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeyException {
if (key == null) {
throw new InvalidKeyException("key == null");
}
try {
Cipher cipher = Cipher.getInstance(sealAlg);
if ((paramsAlg != null) && (paramsAlg.length() != 0)) {
AlgorithmParameters params = AlgorithmParameters.getInstance(paramsAlg);
params.init(encodedParams);
cipher.init(Cipher.DECRYPT_MODE, key, params);
} else {
cipher.init(Cipher.DECRYPT_MODE, key);
}
byte[] serialized = cipher.doFinal(encryptedContent);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serialized));
return ois.readObject();
} catch (NoSuchPaddingException e) {
// with existing padding
throw new NoSuchAlgorithmException(e.toString());
} catch (InvalidAlgorithmParameterException e) {
// with correct algorithm parameters
throw new NoSuchAlgorithmException(e.toString());
} catch (IllegalBlockSizeException e) {
// was correctly made
throw new NoSuchAlgorithmException(e.toString());
} catch (BadPaddingException e) {
// was correctly made
throw new NoSuchAlgorithmException(e.toString());
} catch (IllegalStateException e) {
// should never be thrown because cipher is initialized
throw new NoSuchAlgorithmException(e.toString());
}
}
use of java.security.AlgorithmParameters in project robovm by robovm.
the class EncryptedPrivateKeyInfoTest method testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray4.
/**
* Test #4 for
* <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
* </code>
* constructor <br>
* Assertion: byte array is copied to prevent subsequent modification <br>
* Test preconditions: valid array passed then modified <br>
* Expected: getEncryptedData(), invoked after above modification, must
* return array as it was before the modification
*
* @throws IOException
*/
public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray4() throws Exception {
AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
// use pregenerated AlgorithmParameters encodings
ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
byte[] encryptedDataCopy = EncryptedPrivateKeyInfoData.encryptedData.clone();
// pass valid array
EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap, encryptedDataCopy);
// modify array passed
encryptedDataCopy[0] = (byte) 6;
// check that internal state has not been affected
assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.encryptedData, epki.getEncryptedData()));
}
use of java.security.AlgorithmParameters in project robovm by robovm.
the class EncryptedPrivateKeyInfoTest method testGetEncoded03.
/**
* Test #3 for <code>getEncoded()</code> method <br>
* Assertion: returns the ASN.1 encoding of this object <br>
* Test preconditions: test object created using ctor which takes algorithm
* name and encrypted data as a parameters <br>
* Expected: equivalent encoded form (without alg params) must be returned
*
* @throws IOException
*/
public final void testGetEncoded03() throws IOException {
boolean performed = false;
for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
try {
AlgorithmParameters ap = AlgorithmParameters.getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
// use pregenerated AlgorithmParameters encodings
ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]));
EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap, EncryptedPrivateKeyInfoData.encryptedData);
// check that method under test returns
// valid encoded form
assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]), epki.getEncoded()));
performed = true;
} catch (NoSuchAlgorithmException allowedFailure) {
}
}
assertTrue("Test not performed", performed);
}
use of java.security.AlgorithmParameters in project robovm by robovm.
the class KeyAgreementThread method test.
@Override
public void test() throws Exception {
AlgorithmParameterGenerator apg = AlgorithmParameterGenerator.getInstance("DH");
apg.init(1024, new SecureRandom());
AlgorithmParameters ap = apg.generateParameters();
DHParameterSpec ps = ap.getParameterSpec(DHParameterSpec.class);
KeyAgreementGen kag1 = new KeyAgreementGen(ps);
KeyAgreementGen kag2 = new KeyAgreementGen(ps);
byte[] bArray1 = kag1.getPublicKeyBytes();
byte[] bArray2 = kag2.getPublicKeyBytes();
byte[] sk1 = kag1.getSecretKey(algName, bArray2);
byte[] sk2 = kag2.getSecretKey(algName, bArray1);
if (Arrays.areEqual(sk1, sk2) == false) {
throw new Exception("Generated keys are not the same");
}
}
Aggregations