use of java.security.spec.KeySpec in project ORCID-Source by ORCID.
the class DesEncrypter method initDesEncrypter.
private void initDesEncrypter(final String passPhrase) {
try {
// Create the key
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
// Create the ciphers
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
} catch (GeneralSecurityException e) {
LOGGER.trace("DesEncrypter.creation failed", e);
throw new ApplicationException("DesEncrypter creation failed", e);
}
}
use of java.security.spec.KeySpec in project j2objc by google.
the class KeyFactorySpiTest method testKeyFactorySpi.
/**
* Test for <code>KeyFactorySpi</code> constructor
* Assertion: constructs KeyFactorySpi
*/
public void testKeyFactorySpi() {
MyKeyFactorySpi keyFSpi = new MyKeyFactorySpi();
assertTrue(keyFSpi instanceof KeyFactorySpi);
KeySpec ks = new MyKeySpec();
KeySpec kss = new MyKeySpec();
try {
keyFSpi.engineGeneratePrivate(ks);
keyFSpi.engineGeneratePublic(ks);
keyFSpi.engineGetKeySpec(null, java.lang.Class.class);
keyFSpi.engineTranslateKey(null);
} catch (Exception e) {
fail("Unexpected exception");
}
}
use of java.security.spec.KeySpec in project j2objc by google.
the class KeyFactoryTest method testGetKeySpec.
@SuppressWarnings("unchecked")
public void testGetKeySpec() {
KeyFactory factory = null;
try {
factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
}
assertNotNull(factory);
{
Key[] keys = { new TestPrivateKey(), new TestPublicKey(), new TestPrivateKey(new byte[] { 42, 41, 40 }), new TestPublicKey(new byte[] { 40, 41, 42 }) };
Class[] keySpecs = { TestPrivateKeySpec.class, TestPublicKeySpec.class, TestPrivateKeySpec.class, TestPublicKeySpec.class };
for (int i = 0; i < keys.length; i++) {
Key key = keys[i];
Class keySpec = keySpecs[i];
String message = "getKeySpec(" + key.toString() + ", " + keySpec.toString() + ")";
try {
KeySpec spec = factory.getKeySpec(key, keySpec);
assertNotNull(spec);
assertTrue(spec.getClass() == keySpec);
} catch (InvalidKeySpecException e) {
fail("unexpected exception: " + e);
}
}
}
{
Key[] keys = { new AnotherKey(), null, new TestPrivateKey(), null };
Class[] keySpecs = { KeySpec.class, TestPrivateKeySpec.class, null, null };
Class[] exceptions = { InvalidKeySpecException.class, NullPointerException.class, InvalidKeySpecException.class, NullPointerException.class };
for (int i = 0; i < keys.length; i++) {
Key key = keys[i];
Class keySpec = keySpecs[i];
exceptionThrown = false;
String message = "getKeySpec(" + (key == null ? "null" : key.toString()) + ", " + (keySpec == null ? "null" : keySpec.toString()) + ")";
try {
factory.getKeySpec(key, keySpec);
} catch (Exception e) {
checkException(message, e, exceptions[i]);
} finally {
checkException(message, null, exceptions[i]);
}
}
}
}
use of java.security.spec.KeySpec in project j2objc by google.
the class RSAPublicKeySpecTest method testRSAPublicKeySpec02.
/**
* Test #2 for <code>RSAPublicKeySpec</code> constructor
* Assertion: Constructs <code>RSAPublicKeySpec</code>
* object using valid parameters
*/
public final void testRSAPublicKeySpec02() {
KeySpec ks = new RSAPublicKeySpec(null, null);
assertTrue(ks instanceof RSAPublicKeySpec);
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class SubjectPublicKeyInfo method getPublicKey.
/**
* Returns the PublicKey corresponding to this SubjectPublicKeyInfo
* instance.
*/
public PublicKey getPublicKey() {
if (publicKey == null) {
final byte[] encoded = getEncoded();
final KeySpec keySpec = new X509EncodedKeySpec(encoded);
/* Try using the algorithm name first. */
final String algName = algorithmID.getAlgorithmName();
publicKey = generateKeyForAlgorithm(keySpec, algName);
/*
* Fall back to using the algorithm OID if it's not the same as the
* algorithm name.
*/
final String algOid = algorithmID.getAlgorithm();
if (publicKey == null && !algOid.equals(algName)) {
publicKey = generateKeyForAlgorithm(keySpec, algOid);
}
/*
* Encode this as an X.509 public key since we didn't have any
* KeyFactory that could handle this algorithm name or OID. Perhaps
* the thing that's using this can decode it.
*/
if (publicKey == null) {
publicKey = new X509PublicKey(algOid, encoded, subjectPublicKey);
}
}
return publicKey;
}
Aggregations