Search in sources :

Example 51 with KeyGenerator

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

the class myKeyGenerator method testInitParams.

/*
     * Test for <code>init(AlgorithmParameterSpec params)</code> and
     * <code>init(AlgorithmParameterSpec params, SecureRandom random)</code> methods
     * Assertion: throws InvalidAlgorithmParameterException when params is null
     */
public void testInitParams() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyGenerator[] kgs = createKGs();
    AlgorithmParameterSpec aps = null;
    for (int i = 0; i < kgs.length; i++) {
        try {
            kgs[i].init(aps);
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
        try {
            kgs[i].init(aps, new SecureRandom());
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 52 with KeyGenerator

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

the class CipherOutputStream1Test method test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher.

public void test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(56, new SecureRandom());
    Key key = kg.generateKey();
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, key);
    CipherOutputStream cos = new CipherOutputStream(baos, c);
    assertNotNull(cos);
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) SecureRandom(java.security.SecureRandom) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 53 with KeyGenerator

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

the class myKeyGenerator method testInitKey.

/*
     * Test for <code>init(int keysize)</code> and
     * <code>init(int keysize, SecureRandom random)</code> methods
     * Assertion: throws InvalidParameterException if keysize is wrong
     *
     */
public void testInitKey() throws Exception {
    byte flag = 0xF;
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    if (defaultAlgorithm.equals(validAlgorithmsKeyGenerator[validAlgorithmsKeyGenerator.length - 1])) {
        return;
    }
    int[] size = { Integer.MIN_VALUE, -1, 0, 112, 168, Integer.MAX_VALUE };
    KeyGenerator[] kgs = createKGs();
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < kgs.length; i++) {
        for (int j = 0; j < size.length; j++) {
            try {
                kgs[i].init(size[j]);
                flag &= 0xE;
            } catch (InvalidParameterException ignore) {
                flag &= 0xD;
            }
            try {
                kgs[i].init(size[j], random);
                flag &= 0xB;
            } catch (InvalidParameterException ignore) {
                flag &= 0x7;
            }
        }
    }
    assertTrue(flag == 0);
}
Also used : InvalidParameterException(java.security.InvalidParameterException) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator)

Example 54 with KeyGenerator

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

the class CipherInputStream1Test method test_ConstructorLjava_io_InputStreamLjavax_crypto_Cipher.

public void test_ConstructorLjava_io_InputStreamLjavax_crypto_Cipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    ByteArrayInputStream bais = new ByteArrayInputStream(new byte[100]);
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(56, new SecureRandom());
    Key key = kg.generateKey();
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, key);
    CipherInputStream cis = new CipherInputStream(bais, c);
    assertNotNull(cis);
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SecureRandom(java.security.SecureRandom) NullCipher(javax.crypto.NullCipher) Cipher(javax.crypto.Cipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 55 with KeyGenerator

use of javax.crypto.KeyGenerator in project uavstack by uavorg.

the class EncryptionHelper method decryptByAesCBC.

/**
 * AES CBC解密
 *
 * 数据块:128位
 *
 * @param hexadecimalMsg
 *            :16进制的加密字符串
 * @param sKey
 *            :加密key(16位)
 * @param sKeyFormat
 *            :skey是否格式处理
 * @param ivParameter
 *            :偏移量(16位)使用CBC模式,需要一个向量iv,可增加加密算法的强度
 * @param encoding
 *            :编码
 * @return :解密后字符串
 */
public static String decryptByAesCBC(String hexadecimalMsg, String sKey, boolean sKeyFormat, String ivParameter, String encoding) {
    try {
        byte[] keyByte = null;
        if (sKeyFormat) {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(sKey.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            keyByte = secretKey.getEncoded();
        } else {
            keyByte = sKey.getBytes(encoding);
        }
        SecretKeySpec skeySpec = new SecretKeySpec(keyByte, "AES");
        // 算法/模式/补码方式
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] msgByte = parseHexStr2Byte(hexadecimalMsg);
        byte[] resultByte = cipher.doFinal(msgByte);
        return new String(resultByte, encoding);
    } catch (Exception ex) {
        ex.printStackTrace();
        return "";
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

KeyGenerator (javax.crypto.KeyGenerator)464 SecretKey (javax.crypto.SecretKey)343 Test (org.junit.Test)106 ArrayList (java.util.ArrayList)104 SecureRandom (java.security.SecureRandom)99 Document (org.w3c.dom.Document)98 InputStream (java.io.InputStream)95 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)93 ByteArrayOutputStream (java.io.ByteArrayOutputStream)87 NodeList (org.w3c.dom.NodeList)82 Cipher (javax.crypto.Cipher)79 ByteArrayInputStream (java.io.ByteArrayInputStream)75 XMLStreamReader (javax.xml.stream.XMLStreamReader)68 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)68 DocumentBuilder (javax.xml.parsers.DocumentBuilder)62 Key (java.security.Key)58 QName (javax.xml.namespace.QName)47 IOException (java.io.IOException)45 SecurePart (org.apache.xml.security.stax.ext.SecurePart)40 SecretKeySpec (javax.crypto.spec.SecretKeySpec)39