Search in sources :

Example 11 with RC2ParameterSpec

use of javax.crypto.spec.RC2ParameterSpec in project jdk8u_jdk by JetBrains.

the class RC2Cipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (params != null && params.getAlgorithm().equals("RC2")) {
        try {
            RC2ParameterSpec rc2Params = params.getParameterSpec(RC2ParameterSpec.class);
            engineInit(opmode, key, rc2Params, random);
        } catch (InvalidParameterSpecException ipse) {
            throw new InvalidAlgorithmParameterException("Wrong parameter type: RC2 expected");
        }
    } else {
        embeddedCipher.initEffectiveKeyBits(0);
        core.init(opmode, key, params, random);
    }
}
Also used : RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec)

Example 12 with RC2ParameterSpec

use of javax.crypto.spec.RC2ParameterSpec in project jdk8u_jdk by JetBrains.

the class RC2AlgorithmParameters method testParams.

private static byte[] testParams(AlgorithmParameters rc2Params, RC2ParameterSpec rc2Spec) throws Exception {
    // test getParameterSpec returns object equal to input
    rc2Params.init(rc2Spec);
    RC2ParameterSpec rc2OtherSpec = (RC2ParameterSpec) rc2Params.getParameterSpec(RC2ParameterSpec.class);
    if (!rc2Spec.equals(rc2OtherSpec)) {
        throw new Exception("AlgorithmParameterSpecs should be equal");
    }
    // test RC2ParameterSpec with RC2 Cipher
    Cipher rc2Cipher = Cipher.getInstance("RC2/CBC/PKCS5PADDING", "SunJCE");
    rc2Cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec("secret".getBytes("ASCII"), "RC2"), rc2Spec);
    // get IV
    byte[] iv = rc2Cipher.getIV();
    if (!Arrays.equals(iv, rc2Spec.getIV())) {
        throw new Exception("ivs should be equal");
    }
    // test encoding and decoding
    byte[] encoded = rc2Params.getEncoded();
    AlgorithmParameters params = AlgorithmParameters.getInstance("RC2");
    params.init(encoded);
    // test RC2 AlgorithmParameters with RC2 Cipher
    rc2Cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec("secret".getBytes("ASCII"), "RC2"), params);
    // get IV
    iv = rc2Cipher.getIV();
    if (!Arrays.equals(iv, rc2Spec.getIV())) {
        throw new Exception("ivs should be equal");
    }
    return encoded;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 13 with RC2ParameterSpec

use of javax.crypto.spec.RC2ParameterSpec in project poi by apache.

the class AgileDecryptor method initCipherForBlock.

protected static Cipher initCipherForBlock(Cipher existing, int block, boolean lastChunk, EncryptionInfo encryptionInfo, SecretKey skey, int encryptionMode) throws GeneralSecurityException {
    EncryptionHeader header = encryptionInfo.getHeader();
    String padding = (lastChunk ? "PKCS5Padding" : "NoPadding");
    if (existing == null || !existing.getAlgorithm().endsWith(padding)) {
        existing = getCipher(skey, header.getCipherAlgorithm(), header.getChainingMode(), header.getKeySalt(), encryptionMode, padding);
    }
    byte[] blockKey = new byte[4];
    LittleEndian.putInt(blockKey, 0, block);
    byte[] iv = generateIv(header.getHashAlgorithm(), header.getKeySalt(), blockKey, header.getBlockSize());
    AlgorithmParameterSpec aps;
    if (header.getCipherAlgorithm() == CipherAlgorithm.rc2) {
        aps = new RC2ParameterSpec(skey.getEncoded().length * 8, iv);
    } else {
        aps = new IvParameterSpec(iv);
    }
    existing.init(encryptionMode, skey, aps);
    return existing;
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) EncryptionHeader(org.apache.poi.poifs.crypt.EncryptionHeader)

Example 14 with RC2ParameterSpec

use of javax.crypto.spec.RC2ParameterSpec in project poi by apache.

the class CryptoFunctions method getCipher.

/**
     * Initialize a new cipher object with the given cipher properties
     * If the given algorithm is not implemented in the JCE, it will try to load it from the bouncy castle
     * provider.
     *
     * @param key the secrect key
     * @param cipherAlgorithm the cipher algorithm
     * @param chain the chaining mode
     * @param vec the initialization vector (IV), can be null
     * @param cipherMode Cipher.DECRYPT_MODE or Cipher.ENCRYPT_MODE
     * @param padding the padding (null = NOPADDING, ANSIX923Padding, PKCS5Padding, PKCS7Padding, ISO10126Padding, ...)
     * @return the requested cipher
     * @throws GeneralSecurityException
     * @throws EncryptedDocumentException if the initialization failed or if an algorithm was specified,
     *   which depends on a missing bouncy castle provider 
     */
public static Cipher getCipher(Key key, CipherAlgorithm cipherAlgorithm, ChainingMode chain, byte[] vec, int cipherMode, String padding) {
    int keySizeInBytes = key.getEncoded().length;
    if (padding == null)
        padding = "NoPadding";
    try {
        // Ensure the JCE policies files allow for this sized key
        if (Cipher.getMaxAllowedKeyLength(cipherAlgorithm.jceId) < keySizeInBytes * 8) {
            throw new EncryptedDocumentException("Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files");
        }
        Cipher cipher;
        if (cipherAlgorithm == CipherAlgorithm.rc4) {
            cipher = Cipher.getInstance(cipherAlgorithm.jceId);
        } else if (cipherAlgorithm.needsBouncyCastle) {
            registerBouncyCastle();
            cipher = Cipher.getInstance(cipherAlgorithm.jceId + "/" + chain.jceId + "/" + padding, "BC");
        } else {
            cipher = Cipher.getInstance(cipherAlgorithm.jceId + "/" + chain.jceId + "/" + padding);
        }
        if (vec == null) {
            cipher.init(cipherMode, key);
        } else {
            AlgorithmParameterSpec aps;
            if (cipherAlgorithm == CipherAlgorithm.rc2) {
                aps = new RC2ParameterSpec(key.getEncoded().length * 8, vec);
            } else {
                aps = new IvParameterSpec(vec);
            }
            cipher.init(cipherMode, key, aps);
        }
        return cipher;
    } catch (GeneralSecurityException e) {
        throw new EncryptedDocumentException(e);
    }
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) GeneralSecurityException(java.security.GeneralSecurityException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Aggregations

RC2ParameterSpec (javax.crypto.spec.RC2ParameterSpec)14 IOException (java.io.IOException)3 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)3 Cipher (javax.crypto.Cipher)3 IvParameterSpec (javax.crypto.spec.IvParameterSpec)3 IllegalArgumentException (java.lang.IllegalArgumentException)2 AlgorithmParameters (java.security.AlgorithmParameters)2 GeneralSecurityException (java.security.GeneralSecurityException)1 Key (java.security.Key)1 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)1 SecretKey (javax.crypto.SecretKey)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)1 EncryptionHeader (org.apache.poi.poifs.crypt.EncryptionHeader)1