Search in sources :

Example 81 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project i2p.i2p by i2p.

the class SigUtil method fromJavaKey.

/**
 *  Use if SigType is unknown.
 *  For efficiency, use fromJavakey(pk, type) if type is known.
 *
 *  @param pk JAVA key!
 *  @throws IllegalArgumentException on unknown type
 *  @since 0.9.18
 */
public static SigningPrivateKey fromJavaKey(PrivateKey pk) throws GeneralSecurityException {
    if (pk instanceof DSAPrivateKey) {
        return fromJavaKey((DSAPrivateKey) pk);
    }
    if (pk instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) pk;
        AlgorithmParameterSpec spec = k.getParams();
        SigType type;
        if (spec.equals(SigType.ECDSA_SHA256_P256.getParams()))
            type = SigType.ECDSA_SHA256_P256;
        else if (spec.equals(SigType.ECDSA_SHA384_P384.getParams()))
            type = SigType.ECDSA_SHA384_P384;
        else if (spec.equals(SigType.ECDSA_SHA512_P521.getParams()))
            type = SigType.ECDSA_SHA512_P521;
        else
            throw new IllegalArgumentException("Unknown EC type");
        return fromJavaKey(k, type);
    }
    if (pk instanceof EdDSAPrivateKey) {
        return fromJavaKey((EdDSAPrivateKey) pk, SigType.EdDSA_SHA512_Ed25519);
    }
    if (pk instanceof RSAPrivateKey) {
        RSAPrivateKey k = (RSAPrivateKey) pk;
        int sz = k.getModulus().bitLength();
        SigType type;
        if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA256_2048.getParams()).getKeysize())
            type = SigType.RSA_SHA256_2048;
        else if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA384_3072.getParams()).getKeysize())
            type = SigType.RSA_SHA384_3072;
        else if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA512_4096.getParams()).getKeysize())
            type = SigType.RSA_SHA512_4096;
        else
            throw new IllegalArgumentException("Unknown RSA type");
        return fromJavaKey(k, type);
    }
    throw new IllegalArgumentException("Unknown type: " + pk.getClass());
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) ECPoint(java.security.spec.ECPoint)

Example 82 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project XUtil by xuexiangjys.

the class EncryptUtils method desTemplate.

/**
 * DES 加密模板
 *
 * @param data           数据
 * @param key            秘钥
 * @param algorithm      加密算法
 * @param transformation 转变
 * @param isEncrypt      {@code true}: 加密 {@code false}: 解密
 * @return 密文或者明文,适用于 DES,3DES,AES
 */
private static byte[] desTemplate(final byte[] data, final byte[] key, final String algorithm, final String transformation, final byte[] iv, final boolean isEncrypt) {
    if (data == null || data.length == 0 || key == null || key.length == 0)
        return null;
    try {
        SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
        Cipher cipher = Cipher.getInstance(transformation);
        if (iv == null || iv.length == 0) {
            cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec);
        } else {
            AlgorithmParameterSpec params = new IvParameterSpec(iv);
            cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, params);
        }
        return cipher.doFinal(data);
    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 83 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project credhub by cloudfoundry-incubator.

the class EncryptionService method encrypt.

public EncryptedValue encrypt(UUID canaryUuid, Key key, String value) throws Exception {
    byte[] nonce = generateNonce();
    AlgorithmParameterSpec parameterSpec = generateParameterSpec(nonce);
    CipherWrapper encryptionCipher = getCipher();
    encryptionCipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
    byte[] encrypted = encryptionCipher.doFinal(value.getBytes(CHARSET));
    return new EncryptedValue(canaryUuid, encrypted, nonce);
}
Also used : EncryptedValue(org.cloudfoundry.credhub.entity.EncryptedValue) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 84 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project jruby-openssl by jruby.

the class Cipher method doInitCipher.

private void doInitCipher(final Ruby runtime) {
    if (isDebug(runtime)) {
        dumpVars(runtime.getOut(), "doInitCipher()");
    }
    checkCipherNotNull(runtime);
    if (key == null) {
        // key = emptyKey(keyLength);
        throw newCipherError(runtime, "key not specified");
    }
    try {
        // ECB mode is the only mode that does not require an IV
        if ("ECB".equalsIgnoreCase(cryptoMode)) {
            cipher.init(encryptMode ? ENCRYPT_MODE : DECRYPT_MODE, new SimpleSecretKey(getCipherAlgorithm(), this.key));
        } else {
            // if no IV yet, start out with all \0s
            if (realIV == null)
                realIV = new byte[ivLength];
            if ("RC2".equalsIgnoreCase(cryptoBase)) {
                cipher.init(encryptMode ? ENCRYPT_MODE : DECRYPT_MODE, new SimpleSecretKey("RC2", this.key), new RC2ParameterSpec(this.key.length * 8, this.realIV));
            } else if ("RC4".equalsIgnoreCase(cryptoBase)) {
                cipher.init(encryptMode ? ENCRYPT_MODE : DECRYPT_MODE, new SimpleSecretKey("RC4", this.key));
            } else {
                final AlgorithmParameterSpec ivSpec;
                if ("GCM".equalsIgnoreCase(cryptoMode)) {
                    // e.g. 'aes-128-gcm'
                    ivSpec = new GCMParameterSpec(getAuthTagLength() * 8, this.realIV);
                } else {
                    ivSpec = new IvParameterSpec(this.realIV);
                }
                cipher.init(encryptMode ? ENCRYPT_MODE : DECRYPT_MODE, new SimpleSecretKey(getCipherAlgorithm(), this.key), ivSpec);
            }
        }
    } catch (InvalidKeyException e) {
        throw newCipherError(runtime, e + "\n possibly you need to install Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files for your JRE");
    } catch (Exception e) {
        debugStackTrace(runtime, e);
        throw newCipherError(runtime, e);
    }
    cipherInited = true;
    processedDataBytes = 0;
// outBuffer = new ByteList(keyLength);
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) RaiseException(org.jruby.exceptions.RaiseException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 85 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project mule by mulesoft.

the class AbstractJCEEncryptionStrategy method createAndInitCiphers.

protected void createAndInitCiphers() throws GeneralSecurityException {
    encryptCipher = Cipher.getInstance(getAlgorithm());
    decryptCipher = Cipher.getInstance(getAlgorithm());
    AlgorithmParameterSpec paramSpec = createAlgorithmParameterSpec();
    if (paramSpec != null) {
        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
    } else {
        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
    }
}
Also used : 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