use of java.security.spec.AlgorithmParameterSpec in project mule by mulesoft.
the class SecretKeyEncryptionStrategy method createAndInitCiphers.
@Override
protected void createAndInitCiphers() throws GeneralSecurityException {
encryptCipher = Cipher.getInstance(getAlgorithm());
decryptCipher = Cipher.getInstance(getAlgorithm());
AlgorithmParameterSpec paramSpec = createAlgorithmParameterSpec();
if (paramSpec != null) {
encryptCipher.init(Cipher.ENCRYPT_MODE, (SecretKeySpec) keySpec, paramSpec);
decryptCipher.init(Cipher.DECRYPT_MODE, (SecretKeySpec) keySpec, paramSpec);
} else {
encryptCipher.init(Cipher.ENCRYPT_MODE, (SecretKeySpec) keySpec);
decryptCipher.init(Cipher.DECRYPT_MODE, (SecretKeySpec) keySpec);
}
}
use of java.security.spec.AlgorithmParameterSpec in project BiglyBT by BiglySoftware.
the class PEMUtilities method crypt.
static byte[] crypt(boolean encrypt, String provider, byte[] bytes, char[] password, String dekAlgName, byte[] iv) throws IOException {
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
String alg;
String blockMode = "CBC";
String padding = "PKCS5Padding";
Key sKey;
// Figure out block mode and padding.
if (dekAlgName.endsWith("-CFB")) {
blockMode = "CFB";
padding = "NoPadding";
}
if (dekAlgName.endsWith("-ECB") || "DES-EDE".equals(dekAlgName) || "DES-EDE3".equals(dekAlgName)) {
// ECB is actually the default (though seldom used) when OpenSSL
// uses DES-EDE (des2) or DES-EDE3 (des3).
blockMode = "ECB";
paramSpec = null;
}
if (dekAlgName.endsWith("-OFB")) {
blockMode = "OFB";
padding = "NoPadding";
}
// Figure out algorithm and key size.
if (dekAlgName.startsWith("DES-EDE")) {
alg = "DESede";
// "DES-EDE" is actually des2 in OpenSSL-speak!
// "DES-EDE3" is des3.
boolean des2 = !dekAlgName.startsWith("DES-EDE3");
sKey = getKey(password, alg, 24, iv, des2);
} else if (dekAlgName.startsWith("DES-")) {
alg = "DES";
sKey = getKey(password, alg, 8, iv);
} else if (dekAlgName.startsWith("BF-")) {
alg = "Blowfish";
sKey = getKey(password, alg, 16, iv);
} else if (dekAlgName.startsWith("RC2-")) {
alg = "RC2";
int keyBits = 128;
if (dekAlgName.startsWith("RC2-40-")) {
keyBits = 40;
} else if (dekAlgName.startsWith("RC2-64-")) {
keyBits = 64;
}
sKey = getKey(password, alg, keyBits / 8, iv);
if (// ECB block mode
paramSpec == null) {
paramSpec = new RC2ParameterSpec(keyBits);
} else {
paramSpec = new RC2ParameterSpec(keyBits, iv);
}
} else if (dekAlgName.startsWith("AES-")) {
alg = "AES";
byte[] salt = iv;
if (salt.length > 8) {
salt = new byte[8];
System.arraycopy(iv, 0, salt, 0, 8);
}
int keyBits;
if (dekAlgName.startsWith("AES-128-")) {
keyBits = 128;
} else if (dekAlgName.startsWith("AES-192-")) {
keyBits = 192;
} else if (dekAlgName.startsWith("AES-256-")) {
keyBits = 256;
} else {
throw new IOException("unknown AES encryption with private key");
}
sKey = getKey(password, "AES", keyBits / 8, salt);
} else {
throw new IOException("unknown encryption with private key");
}
String transformation = alg + "/" + blockMode + "/" + padding;
try {
Cipher c = Cipher.getInstance(transformation, provider);
int mode = encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
if (// ECB block mode
paramSpec == null) {
c.init(mode, sKey);
} else {
c.init(mode, sKey, paramSpec);
}
return c.doFinal(bytes);
} catch (Exception e) {
throw new IOException("exception using cipher: " + e.toString());
}
}
use of java.security.spec.AlgorithmParameterSpec in project AppCenter-SDK-Android by Microsoft.
the class CryptoDefaultKeyGeneratorMockTest method checkParametersPassingForKeyGenerator.
@Test
public void checkParametersPassingForKeyGenerator() throws Exception {
mockStatic(KeyGenerator.class);
KeyGenerator mockKeyGenerator = mock(KeyGenerator.class);
when(KeyGenerator.getInstance(anyString(), anyString())).thenReturn(mockKeyGenerator);
CryptoUtils.IKeyGenerator keyGenerator = CryptoUtils.DEFAULT_CRYPTO_FACTORY.getKeyGenerator("AES", "MockProvider");
AlgorithmParameterSpec parameters = mock(AlgorithmParameterSpec.class);
keyGenerator.init(parameters);
verify(mockKeyGenerator).init(parameters);
keyGenerator.generateKey();
verify(mockKeyGenerator).generateKey();
}
use of java.security.spec.AlgorithmParameterSpec in project jmulticard by ctt-gob-es.
the class JseCryptoHelper method getEcPoint.
@Override
public AlgorithmParameterSpec getEcPoint(final byte[] nonceS, final byte[] sharedSecretH, final EcCurve curveName) {
final AlgorithmParameterSpec ecParams = ECNamedCurveTable.getParameterSpec(curveName.toString());
final BigInteger affineX = os2i(sharedSecretH);
final BigInteger affineY = computeAffineY(affineX, (ECParameterSpec) ecParams);
final ECPoint sharedSecretPointH = new ECPoint(affineX, affineY);
return mapNonceGMWithECDH(os2i(nonceS), sharedSecretPointH, (ECParameterSpec) ecParams);
}
use of java.security.spec.AlgorithmParameterSpec in project conceal by facebook.
the class AESCipher method getInstance.
public static AESCipher getInstance() {
byte[] iv = new byte[16];
byte[] key = new byte[16];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
random.nextBytes(key);
AlgorithmParameterSpec spec = new IvParameterSpec(iv);
return new AESCipher(spec, new SecretKeySpec(key, "AES"));
}
Aggregations