List of usage examples for javax.crypto SecretKeyFactory generateSecret
public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException
From source file:com.anteam.demo.codec.cipher.symmetric.DESTest.java
License:asdf
public byte[] testDESedeEn(String plainText) { try {//www . ja v a 2s .c o m // Create an array to hold the key byte[] encryptKey = "This is a test DESede key".getBytes(); // Create a DESede key spec from the key DESedeKeySpec spec = new DESedeKeySpec(encryptKey); // Get the secret key factor for generating DESede keys SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); // Generate a DESede SecretKey object SecretKey theKey = keyFactory.generateSecret(spec); // Create a DESede Cipher Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); // Create an initialization vector (necessary for CBC mode) IvParameterSpec IvParameters = new IvParameterSpec( new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C }); // Initialize the cipher and put it into encrypt mode cipher.init(Cipher.ENCRYPT_MODE, theKey, IvParameters); return cipher.doFinal(plainText.getBytes()); } catch (Exception exc) { exc.printStackTrace(); } return null; }
From source file:com.anteam.demo.codec.cipher.symmetric.DESTest.java
License:asdf
public byte[] testDESedeDe(byte[] encryptedText) { try {/* ww w. ja va 2 s . com*/ // Create an array to hold the key byte[] encryptKey = "This is a test DESede key".getBytes(); // Create a DESede key spec from the key DESedeKeySpec spec = new DESedeKeySpec(encryptKey); // Get the secret key factor for generating DESede keys SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); // Generate a DESede SecretKey object SecretKey theKey = keyFactory.generateSecret(spec); // Create a DESede Cipher Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); // Create an initialization vector (necessary for CBC mode) IvParameterSpec IvParameters = new IvParameterSpec( new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C }); // Initialize the cipher and put it into encrypt mode cipher.init(Cipher.DECRYPT_MODE, theKey, IvParameters); return cipher.doFinal(encryptedText); } catch (Exception exc) { exc.printStackTrace(); } return null; }
From source file:io.hawkcd.agent.services.SecurityService.java
private Key generateKey() throws Exception { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); char[] password = PASSWORD.toCharArray(); byte[] salt = getBytes(SALT); KeySpec spec = new PBEKeySpec(password, salt, 65536, 128); SecretKey tmp = factory.generateSecret(spec); byte[] encoded = tmp.getEncoded(); return new SecretKeySpec(encoded, "AES"); }
From source file:uk.ac.cam.cl.dtg.segue.auth.SegueLocalAuthenticator.java
/** * Compute the hash of a string using the preconfigured hashing function. * * @param str//from w w w .jav a 2 s . c om * - string to hash * @param salt * - random string to use as a salt. * @param keyLength * - the key length * @return a byte array of the hash * @throws NoSuchAlgorithmException * - if the configured algorithm is not valid. * @throws InvalidKeySpecException * - if the preconfigured key spec is invalid. */ private byte[] computeHash(final String str, final String salt, final int keyLength) throws NoSuchAlgorithmException, InvalidKeySpecException { char[] strChars = str.toCharArray(); byte[] saltBytes = salt.getBytes(); PBEKeySpec spec = new PBEKeySpec(strChars, saltBytes, ITERATIONS, keyLength); SecretKeyFactory key = SecretKeyFactory.getInstance(CRYPTO_ALOGRITHM); return key.generateSecret(spec).getEncoded(); }
From source file:com.anteam.demo.codec.cipher.symmetric.DESedeCoder.java
/** * Decodes a byte array and returns the results as a byte array. * * @param source A byte array which has been encoded with the appropriate encoder * @return ?byte.source, null//w ww. jav a2 s . c o m * @throws org.apache.commons.codec.DecoderException A decoder exception is thrown if a Decoder encounters a failure condition during the decode process. */ @Override public byte[] decode(byte[] source) throws DecoderException { if (source == null) { return null; } try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_NAME); SecretKey secretKey = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(CIPHER_NAME); cipher.init(Cipher.DECRYPT_MODE, secretKey, IvParameters); return cipher.doFinal(source); } catch (Exception e) { LOG.error(":" + key + ":" + source, e); throw new DecoderException(":" + key + ":" + source, e); } }
From source file:org.alfresco.util.encryption.impl.AES256PasswordBasedEncrypter.java
/** * Constructor for the class./* www . j a va2 s . c om*/ * * @param password The password to use when encrypting data <i>(must not be null, empty or blank)</i>. */ public AES256PasswordBasedEncrypter(final char[] password) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException { // PRECONDITIONS assert password != null && password.length > 0 : "password must not be null or empty"; // Body SecretKeyFactory factory = SecretKeyFactory.getInstance(PASSWORD_ALGORITHM); KeySpec spec = new PBEKeySpec(password, SALT, NUM_ITERATIONS, KEY_LENGTH); secretKey = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), SECRET_KEY_ALGORITHM); }
From source file:com.anteam.demo.codec.cipher.symmetric.DESedeCoder.java
/** * Encodes a byte array and return the encoded data as a byte array. * * @param source Data to be encoded//from w ww. jav a 2 s.com * @return ?byte.source, null * @throws org.apache.commons.codec.EncoderException thrown if the Encoder encounters a failure condition during the encoding process. */ @Override public byte[] encode(byte[] source) throws EncoderException { if (source == null) { return null; } try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_NAME); SecretKey secretKey = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(CIPHER_NAME); cipher.init(Cipher.ENCRYPT_MODE, secretKey, IvParameters); return cipher.doFinal(source); } catch (Exception e) { LOG.error(":" + key + ":" + source, e); throw new EncoderException(":" + key + ":" + source, e); } }
From source file:org.kuali.rice.kew.documentoperation.web.DocumentContentOperationAction.java
private SecretKey getSecretKey(String encryptionKey) throws Exception { KeyGenerator keygen = KeyGenerator.getInstance("DES"); SecretKey desKey = keygen.generateKey(); // Create the cipher Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init((Cipher.UNWRAP_MODE), desKey); byte[] bytes = Base64.decodeBase64(encryptionKey.getBytes()); SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES"); DESKeySpec keyspec = new DESKeySpec(bytes); desKey = desFactory.generateSecret(keyspec); // Create the cipher cipher.init((Cipher.WRAP_MODE), desKey); return desKey; }
From source file:org.mayocat.security.DefaultCipher.java
private String crypt(String input, Mode mode) throws EncryptionException { if (Strings.isNullOrEmpty(this.configuration.getEncryptionKey())) { throw new EncryptionException("Invalid or missing cookie encryption key in configuration file. " + "You MUST specify a key in order to support cookie authentication."); }//from w w w . j a v a 2s. c o m try { byte[] in = input.getBytes(); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); byte[] keyBytes = this.configuration.getEncryptionKey().getBytes("UTF-8"); DESKeySpec desKeySpec = new DESKeySpec(keyBytes); SecretKey key = keyFactory.generateSecret(desKeySpec); javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DES/ECB/PKCS5Padding"); IvParameterSpec spec = null; if (cipher.getParameters() != null) { spec = cipher.getParameters().getParameterSpec(IvParameterSpec.class); } switch (mode) { case CRYPT: default: if (spec != null) { cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key, spec); } else { cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key); } byte[] encrypted = cipher.doFinal(in); return new String(Base64.encodeBase64(encrypted)); case DECRYPT: if (spec != null) { cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key, spec); } else { cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key); } byte[] decrypted = cipher.doFinal(Base64.decodeBase64(in)); return new String(decrypted); } } catch (BadPaddingException e) { this.logger.warn("Bad padding when attempting to decipher cookies. Key changed ?"); throw new EncryptionException(e); } catch (Exception e) { this.logger.error("Fail to perform cookie crypt or decrypt operation", e); throw new EncryptionException(e); } }
From source file:graphene.util.crypto.PasswordHash.java
/** * Computes the PBKDF2 hash of a password. * /* ww w . j a v a 2 s .c om*/ * @param password * the password to hash. * @param salt * the salt * @param iterations * the iteration count (slowness factor) * @param bytes * the length of the hash to compute in bytes * @return the PBDKF2 hash of the password */ protected byte[] pbkdf2(final char[] password, final byte[] salt, final int iterations, final int bytes) throws NoSuchAlgorithmException, InvalidKeySpecException { final PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bytes * 8); final SecretKeyFactory skf = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM); return skf.generateSecret(spec).getEncoded(); }