Search in sources :

Example 91 with Cipher

use of javax.crypto.Cipher in project robovm by robovm.

the class CipherTest method testRSA_ECB_NoPadding_GetParameters_NoneProvided_Success.

private void testRSA_ECB_NoPadding_GetParameters_NoneProvided_Success(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    final PublicKey pubKey = kf.generatePublic(pubKeySpec);
    Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
    assertNull("Parameters should be null", c.getParameters());
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 92 with Cipher

use of javax.crypto.Cipher in project robovm by robovm.

the class CipherTest method assertCipherInitWithKeyUsage.

private void assertCipherInitWithKeyUsage(Certificate certificate, boolean allowMode, int mode) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    if (allowMode) {
        cipher.init(mode, certificate);
    } else {
        try {
            cipher.init(mode, certificate);
            String modeString;
            switch(mode) {
                case Cipher.ENCRYPT_MODE:
                    modeString = "ENCRYPT_MODE";
                    break;
                case Cipher.DECRYPT_MODE:
                    modeString = "DECRYPT_MODE";
                    break;
                case Cipher.WRAP_MODE:
                    modeString = "WRAP_MODE";
                    break;
                case Cipher.UNWRAP_MODE:
                    modeString = "UNWRAP_MODE";
                    break;
                default:
                    throw new AssertionError("Unknown Cipher.*_MODE " + mode);
            }
            fail("Should have had InvalidKeyException for " + modeString + " for " + certificate);
        } catch (InvalidKeyException expected) {
        }
    }
}
Also used : Cipher(javax.crypto.Cipher) InvalidKeyException(java.security.InvalidKeyException)

Example 93 with Cipher

use of javax.crypto.Cipher in project robovm by robovm.

the class CipherTest method checkCipher.

