Search in sources :

Example 6 with RC2ParameterSpec

use of javax.crypto.spec.RC2ParameterSpec in project XobotOS by xamarin.

the class PEMUtilities method crypt.

static byte[] crypt(boolean encrypt, Provider 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 EncryptionException("unknown AES encryption with private key");
        }
        sKey = getKey(password, "AES", keyBits / 8, salt);
    } else {
        throw new EncryptionException("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 EncryptionException("exception using cipher - please check password and data.", e);
    }
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) IOException(java.io.IOException)

Example 7 with RC2ParameterSpec

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

the class RC2AlgorithmParameters method main.

public static void main(String[] args) throws Exception {
    byte[] iv_1 = { (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x33, (byte) 0x33 };
    // check that RC2 is supported by our provider
    AlgorithmParameters rc2Params = AlgorithmParameters.getInstance("RC2", "SunJCE");
    // check that getAlgorithm returns "RC2"
    if (!rc2Params.getAlgorithm().equals("RC2")) {
        throw new Exception("getAlgorithm() returned " + rc2Params.getAlgorithm() + " instead of RC2");
    }
    // test parameters with effective key size and iv
    byte[] encoded = testParams(rc2Params, new RC2ParameterSpec(2, iv_1));
    // test parameters with just iv
    encoded = testParams(AlgorithmParameters.getInstance("RC2"), new RC2ParameterSpec(0, iv_1));
    // test vectors in RFC 2268
    runTests(tests);
}
Also used : RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) IOException(java.io.IOException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 8 with RC2ParameterSpec

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

the class RC2Parameters method engineInit.

protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException {
    if (!(paramSpec instanceof RC2ParameterSpec)) {
        throw new InvalidParameterSpecException("Inappropriate parameter specification");
    }
    RC2ParameterSpec rps = (RC2ParameterSpec) paramSpec;
    // check effective key size (a value of 0 means it is unspecified)
    effectiveKeySize = rps.getEffectiveKeyBits();
    if (effectiveKeySize != 0) {
        if (effectiveKeySize < 1 || effectiveKeySize > 1024) {
            throw new InvalidParameterSpecException("RC2 effective key " + "size must be between 1 and 1024 bits");
        }
        if (effectiveKeySize < 256) {
            version = EKB_TABLE[effectiveKeySize];
        } else {
            version = effectiveKeySize;
        }
    }
    this.iv = rps.getIV();
}
Also used : RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Example 9 with RC2ParameterSpec

use of javax.crypto.spec.RC2ParameterSpec in project robovm by robovm.

the class RC2ParameterSpecTest method testRC2ParameterSpec2.

/**
     * RC2ParameterSpec(int effectiveKeyBits, byte[] iv, int offset) method
     * testing. Tests that IllegalArgumentException is thrown in the case of
     * inappropriate constructor parameters and that input iv array is
     * copied to protect against subsequent modification.
     */
public void testRC2ParameterSpec2() {
    int effectiveKeyBits = 10;
    byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    int offset = 2;
    try {
        new RC2ParameterSpec(effectiveKeyBits, null, offset);
        fail("An IllegalArgumentException should be thrown " + "in the case of null iv.");
    } catch (IllegalArgumentException e) {
    }
    try {
        new RC2ParameterSpec(effectiveKeyBits, iv, 4);
        fail("An IllegalArgumentException should be thrown " + "in the case of short iv.");
    } catch (IllegalArgumentException e) {
    }
    RC2ParameterSpec ps = new RC2ParameterSpec(effectiveKeyBits, iv, offset);
    iv[offset]++;
    assertFalse("The change of iv specified in the constructor " + "should not cause the change of internal array.", iv[offset] == ps.getIV()[0]);
}
Also used : RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) IllegalArgumentException(java.lang.IllegalArgumentException)

Example 10 with RC2ParameterSpec

use of javax.crypto.spec.RC2ParameterSpec in project robovm by robovm.

the class RC2ParameterSpecTest method testGetIV.

/**
     * getIV() method testing. Tests that returned array is equal to the
     * array specified in the constructor. Checks that modification
     * of returned array does not affect the internal array. Also it checks
     * that getIV() method returns null if iv is not specified.
     */
public void testGetIV() {
    int effectiveKeyBits = 10;
    byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    RC2ParameterSpec ps = new RC2ParameterSpec(effectiveKeyBits, iv);
    byte[] result = ps.getIV();
    if (!Arrays.equals(iv, result)) {
        fail("The returned iv is not equal to the specified " + "in the constructor.");
    }
    result[0]++;
    assertFalse("The change of returned by getIV() method iv " + "should not cause the change of internal array.", result[0] == ps.getIV()[0]);
    ps = new RC2ParameterSpec(effectiveKeyBits);
    assertNull("The getIV() method should return null if the parameter " + "set does not contain iv.", ps.getIV());
}
Also used : RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec)

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