use of javax.crypto.spec.DESedeKeySpec in project robovm by robovm.
the class mySecretKeyFactory method testSecretKeyFactory10.
/**
* Test for <code>generateSecret(KeySpec keySpec)</code> and
* <code>getKeySpec(SecretKey key, Class keySpec)
* methods
* Assertion:
* throw InvalidKeySpecException if parameter is inappropriate
*/
public void testSecretKeyFactory10() throws InvalidKeyException, InvalidKeySpecException {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
byte[] bb = new byte[24];
KeySpec ks = (defaultAlgorithm.equals(defaultAlgorithm2) ? (KeySpec) new DESKeySpec(bb) : (KeySpec) new DESedeKeySpec(bb));
KeySpec rks = null;
SecretKeySpec secKeySpec = new SecretKeySpec(bb, defaultAlgorithm);
SecretKey secKey = null;
SecretKeyFactory[] skF = createSKFac();
assertNotNull("SecretKeyFactory object were not created", skF);
for (int i = 0; i < skF.length; i++) {
try {
skF[i].generateSecret(null);
fail("generateSecret(null): InvalidKeySpecException must be thrown");
} catch (InvalidKeySpecException e) {
}
secKey = skF[i].generateSecret(ks);
try {
skF[i].getKeySpec(null, null);
fail("getKeySpec(null,null): InvalidKeySpecException must be thrown");
} catch (InvalidKeySpecException e) {
}
try {
skF[i].getKeySpec(null, ks.getClass());
fail("getKeySpec(null, Class): InvalidKeySpecException must be thrown");
} catch (InvalidKeySpecException e) {
}
try {
skF[i].getKeySpec(secKey, null);
fail("getKeySpec(secKey, null): NullPointerException or InvalidKeySpecException must be thrown");
} catch (InvalidKeySpecException e) {
// Expected
} catch (NullPointerException e) {
// Expected
}
try {
Class c;
if (defaultAlgorithm.equals(defaultAlgorithm2)) {
c = DESedeKeySpec.class;
} else {
c = DESKeySpec.class;
}
skF[i].getKeySpec(secKeySpec, c);
fail("getKeySpec(secKey, Class): InvalidKeySpecException must be thrown");
} catch (InvalidKeySpecException e) {
}
rks = skF[i].getKeySpec(secKeySpec, ks.getClass());
if (defaultAlgorithm.equals(defaultAlgorithm1)) {
assertTrue("Incorrect getKeySpec() result 1", rks instanceof DESedeKeySpec);
} else {
assertTrue("Incorrect getKeySpec() result 1", rks instanceof DESKeySpec);
}
rks = skF[i].getKeySpec(secKey, ks.getClass());
if (defaultAlgorithm.equals(defaultAlgorithm1)) {
assertTrue("Incorrect getKeySpec() result 2", rks instanceof DESedeKeySpec);
} else {
assertTrue("Incorrect getKeySpec() result 2", rks instanceof DESKeySpec);
}
}
}
use of javax.crypto.spec.DESedeKeySpec in project robovm by robovm.
the class DESedeKeySpecTest method testGetKey.
/**
* getKey() method testing. Checks that modification of returned key
* does not affect the internal key. Also test check an equality of
* the key with the key specified in the constructor. The object under
* the test is created by different constructors.
*/
public void testGetKey() {
byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2 };
DESedeKeySpec ks;
try {
ks = new DESedeKeySpec(key);
} catch (InvalidKeyException e) {
fail("InvalidKeyException should not be thrown.");
return;
}
byte[] res = ks.getKey();
assertTrue("The returned array should be equal to the specified " + "in constructor.", Arrays.equals(key, res));
res[0] += 1;
assertFalse("The modification of returned key should not affect" + "the underlying key.", key[0] == res[0]);
byte[] key1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3 };
try {
ks = new DESedeKeySpec(key1, 2);
} catch (InvalidKeyException e) {
fail("InvalidKeyException should not be thrown.");
return;
}
res = ks.getKey();
assertNotSame("The returned array should not be the same object " + "as specified in a constructor.", key1, res);
byte[] exp = new byte[24];
System.arraycopy(key1, 2, exp, 0, 24);
assertTrue("The returned array should be equal to the specified " + "in constructor.", Arrays.equals(exp, res));
}
use of javax.crypto.spec.DESedeKeySpec in project robovm by robovm.
the class SecretKeyFactoryThread method test.
@Override
public void test() throws Exception {
SecretKeyFactory skf = SecretKeyFactory.getInstance(algName);
byte[] b = new byte[24];
KeySpec ks = (KeySpec) ((algName == "DES") ? new DESKeySpec(b) : (algName == "DESede") ? new DESedeKeySpec(b) : new PBEKeySpec("passw".toCharArray()));
skf.generateSecret(ks);
}
use of javax.crypto.spec.DESedeKeySpec in project camel by apache.
the class XMLSecurityDataFormat method generateKeyEncryptionKey.
private Key generateKeyEncryptionKey(String algorithm) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
DESedeKeySpec keySpec;
Key secretKey;
try {
if (algorithm.equalsIgnoreCase("DESede")) {
keySpec = new DESedeKeySpec(passPhrase);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
secretKey = keyFactory.generateSecret(keySpec);
} else if (algorithm.equalsIgnoreCase("SEED")) {
secretKey = new SecretKeySpec(passPhrase, "SEED");
} else if (algorithm.equalsIgnoreCase("CAMELLIA")) {
secretKey = new SecretKeySpec(passPhrase, "CAMELLIA");
} else {
secretKey = new SecretKeySpec(passPhrase, "AES");
}
if (Arrays.equals(passPhrase, DEFAULT_KEY.getBytes())) {
LOG.warn("Using the default encryption key is not secure");
}
} catch (InvalidKeyException e) {
throw new InvalidKeyException("InvalidKeyException due to invalid passPhrase: " + Arrays.toString(passPhrase));
} catch (NoSuchAlgorithmException e) {
throw new NoSuchAlgorithmException("NoSuchAlgorithmException while using algorithm: " + algorithm);
} catch (InvalidKeySpecException e) {
throw new InvalidKeySpecException("Invalid Key generated while using passPhrase: " + Arrays.toString(passPhrase));
}
return secretKey;
}
use of javax.crypto.spec.DESedeKeySpec in project oxCore by GluuFederation.
the class StringEncrypter method decrypt.
/**
* Decrypt a string encrypted with this encrypter
*
* @param encryptedString
* Encrypted string
* @return Decrypted string
* @throws EncryptionException
*/
public String decrypt(final String encryptedString, String encryptionKey) throws EncryptionException {
lock.lock();
try {
final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
KeySpec keySpec;
if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
keySpec = new DESedeKeySpec(keyAsBytes);
} else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
keySpec = new DESKeySpec(keyAsBytes);
} else {
throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
}
return decrypt(encryptedString, keySpec);
} catch (final Exception e) {
throw new EncryptionException(e);
} finally {
lock.unlock();
}
}
Aggregations