use of javax.crypto.SecretKeyFactory in project orientdb by orientechnologies.
the class ODESEncryption method configure.
public OEncryption configure(final String iOptions) {
initialized = false;
if (iOptions == null)
throw new OSecurityException("DES encryption has been selected, but no key was found. Please configure it by passing the key as property at database create/open. The property key is: '" + OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey() + "'");
try {
final byte[] key = OBase64Utils.decode(iOptions);
final DESKeySpec desKeySpec = new DESKeySpec(key);
final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_NAME);
theKey = keyFactory.generateSecret(desKeySpec);
cipher = Cipher.getInstance(TRANSFORMATION);
} catch (Exception e) {
throw OException.wrapException(new OInvalidStorageEncryptionKeyException("Cannot initialize DES encryption with current key. Assure the key is a BASE64 - 64 bits long"), e);
}
this.initialized = true;
return this;
}
use of javax.crypto.SecretKeyFactory in project robovm by robovm.
the class PKCS12KeyStoreSpi method unwrapKey.
protected PrivateKey unwrapKey(AlgorithmIdentifier algId, byte[] data, char[] password, boolean wrongPKCS12Zero) throws IOException {
ASN1ObjectIdentifier algorithm = algId.getAlgorithm();
try {
if (algorithm.on(PKCSObjectIdentifiers.pkcs_12PbeIds)) {
PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters());
PBEKeySpec pbeSpec = new PBEKeySpec(password);
PrivateKey out;
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm.getId(), bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
SecretKey k = keyFact.generateSecret(pbeSpec);
((BCPBEKey) k).setTryWrongPKCS12Zero(wrongPKCS12Zero);
Cipher cipher = Cipher.getInstance(algorithm.getId(), bcProvider);
cipher.init(Cipher.UNWRAP_MODE, k, defParams);
// we pass "" as the key algorithm type as it is unknown at this point
return (PrivateKey) cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
} else if (algorithm.equals(PKCSObjectIdentifiers.id_PBES2)) {
PBES2Parameters alg = PBES2Parameters.getInstance(algId.getParameters());
PBKDF2Params func = PBKDF2Params.getInstance(alg.getKeyDerivationFunc().getParameters());
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(alg.getKeyDerivationFunc().getAlgorithm().getId(), bcProvider);
SecretKey k = keyFact.generateSecret(new PBEKeySpec(password, func.getSalt(), func.getIterationCount().intValue(), SecretKeyUtil.getKeySize(alg.getEncryptionScheme().getAlgorithm())));
Cipher cipher = Cipher.getInstance(alg.getEncryptionScheme().getAlgorithm().getId(), bcProvider);
cipher.init(Cipher.UNWRAP_MODE, k, new IvParameterSpec(ASN1OctetString.getInstance(alg.getEncryptionScheme().getParameters()).getOctets()));
// we pass "" as the key algorithm type as it is unknown at this point
return (PrivateKey) cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
}
} catch (Exception e) {
throw new IOException("exception unwrapping private key - " + e.toString());
}
throw new IOException("exception unwrapping private key - cannot recognise: " + algorithm);
}
use of javax.crypto.SecretKeyFactory in project robovm by robovm.
the class PKCS12KeyStoreSpi method calculatePbeMac.
private static byte[] calculatePbeMac(ASN1ObjectIdentifier oid, byte[] salt, int itCount, char[] password, boolean wrongPkcs12Zero, byte[] data) throws Exception {
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(oid.getId(), bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(salt, itCount);
PBEKeySpec pbeSpec = new PBEKeySpec(password);
BCPBEKey key = (BCPBEKey) keyFact.generateSecret(pbeSpec);
key.setTryWrongPKCS12Zero(wrongPkcs12Zero);
Mac mac = Mac.getInstance(oid.getId(), bcProvider);
mac.init(key, defParams);
mac.update(data);
return mac.doFinal();
}
use of javax.crypto.SecretKeyFactory in project robovm by robovm.
the class PKCS12KeyStoreSpi method cryptData.
protected byte[] cryptData(boolean forEncryption, AlgorithmIdentifier algId, char[] password, boolean wrongPKCS12Zero, byte[] data) throws IOException {
String algorithm = algId.getAlgorithm().getId();
PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters());
PBEKeySpec pbeSpec = new PBEKeySpec(password);
try {
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
BCPBEKey key = (BCPBEKey) keyFact.generateSecret(pbeSpec);
key.setTryWrongPKCS12Zero(wrongPKCS12Zero);
Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
int mode = forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
cipher.init(mode, key, defParams);
return cipher.doFinal(data);
} catch (Exception e) {
throw new IOException("exception decrypting data - " + e.toString());
}
}
use of javax.crypto.SecretKeyFactory 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);
}
}
}
Aggregations