Search in sources :

Example 86 with AlgorithmParameterSpec

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);
    }
}
Also used : AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 87 with AlgorithmParameterSpec

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());
    }
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) IOException(java.io.IOException) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) IOException(java.io.IOException)

Example 88 with AlgorithmParameterSpec

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();
}
Also used : KeyGenerator(javax.crypto.KeyGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 89 with AlgorithmParameterSpec

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);
}
Also used : BigInteger(java.math.BigInteger) ECPoint(java.security.spec.ECPoint) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 90 with AlgorithmParameterSpec

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"));
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) IvParameterSpec(javax.crypto.spec.IvParameterSpec) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Aggregations

AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)173 IvParameterSpec (javax.crypto.spec.IvParameterSpec)56 Cipher (javax.crypto.Cipher)55 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)49 InvalidKeyException (java.security.InvalidKeyException)42 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 SecretKey (javax.crypto.SecretKey)27 SecureRandom (java.security.SecureRandom)24 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)24 BadPaddingException (javax.crypto.BadPaddingException)21 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)20 BigInteger (java.math.BigInteger)19 RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)19 ShortBufferException (javax.crypto.ShortBufferException)19 Key (java.security.Key)18 SecretKeySpec (javax.crypto.spec.SecretKeySpec)18 AlgorithmParameters (java.security.AlgorithmParameters)16 KeyGenerator (javax.crypto.KeyGenerator)16 IOException (java.io.IOException)14 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)14