private void checkCipher(CipherTestParam p, String provider) throws Exception {
    SecretKey key = new SecretKeySpec(p.key, "AES");
    Cipher c = Cipher.getInstance(p.mode + "/PKCS5Padding", provider);
    AlgorithmParameterSpec spec = null;
    if (p.iv != null) {
        spec = new IvParameterSpec(p.iv);
    }
    c.init(Cipher.ENCRYPT_MODE, key, spec);
    final byte[] actualCiphertext = c.doFinal(p.plaintext);
    assertEquals(Arrays.toString(p.ciphertext), Arrays.toString(actualCiphertext));
    byte[] emptyCipherText = c.doFinal();
    assertNotNull(emptyCipherText);
    c.init(Cipher.DECRYPT_MODE, key, spec);
    try {
        c.updateAAD(new byte[8]);
        fail("Cipher should not support AAD");
    } catch (UnsupportedOperationException expected) {
    }
    byte[] emptyPlainText = c.doFinal(emptyCipherText);
    assertEquals(Arrays.toString(EmptyArray.BYTE), Arrays.toString(emptyPlainText));
    // empty decrypt
    {
        if (StandardNames.IS_RI) {
            assertEquals(Arrays.toString(EmptyArray.BYTE), Arrays.toString(c.doFinal()));
            c.update(EmptyArray.BYTE);
            assertEquals(Arrays.toString(EmptyArray.BYTE), Arrays.toString(c.doFinal()));
        } else if (provider.equals("BC")) {
            try {
                c.doFinal();
                fail();
            } catch (IllegalBlockSizeException expected) {
            }
            try {
                c.update(EmptyArray.BYTE);
                c.doFinal();
                fail();
            } catch (IllegalBlockSizeException expected) {
            }
        } else if (provider.equals("AndroidOpenSSL")) {
            assertNull(c.doFinal());
            c.update(EmptyArray.BYTE);
            assertNull(c.doFinal());
        } else {
            throw new AssertionError("Define your behavior here for " + provider);
        }
    }
    // .doFinal(input)
    {
        final byte[] actualPlaintext = c.doFinal(p.ciphertext);
        assertEquals(Arrays.toString(p.plaintext), Arrays.toString(actualPlaintext));
    }
    // .doFinal(input, offset, len, output)
    {
        final byte[] largerThanCiphertext = new byte[p.ciphertext.length + 5];
        System.arraycopy(p.ciphertext, 0, largerThanCiphertext, 5, p.ciphertext.length);
        final byte[] actualPlaintext = new byte[c.getOutputSize(p.ciphertext.length)];
        assertEquals(p.plaintext.length, c.doFinal(largerThanCiphertext, 5, p.ciphertext.length, actualPlaintext));
        assertEquals(Arrays.toString(p.plaintext), Arrays.toString(Arrays.copyOfRange(actualPlaintext, 0, p.plaintext.length)));
    }
    // .doFinal(input, offset, len, output, offset)
    {
        final byte[] largerThanCiphertext = new byte[p.ciphertext.length + 10];
        System.arraycopy(p.ciphertext, 0, largerThanCiphertext, 5, p.ciphertext.length);
        final byte[] actualPlaintext = new byte[c.getOutputSize(p.ciphertext.length) + 2];
        assertEquals(p.plaintext.length, c.doFinal(largerThanCiphertext, 5, p.ciphertext.length, actualPlaintext, 1));
        assertEquals(Arrays.toString(p.plaintext), Arrays.toString(Arrays.copyOfRange(actualPlaintext, 1, p.plaintext.length + 1)));
    }
    Cipher cNoPad = Cipher.getInstance(p.mode + "/NoPadding", provider);
    cNoPad.init(Cipher.DECRYPT_MODE, key, spec);
    final byte[] actualPlaintextPadded = cNoPad.doFinal(p.ciphertext);
    assertEquals(Arrays.toString(p.plaintextPadded), Arrays.toString(actualPlaintextPadded));
    // Test wrapping a key. Every cipher should be able to wrap.
    {
        // Generate a small SecretKey for AES.
        KeyGenerator kg = KeyGenerator.getInstance("AES");
        kg.init(128);
        SecretKey sk = kg.generateKey();
        // Wrap it
        c.init(Cipher.WRAP_MODE, key, spec);
        byte[] cipherText = c.wrap(sk);
        // Unwrap it
        c.init(Cipher.UNWRAP_MODE, key, spec);
        Key decryptedKey = c.unwrap(cipherText, sk.getAlgorithm(), Cipher.SECRET_KEY);
        assertEquals("sk.getAlgorithm()=" + sk.getAlgorithm() + " decryptedKey.getAlgorithm()=" + decryptedKey.getAlgorithm() + " encryptKey.getEncoded()=" + Arrays.toString(sk.getEncoded()) + " decryptedKey.getEncoded()=" + Arrays.toString(decryptedKey.getEncoded()), sk, decryptedKey);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey)

Example 94 with Cipher

use of javax.crypto.Cipher in project robovm by robovm.

the class CipherTest method test_doFinal$BII.

public void test_doFinal$BII() throws Exception {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    byte[] b1 = new byte[30];
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    try {
        c.doFinal(b, 0, 10);
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal(b, 0, 10);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    int len1 = c.doFinal(b, 0, 16).length;
    assertEquals(16, len1);
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    int len2 = c.doFinal(b, 0, 16, b1, 0);
    assertEquals(16, len2);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    try {
        c.doFinal(b1, 0, 24);
        fail();
    } catch (BadPaddingException expected) {
    }
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) BadPaddingException(javax.crypto.BadPaddingException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 95 with Cipher

use of javax.crypto.Cipher in project robovm by robovm.

the class CipherTest method test_initWithCertificate.

public void test_initWithCertificate() throws Exception {
    /* Certificate creation notes: certificate should be valid 37273 starting
     * from 13 Nov 2008
     * If it brcomes invalidated regenerate it using following commands:
     * 1. openssl genrsa -des3 -out test.key 1024
     * 2. openssl req -new -key test.key -out test.csr
     * 3. cp test.key test.key.org
     * 4. openssl rsa -in test.key.org -out test.key
     * 5. openssl x509 -req -days 37273 -in test.csr -signkey test.key -out test.cert
     * */
    String certName = Support_Resources.getURL("test.cert");
    InputStream is = new URL(certName).openConnection().getInputStream();
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Certificate cert = cf.generateCertificate(is);
    is.close();
    Cipher c = Cipher.getInstance("RSA");
    c.init(Cipher.ENCRYPT_MODE, cert);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    try {
        c.init(Cipher.ENCRYPT_MODE, cert);
        fail();
    } catch (InvalidKeyException expected) {
    }
}
Also used : InputStream(java.io.InputStream) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) InvalidKeyException(java.security.InvalidKeyException) CertificateFactory(java.security.cert.CertificateFactory) URL(java.net.URL) Certificate(java.security.cert.Certificate)

Aggregations

Cipher (javax.crypto.Cipher)1531 SecretKeySpec (javax.crypto.spec.SecretKeySpec)516 SecretKey (javax.crypto.SecretKey)377 IvParameterSpec (javax.crypto.spec.IvParameterSpec)368 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)349 InvalidKeyException (java.security.InvalidKeyException)257 IOException (java.io.IOException)202 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)202 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)196 BadPaddingException (javax.crypto.BadPaddingException)183 GeneralSecurityException (java.security.GeneralSecurityException)161 SecretKeyFactory (javax.crypto.SecretKeyFactory)149 Key (java.security.Key)148 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)134 SecureRandom (java.security.SecureRandom)105 PrivateKey (java.security.PrivateKey)93 PublicKey (java.security.PublicKey)86 UnsupportedEncodingException (java.io.UnsupportedEncodingException)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)82 KeyGenerator (javax.crypto.KeyGenerator)78