List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:org.jboss.ejb3.examples.ch05.encryption.EncryptionBean.java
/** * Initializes this service before it may handle requests * //from w ww .j ava 2 s .c o m * @throws Exception If some unexpected error occurred */ @PostConstruct public void initialize() throws Exception { // Log that we're here log.info("Initializing, part of " + PostConstruct.class.getName() + " lifecycle"); /* * Symmetric Encryption */ // Obtain parameters used in initializing the ciphers final String cipherAlgorithm = DEFAULT_ALGORITHM_CIPHER; final byte[] ciphersSalt = DEFAULT_SALT_CIPHERS; final int ciphersIterationCount = DEFAULT_ITERATION_COUNT_CIPHERS; final String ciphersPassphrase = this.getCiphersPassphrase(); // Obtain key and param spec for the ciphers final KeySpec ciphersKeySpec = new PBEKeySpec(ciphersPassphrase.toCharArray(), ciphersSalt, ciphersIterationCount); final SecretKey ciphersKey = SecretKeyFactory.getInstance(cipherAlgorithm).generateSecret(ciphersKeySpec); final AlgorithmParameterSpec paramSpec = new PBEParameterSpec(ciphersSalt, ciphersIterationCount); // Create and init the ciphers this.encryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm()); this.decryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm()); encryptionCipher.init(Cipher.ENCRYPT_MODE, ciphersKey, paramSpec); decryptionCipher.init(Cipher.DECRYPT_MODE, ciphersKey, paramSpec); // Log log.info("Initialized encryption cipher: " + this.encryptionCipher); log.info("Initialized decryption cipher: " + this.decryptionCipher); /* * One-way Hashing */ // Get the algorithm for the MessageDigest final String messageDigestAlgorithm = this.getMessageDigestAlgorithm(); // Create the MessageDigest try { this.messageDigest = MessageDigest.getInstance(messageDigestAlgorithm); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Could not obtain the " + MessageDigest.class.getSimpleName() + " for algorithm: " + messageDigestAlgorithm, e); } log.info("Initialized MessageDigest for one-way hashing: " + this.messageDigest); }
From source file:com.kuzumeji.platform.standard.SecurityService.java
/** * ???/*from w ww . ja v a 2 s .c o m*/ * <dl> * <dt>? * <dd>PBKDF2??????? * </dl> * @param password * @param salt * @return ?(?) */ public byte[] createCommonKey(final char[] password, final byte[] salt) { try { return SecretKeyFactory.getInstance(PBKDF2_ALGO_NAME) .generateSecret(new PBEKeySpec(password, salt, PBE_ITER_COUNT, PBE_KEY_LENGTH)).getEncoded(); } catch (final NoSuchAlgorithmException | InvalidKeySpecException e) { throw new RuntimeException(e); } }
From source file:org.kuali.rice.krad.devtools.maintainablexml.EncryptionService.java
private SecretKey unwrapEncodedKeyOld(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()); // If decoding was done with commons-codec 1.3 and the key not ended with '=' bytes[6] = 1;//from ww w .j ava 2 s. c o m bytes[7] = 1; SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES"); DESKeySpec keyspec = new DESKeySpec(bytes); SecretKey k = desFactory.generateSecret(keyspec); return k; }
From source file:com.dc.tes.License.java
public static byte[] decrypt(byte[] src, byte[] key) throws Exception { // DES????// w w w .j a v a2 s . com SecureRandom sr = new SecureRandom(); // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec?? // SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher?? Cipher cipher = Cipher.getInstance("DES"); // Cipher cipher.init(Cipher.DECRYPT_MODE, securekey, sr); // ?? // ?? return cipher.doFinal(src); }
From source file:org.kawanfw.commons.util.convert.Pbe.java
/** * Encrypt or decrypt a file using a password * //from w w w.ja v a2s. co m * @param mode * Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE * @param fileIn * the file to encrypt or Decrypt. * @param fileOut * the resulting encrypted/decrypted file * @param password * the password to use * * @throws Exception */ private void cipher(int mode, File fileIn, File fileOut, char[] password) throws Exception { if (mode != Cipher.ENCRYPT_MODE && mode != Cipher.DECRYPT_MODE) { throw new IllegalArgumentException("mode is not Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE!"); } if (fileIn == null) { throw new IllegalArgumentException("in File can not be null!"); } if (fileOut == null) { throw new IllegalArgumentException("out File can not be null!"); } if (password == null) { throw new IllegalArgumentException("password can not be null!"); } PBEKeySpec pbeKeySpec; PBEParameterSpec pbeParamSpec; SecretKeyFactory keyFac; // Salt byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; // Iteration count int count = 1; // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt, count); pbeKeySpec = new PBEKeySpec(password); keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); // Create PBE Cipher Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); // Initialize PBE Cipher with key and parameters pbeCipher.init(mode, pbeKey, pbeParamSpec); InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(fileIn)); out = new BufferedOutputStream(new FileOutputStream(fileOut)); byte[] input = new byte[2048 * 10]; int bytesRead; while ((bytesRead = in.read(input)) != -1) { byte[] output = pbeCipher.update(input, 0, bytesRead); if (output != null) out.write(output); } byte[] output = pbeCipher.doFinal(); if (output != null) out.write(output); out.flush(); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } }
From source file:org.opensafety.hishare.util.implementation.EncryptionImpl.java
private SecretKey generateKey(String password, byte[] salt) throws CryptographyException { PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, pbeIterationCount, pbeKeyLength); SecretKeyFactory factory;/*from www .ja v a2 s .c o m*/ SecretKey tmp; try { factory = SecretKeyFactory.getInstance(pbeAlgorithm); tmp = factory.generateSecret(pbeKeySpec); } catch (NoSuchAlgorithmException e) { throw new CryptographyException(e.getMessage()); } catch (InvalidKeySpecException e) { throw new CryptographyException(e.getMessage()); } SecretKey secret = new SecretKeySpec(tmp.getEncoded(), keyGenerator); return secret; }
From source file:org.coronastreet.gpxconverter.AccountManager.java
public static String encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8"))); }
From source file:com.yaxin.utils.StringUtils.java
/** * //from w ww. j a v a 2 s.co m * @param src * @param key 8 * @return * @throws Exception */ public static byte[] decrypt(byte[] src, byte[] key) throws Exception { // DES SecureRandom sr = new SecureRandom(); // DESKeySpec DESKeySpec dks = new DESKeySpec(key); // DESKeySpec // SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher Cipher cipher = Cipher.getInstance(DES); // Cipher cipher.init(Cipher.DECRYPT_MODE, securekey, sr); // // return cipher.doFinal(src); }
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:com.bcmcgroup.flare.client.ClientUtil.java
/** * Encrypt plain text using AES/*from w ww.j a va2 s . c om*/ * * @param plainText the String text to be encrypted in AES * @return the encrypted text String * */ public static String encrypt(String plainText) { try { byte[] saltBytes = salt.getBytes("UTF-8"); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(seeds, saltBytes, iterations, keySize); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(ivBytes)); byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8")); return new Base64().encodeAsString(encryptedTextBytes); } catch (NoSuchAlgorithmException e) { logger.error("NoSuchAlgorithmException when attempting to encrypt a string. "); } catch (InvalidKeySpecException e) { logger.error("InvalidKeySpecException when attempting to encrypt a string. "); } catch (NoSuchPaddingException e) { logger.error("NoSuchPaddingException when attempting to encrypt a string. "); } catch (IllegalBlockSizeException e) { logger.error("IllegalBlockSizeException when attempting to encrypt a string. "); } catch (BadPaddingException e) { logger.error("BadPaddingException when attempting to encrypt a string. "); } catch (UnsupportedEncodingException e) { logger.error("UnsupportedEncodingException when attempting to encrypt a string. "); } catch (InvalidKeyException e) { logger.error("InvalidKeyException when attempting to encrypt a string. "); } catch (InvalidAlgorithmParameterException e) { logger.error("InvalidAlgorithmParameterException when attempting to encrypt a string. "); } return null; }