List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:org.jamwiki.utils.Encryption.java
/** * Create the encryption key value./* w w w.j a v a2 s.co m*/ * * @return An encryption key value implementing the DES encryption algorithm. */ private static SecretKey createKey() throws GeneralSecurityException, UnsupportedEncodingException { byte[] bytes = ENCRYPTION_KEY.getBytes("UTF8"); DESKeySpec spec = new DESKeySpec(bytes); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM); return keyFactory.generateSecret(spec); }
From source file:com.hernandez.rey.crypto.TripleDES.java
/** * Read a TripleDES secret key from the specified file * /*from w w w . j a va2s . co m*/ * @param keyFile key file to read * @return secret key appropriate for encryption/decryption with 3DES * @throws IOException * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws InvalidKeySpecException */ public static SecretKey readKey(final File keyFile) throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException { // Read the raw bytes from the keyfile final DataInputStream in = new DataInputStream(new FileInputStream(keyFile)); final byte[] rawkey = new byte[(int) keyFile.length()]; in.readFully(rawkey); in.close(); // Convert the raw bytes to a secret key like this final DESedeKeySpec keyspec = new DESedeKeySpec(rawkey); final SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede"); final SecretKey key = keyfactory.generateSecret(keyspec); return key; }
From source file:org.artifactory.security.crypto.CryptoHelper.java
private static SecretKey generatePbeKey(String password) { try {/* ww w . jav a 2 s .c om*/ PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SYM_ALGORITHM); SecretKey secretKey = keyFactory.generateSecret(pbeKeySpec); return secretKey; } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("No such algorithm: " + e.getMessage()); } catch (InvalidKeySpecException e) { throw new RuntimeException("Unexpected exception: ", e); } }
From source file:eu.eubrazilcc.lvl.storage.security.shiro.CryptProvider.java
/** * Joins the tokens and dates provided as parameters with a salt generated from {@link #DEFAULT_STRENGTH} random bytes and computes a digest using * the PBKDF2 key derivation function.// w w w .j a v a2s . c om * @param tokens - list of input tokens * @param dates - list of input dates * @return a digest of the input parameters (tokens and dates). * @throws IOException is thrown if the computation of the digest fails. * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html">JCA Standard Algorithm Name Documentation</a> */ private static byte[] digest(final @Nullable String[] tokens, @Nullable final Date[] dates) throws IOException { try { String mixName = ""; if (tokens != null) { for (final String token : tokens) { mixName += (token != null ? token.trim() : ""); } } if (dates != null) { for (final Date date : dates) { mixName += (date != null ? Long.toString(date.getTime()) : ""); } } // compute digest final byte[] bytesOfMixName = mixName.getBytes(UTF_8.name()); final char[] charOfMixName = new String(bytesOfMixName).toCharArray(); final byte[] salt = generateSalt(SALT_BYTES_SIZE); final SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); final PBEKeySpec spec = new PBEKeySpec(charOfMixName, salt, KEY_DEVIATION_ITERATIONS, SALT_BYTES_SIZE * 8); return factory.generateSecret(spec).getEncoded(); } catch (Exception e) { throw new IOException("Digest computation has failed", e); } }
From source file:org.jets3t.service.security.EncryptionUtil.java
/** * Constructs class configured with the provided password, and set up to use the encryption * method specified./*from ww w. jav a 2 s . c o m*/ * * @param encryptionKey * the password to use for encryption/decryption. * @param algorithm * the Java name of an encryption algorithm to use, eg PBEWithMD5AndDES * @param version * the version of encyption to use, for historic and future compatibility. * Unless using an historic version, this should always be * {@link #DEFAULT_VERSION} * * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeySpecException */ public EncryptionUtil(String encryptionKey, String algorithm, String version) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException { this.algorithm = algorithm; this.version = version; if (log.isDebugEnabled()) { log.debug("Cryptographic properties: algorithm=" + this.algorithm + ", version=" + this.version); } if (!DEFAULT_VERSION.equals(version)) { throw new RuntimeException("Unrecognised crypto version setting: " + version); } PBEKeySpec keyspec = new PBEKeySpec(encryptionKey.toCharArray(), salt, ITERATION_COUNT, 32); SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm); key = skf.generateSecret(keyspec); algParamSpec = new PBEParameterSpec(salt, ITERATION_COUNT); }
From source file:jef.tools.security.EncrypterUtil.java
/** * ??DESKey//www .j av a 2 s.c o m * * @param password * @return */ public static final SecretKey toDESKey(String password) { byte[] bb = password.getBytes(); Assert.isTrue(bb.length > 7, "the secretKey for DES must be 8 bytes at least."); try { KeySpec keySpec = new DESKeySpec(bb); SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec); return key; } catch (GeneralSecurityException e) { throw new RuntimeException(e.getMessage()); } }
From source file:com.anteam.demo.codec.cipher.symmetric.DESTest.java
License:asdf
public byte[] testDESedeDe(byte[] encryptedText) { try {/*from w w w .j a v a 2 s. 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.DECRYPT_MODE, theKey, IvParameters); return cipher.doFinal(encryptedText); } catch (Exception exc) { exc.printStackTrace(); } return null; }
From source file:com.cherong.mock.common.base.util.EncryptionUtil.java
/** * DES bytKey8?/*from w w w . j a va 2 s .com*/ * * @param bytE * @param bytKey * @return * @throws Exception */ public static byte[] decryptByDES(byte[] bytE, byte[] bytKey) throws Exception { DESKeySpec desKS = new DESKeySpec(bytKey); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey sk = skf.generateSecret(desKS); Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM); cip.init(Cipher.DECRYPT_MODE, sk); return cip.doFinal(bytE); }
From source file:com.mb.framework.util.SecurityUtil.java
/** * //from ww w . j a v a 2 s.c o m * This method is used for decrypt by using Algorithm - AES/CBC/PKCS5Padding * * @param String * @return String * @throws Exception */ @SuppressWarnings("static-access") public static String decryptAESPBKDF2(String encryptedText) throws Exception { byte[] saltBytes = salt.getBytes("UTF-8"); byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText); // Derive the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), saltBytes, pswdIterations, keySize); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); // Decrypt the message Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING_ALGO); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes)); byte[] decryptedTextBytes = null; try { decryptedTextBytes = cipher.doFinal(encryptedTextBytes); } catch (IllegalBlockSizeException e) { LOGGER.error("error " + e.getMessage()); } catch (BadPaddingException e) { LOGGER.error("error " + e.getMessage()); } return new String(decryptedTextBytes); }
From source file:org.kuali.rice.core.impl.encryption.DemonstrationGradeEncryptionServiceImpl.java
private SecretKey unwrapEncodedKey(String key) 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(key.getBytes()); SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES"); DESKeySpec keyspec = new DESKeySpec(bytes); SecretKey k = desFactory.generateSecret(keyspec); return k;// w ww . j av a 2 s . c om }