use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.
the class APSpecSpi method testKeyGeneratorSpi01.
/**
* Test for <code>KeyGeneratorSpi</code> constructor Assertion: constructs
* KeyGeneratorSpi
*/
public void testKeyGeneratorSpi01() throws InvalidAlgorithmParameterException {
Mock_KeyGeneratorSpi kgSpi = new Mock_KeyGeneratorSpi();
assertNull("Not null result", kgSpi.engineGenerateKey());
try {
kgSpi.engineInit(77, new SecureRandom());
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
try {
kgSpi.engineInit(new SecureRandom());
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
AlgorithmParameterSpec aps = null;
try {
kgSpi.engineInit(aps, new SecureRandom());
fail("InvalidAlgorithmParameterException must be thrown when parameter is null");
} catch (InvalidAlgorithmParameterException e) {
}
aps = new APSpecSpi();
kgSpi.engineInit(aps, new SecureRandom());
}
use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.
the class myKeyGenerator method testKeyGenerator.
/**
* Test for <code>KeyGenerator</code> constructor Assertion: returns
* KeyGenerator object
*/
public void testKeyGenerator() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
KeyGeneratorSpi spi = new MyKeyGeneratorSpi();
KeyGenerator keyG = new myKeyGenerator(spi, defaultProvider, defaultAlgorithm);
assertEquals("Incorrect algorithm", keyG.getAlgorithm(), defaultAlgorithm);
assertEquals("Incorrect provider", keyG.getProvider(), defaultProvider);
AlgorithmParameterSpec params = null;
int keysize = 0;
try {
keyG.init(params, null);
fail("InvalidAlgorithmParameterException must be thrown");
} catch (InvalidAlgorithmParameterException e) {
}
try {
keyG.init(keysize, null);
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
keyG = new myKeyGenerator(null, null, null);
assertNull("Algorithm must be null", keyG.getAlgorithm());
assertNull("Provider must be null", keyG.getProvider());
try {
keyG.init(params, null);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
}
try {
keyG.init(keysize, null);
fail("NullPointerException or InvalidParameterException must be thrown");
} catch (InvalidParameterException e) {
} catch (NullPointerException e) {
}
}
use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.
the class myKeyGenerator method testInitParams.
/*
* Test for <code>init(AlgorithmParameterSpec params)</code> and
* <code>init(AlgorithmParameterSpec params, SecureRandom random)</code> methods
* Assertion: throws InvalidAlgorithmParameterException when params is null
*/
public void testInitParams() throws Exception {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
KeyGenerator[] kgs = createKGs();
AlgorithmParameterSpec aps = null;
for (int i = 0; i < kgs.length; i++) {
try {
kgs[i].init(aps);
fail("InvalidAlgorithmParameterException must be thrown");
} catch (InvalidAlgorithmParameterException e) {
}
try {
kgs[i].init(aps, new SecureRandom());
fail("InvalidAlgorithmParameterException must be thrown");
} catch (InvalidAlgorithmParameterException e) {
}
}
}
use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.
the class CipherTest method test_updateLjava_nio_ByteBufferLjava_nio_ByteBuffer.
public void test_updateLjava_nio_ByteBufferLjava_nio_ByteBuffer() throws Exception {
byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
ByteBuffer bInput = ByteBuffer.allocate(256);
ByteBuffer bOutput = ByteBuffer.allocate(256);
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
bInput.put(b, 0, 10);
bInput.rewind();
bOutput.rewind();
c.update(bInput, bOutput);
c = Cipher.getInstance("DES/CBC/NoPadding");
try {
c.update(bInput, bOutput);
fail();
} catch (IllegalStateException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
bInput = ByteBuffer.allocate(16);
bInput.put(b, 0, 16);
bInput.rewind();
bOutput.rewind();
c.update(bInput, bOutput);
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
bInput = ByteBuffer.allocate(64);
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
bInput.put(b, 0, 16);
bInput.rewind();
try {
c.update(bInput, bInput);
fail();
} catch (IllegalArgumentException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
bInput.put(b, 0, 16);
bInput.rewind();
bOutput.rewind();
try {
c.update(bInput, bOutput.asReadOnlyBuffer());
fail();
} catch (ReadOnlyBufferException expected) {
}
bInput.rewind();
bInput.put(b, 0, 16);
bInput.rewind();
bOutput = ByteBuffer.allocate(8);
c = Cipher.getInstance("DESede");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
try {
c.update(bInput, bOutput);
fail();
} catch (ShortBufferException expected) {
}
}
use of java.security.spec.AlgorithmParameterSpec in project jdk8u_jdk by JetBrains.
the class PKCS12KeyStore method encryptPrivateKey.
/*
* Encrypt private key using Password-based encryption (PBE)
* as defined in PKCS#5.
*
* NOTE: By default, pbeWithSHAAnd3-KeyTripleDES-CBC algorithmID is
* used to derive the key and IV.
*
* @return encrypted private key encoded as EncryptedPrivateKeyInfo
*/
private byte[] encryptPrivateKey(byte[] data, KeyStore.PasswordProtection passwordProtection) throws IOException, NoSuchAlgorithmException, UnrecoverableKeyException {
byte[] key = null;
try {
String algorithm;
AlgorithmParameters algParams;
AlgorithmId algid;
// Initialize PBE algorithm and parameters
algorithm = passwordProtection.getProtectionAlgorithm();
if (algorithm != null) {
AlgorithmParameterSpec algParamSpec = passwordProtection.getProtectionParameters();
if (algParamSpec != null) {
algParams = AlgorithmParameters.getInstance(algorithm);
algParams.init(algParamSpec);
} else {
algParams = getAlgorithmParameters(algorithm);
}
} else {
// Check default key protection algorithm for PKCS12 keystores
algorithm = AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
String prop = Security.getProperty(KEY_PROTECTION_ALGORITHM[0]);
if (prop == null) {
prop = Security.getProperty(KEY_PROTECTION_ALGORITHM[1]);
}
return prop;
}
});
if (algorithm == null || algorithm.isEmpty()) {
algorithm = "PBEWithSHA1AndDESede";
}
algParams = getAlgorithmParameters(algorithm);
}
ObjectIdentifier pbeOID = mapPBEAlgorithmToOID(algorithm);
if (pbeOID == null) {
throw new IOException("PBE algorithm '" + algorithm + " 'is not supported for key entry protection");
}
// Use JCE
SecretKey skey = getPBEKey(passwordProtection.getPassword());
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
byte[] encryptedKey = cipher.doFinal(data);
algid = new AlgorithmId(pbeOID, cipher.getParameters());
if (debug != null) {
debug.println(" (Cipher algorithm: " + cipher.getAlgorithm() + ")");
}
// wrap encrypted private key in EncryptedPrivateKeyInfo
// as defined in PKCS#8
EncryptedPrivateKeyInfo encrInfo = new EncryptedPrivateKeyInfo(algid, encryptedKey);
key = encrInfo.getEncoded();
} catch (Exception e) {
UnrecoverableKeyException uke = new UnrecoverableKeyException("Encrypt Private Key failed: " + e.getMessage());
uke.initCause(e);
throw uke;
}
return key;
}
Aggregations