List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:net.nicholaswilliams.java.licensing.encryption.Encryptor.java
private static SecretKey getSecretKey(char[] passphrase) { try {//from w ww .j a v a 2 s. c om PBEKeySpec keySpec = new PBEKeySpec(passphrase, Encryptor.salt, Encryptor.iterationCount, Encryptor.aesKeyLength); byte[] shortKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec).getEncoded(); byte[] intermediaryKey = new byte[Encryptor.aesKeyLength / 8]; for (int i = 0, j = 0; i < Encryptor.aesKeyLength / 8; i++) { intermediaryKey[i] = shortKey[j]; if (++j == shortKey.length) j = 0; } return new SecretKeySpec(intermediaryKey, "AES"); } catch (NoSuchAlgorithmException e) { throw new AlgorithmNotSupportedException("DES with an MD5 Digest", e); } catch (InvalidKeySpecException e) { throw new InappropriateKeySpecificationException(e); } }
From source file:com.grazerss.EntryManager.java
private static SecretKey getSecretKey() throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException { DESKeySpec keySpec = new DESKeySpec("EntryManager.class".getBytes("UTF8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(keySpec); return secretKey; }
From source file:org.apache.xml.security.test.encryption.XMLCipherTester.java
public void testTrippleDesElementCipher() throws Exception { Document d = document(); // source Document ed = null; // target Document dd = null; // target Element e = (Element) d.getElementsByTagName(element()).item(index()); Element ee = null;/*from w w w . java 2s.co m*/ String source = null; String target = null; if (haveISOPadding) { source = toString(d); // prepare for encryption byte[] passPhrase = "24 Bytes per DESede key!".getBytes(); DESedeKeySpec keySpec = new DESedeKeySpec(passPhrase); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); SecretKey key = keyFactory.generateSecret(keySpec); // encrypt cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES); cipher.init(XMLCipher.ENCRYPT_MODE, key); ed = cipher.doFinal(d, e); //decrypt cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES); cipher.init(XMLCipher.DECRYPT_MODE, key); ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0); EncryptedData encryptedData = cipher.loadEncryptedData(ed, ee); Assert.assertEquals(encryptedData.getEncryptionMethod().getAlgorithm(), XMLCipher.TRIPLEDES); dd = cipher.doFinal(ed, ee); target = toString(dd); Assert.assertEquals(source, target); } else { log.warn("Test testTrippleDesElementCipher skipped as necessary algorithms not available"); } }
From source file:org.owasp.webgoat.lessons.Encoding.java
/** * Convenience method for encrypting a string. * /* w w w .j ava 2 s.c om*/ * @param str * Description of the Parameter * @param pw * Description of the Parameter * @return String the encrypted string. * @exception SecurityException * Description of the Exception */ public static synchronized String encryptString(String str, String pw) throws SecurityException { try { PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20); SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); Cipher passwordEncryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding"); char[] pass = pw.toCharArray(); SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(pass)); passwordEncryptCipher.init(Cipher.ENCRYPT_MODE, k, ps); byte[] utf8 = str.getBytes("UTF-8"); byte[] enc = passwordEncryptCipher.doFinal(utf8); //return encoder.encode(enc); return Base64.encodeBase64String(enc); } catch (Exception e) { return ("Encryption error"); } }
From source file:com.denel.facepatrol.MainActivity.java
private static SecretKey generateKey(char[] passphraseOrPin, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { // Number of PBKDF2 hardening rounds to use. Larger values increase // computation time. You should select a value that causes computation // to take >100ms. final int iterations = 1000; // Generate a 256-bit key final int outputKeyLength = 256; SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec keySpec = new PBEKeySpec(passphraseOrPin, salt, iterations, outputKeyLength); SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); return secretKey; }
From source file:LicenseGenerator.java
/** * /*from w w w . j av a 2 s. c om*/ * * * @param src * ?? * * @param key * 8? * * @return ?? * * @throws Exception * */ public static byte[] encrypt(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.ENCRYPT_MODE, securekey, sr); // ?? // ?? return cipher.doFinal(src); }
From source file:bioLockJ.module.agent.MailAgent.java
private static String encrypt(final String password) throws GeneralSecurityException, UnsupportedEncodingException { final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); final SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); final Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return base64Encode(pbeCipher.doFinal(password.getBytes("UTF-8"))); }
From source file:com.aurel.track.vc.bl.VersionControlBL.java
private static String encrypt(String clearText, char[] password) { // Create PBE parameter set PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count); byte[] ciphertext = { 0 }; PBEKeySpec pbeKeySpec = new PBEKeySpec(password); try {/*from ww w .ja v a2s. co m*/ SecretKeyFactory 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(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); // Encrypt the cleartext ciphertext = pbeCipher.doFinal(clearText.getBytes()); } catch (Exception e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); } return new String(Base64.encodeBase64String(ciphertext)); }
From source file:org.alfresco.repo.lotus.ws.impl.auth.LtpaAuthenticator.java
private byte[] getSecretKey(String ltpa3DESKey, String ltpaPassword) throws Exception { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(ltpaPassword.getBytes());//from w w w . ja va 2s . com byte[] hash3DES = new byte[24]; System.arraycopy(md.digest(), 0, hash3DES, 0, 20); Arrays.fill(hash3DES, 20, 24, (byte) 0); final Cipher cipher = Cipher.getInstance(DES_DECRIPTING_ALGORITHM); final KeySpec keySpec = new DESedeKeySpec(hash3DES); final Key secretKey = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] secret = cipher.doFinal(Base64.decodeBase64(ltpa3DESKey.getBytes())); return secret; }
From source file:com.thruzero.common.core.security.SimpleCipher.java
/** * Construct using the {@code salt}, {@code passPhrase} and {@code iterationCount} defined by the given * {@code simpleCipherConfiguration}.//from ww w . j a va 2s . com * * @throws SimpleCipherException */ public SimpleCipher(final SimpleCipherConfiguration simpleCipherConfiguration) throws SimpleCipherException { try { int count = simpleCipherConfiguration.getIterationCount(); byte[] salt = simpleCipherConfiguration.getSalt(); KeySpec keySpec = new PBEKeySpec(simpleCipherConfiguration.getPassPhrase(), salt, count); AlgorithmParameterSpec parameterSpec = new PBEParameterSpec(salt, count); SecretKey key = SecretKeyFactory.getInstance(PBE_WITH_MD5_AND_DES).generateSecret(keySpec); encryptionCipher = Cipher.getInstance(key.getAlgorithm()); encryptionCipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec); decryptionCipher = Cipher.getInstance(key.getAlgorithm()); decryptionCipher.init(Cipher.DECRYPT_MODE, key, parameterSpec); } catch (InvalidAlgorithmParameterException e) { throw new SimpleCipherException( "Couldn't instantiate SimpleCipher because of " + ExceptionUtils.getMessage(e), e); } catch (Exception e) { throw new SimpleCipherException( "Couldn't instantiate SimpleCipher because of " + ExceptionUtils.getMessage(e), e); } }