Search in sources :

Example 86 with KeyGenerator

use of javax.crypto.KeyGenerator in project BaseProject by fly803.

the class DESUtils method initKey.

/*
     * 生成密钥
     */
public static byte[] initKey() throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance("DES");
    keyGen.init(56);
    SecretKey secretKey = keyGen.generateKey();
    return secretKey.getEncoded();
}
Also used : SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator)

Example 87 with KeyGenerator

use of javax.crypto.KeyGenerator in project BaseProject by fly803.

the class AESUtils method geneKey.

/**
 *     通过KeyGenerator的init(keySize)方法进行初始化,而不是通过传递SecureRandom对象进行初始化也可以达到上面的效果,每次生成的密钥都
 *     可能是不一样的。但是对应的keySize的指定一定要正确,AES算法的keySize是128。
 *
 *     但是这种每次生成出来的密钥都是不同的情况下,我们需要把加密用的密钥存储起来,以供解密的时候使用,不然就没法进行解密了。
 * @return
 * @throws Exception
 */
private SecretKey geneKey() throws Exception {
    // 获取一个密钥生成器实例
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(128);
    SecretKey secretKey = keyGenerator.generateKey();
    return secretKey;
}
Also used : SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator)

Example 88 with KeyGenerator

use of javax.crypto.KeyGenerator in project BaseProject by fly803.

the class AESUtils method generateKey.

private SecretKey generateKey() throws Exception {
    // 获取一个密钥生成器实例
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    SecureRandom random = new SecureRandom();
    // 设置加密用的种子,密钥
    random.setSeed("123456".getBytes());
    keyGenerator.init(random);
    SecretKey secretKey = keyGenerator.generateKey();
    return secretKey;
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator)

Example 89 with KeyGenerator

use of javax.crypto.KeyGenerator in project SilverKing by Morgan-Stanley.

the class AESEncrypterDecrypter method generateKey.

private static SecretKey generateKey() throws NoSuchAlgorithmException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(128);
    SecretKey key = keyGenerator.generateKey();
    return key;
}
Also used : SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator)

Example 90 with KeyGenerator

use of javax.crypto.KeyGenerator in project latke by b3log.

the class Crypts method decryptByAES.

/**
 * Decrypts by AES.
 *
 * @param content the specified content to decrypt
 * @param key     the specified key
 * @return original content
 * @see #encryptByAES(String, String)
 */
public static String decryptByAES(final String content, final String key) {
    try {
        final byte[] data = decodeHex(content.toCharArray());
        final KeyGenerator kgen = KeyGenerator.getInstance("AES");
        final SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(key.getBytes());
        kgen.init(128, secureRandom);
        final SecretKey secretKey = kgen.generateKey();
        final byte[] enCodeFormat = secretKey.getEncoded();
        final SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES");
        final Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        final byte[] result = cipher.doFinal(data);
        return new String(result, "UTF-8");
    } catch (final Exception e) {
        LOGGER.log(Level.WARN, "Decrypt failed");
        return null;
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) 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