List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
private static SecretKey getKeyFromPassphrase(String passphrase, byte[] salt, int iterations) throws GeneralSecurityException { PBEKeySpec keyspec = new PBEKeySpec(passphrase.toCharArray(), salt, iterations); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA1AND128BITAES-CBC-BC"); return skf.generateSecret(keyspec); }
From source file:de.fhdo.helper.DES.java
public static String encrypt(String Text) { try {//from w w w . j a v a 2 s .c om DESKeySpec keySpec = new DESKeySpec("schluessel_stdrepository15".getBytes("UTF8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); // ENCODE plainTextPassword String byte[] cleartext = Text.getBytes("UTF8"); Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe cipher.init(Cipher.ENCRYPT_MODE, key); return Base64.encodeBase64URLSafeString(cipher.doFinal(cleartext)); } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:Main.java
public static Key getDESKey(byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException { DESKeySpec des = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_DES); return keyFactory.generateSecret(des); }
From source file:com.example.license.DESUtil.java
/** * /*from w ww.j ava2 s .c o m*/ * ?key * * @param KeyStr * * @return * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws Exception */ private static SecretKey keyGenerator(String keyStr) throws Exception { byte input[] = HexString2Bytes(keyStr); DESKeySpec desKey = new DESKeySpec(input); // ?DESKeySpec?? SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey securekey = keyFactory.generateSecret(desKey); return securekey; }
From source file:Encrypt.java
private static String encrypt(String message) throws Exception { Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); return toHexString(cipher.doFinal(message.getBytes("UTF-8"))); }
From source file:fi.ilmoeuro.membertrack.util.Crypto.java
public static String hash(String candidate, String salt) { try {/*from www .j av a 2 s .c o m*/ SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec ks = new PBEKeySpec(candidate.toCharArray(), salt.getBytes(StandardCharsets.US_ASCII), 1024, 128); SecretKey sk = skf.generateSecret(ks); Key k = new SecretKeySpec(sk.getEncoded(), "AES"); return Hex.encodeHexString(k.getEncoded()); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { throw new RuntimeException("Error while hashing", ex); } }
From source file:de.uzk.hki.da.passwordEncryptor.passwordEncryptor.java
private static String encryptPasswordForContentBroker(String password) { byte key[] = "394z57f4".getBytes(); byte encryptedPassword[]; try {/*from www . j av a2 s. c o m*/ SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key)); Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding"); encrypt.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(new byte[] { 0x01, 0x02, 0x04, 0x10, 0x01, 0x02, 0x04, 0x10 })); encryptedPassword = encrypt.doFinal(password.getBytes()); } catch (GeneralSecurityException e) { throw new RuntimeException("Couldn't encrypt password " + password + e); } return new String(Base64.encodeBase64(encryptedPassword)); }
From source file:de.uzk.hki.da.utils.PasswordUtils.java
public static String decryptPassword(String password) { byte key[] = "394z57f4".getBytes(); byte decryptedPassword[]; try {/*from w ww . j a v a2 s. c om*/ SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key)); Cipher decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding"); decrypt.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(new byte[] { 0x01, 0x02, 0x04, 0x10, 0x01, 0x02, 0x04, 0x10 })); decryptedPassword = decrypt.doFinal(Base64.decodeBase64(password.getBytes())); } catch (GeneralSecurityException e) { throw new RuntimeException("Couldn't decrypt password " + password + e); } return new String(decryptedPassword); }
From source file:Main.java
private static int generateIterationCount(String passphrase, byte[] salt) { int TARGET_ITERATION_TIME = 50; //ms int MINIMUM_ITERATION_COUNT = 100; //default for low-end devices int BENCHMARK_ITERATION_COUNT = 10000; //baseline starting iteration count try {/*from w w w. j a va 2 s . co m*/ PBEKeySpec keyspec = new PBEKeySpec(passphrase.toCharArray(), salt, BENCHMARK_ITERATION_COUNT); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA1AND128BITAES-CBC-BC"); long startTime = System.currentTimeMillis(); skf.generateSecret(keyspec); long finishTime = System.currentTimeMillis(); int scaledIterationTarget = (int) (((double) BENCHMARK_ITERATION_COUNT / (double) (finishTime - startTime)) * TARGET_ITERATION_TIME); if (scaledIterationTarget < MINIMUM_ITERATION_COUNT) return MINIMUM_ITERATION_COUNT; else return scaledIterationTarget; } catch (NoSuchAlgorithmException e) { Log.w("MasterSecretUtil", e); return MINIMUM_ITERATION_COUNT; } catch (InvalidKeySpecException e) { Log.w("MasterSecretUtil", e); return MINIMUM_ITERATION_COUNT; } }
From source file:com.ro.ssc.app.client.licensing.TrialKeyValidator.java
public static String decodeKey(String encodedEncrypted) { String decoded = ""; try {/*w w w.jav a 2 s .c o m*/ byte[] saltDecrypt = SALT_DECRYPT.getBytes(StandardCharsets.UTF_8); SecretKeyFactory factoryKeyDecrypt = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY); SecretKey tmp2 = factoryKeyDecrypt.generateSecret( new PBEKeySpec(PASS_DECRYPT.toCharArray(), saltDecrypt, ITERATIONS_DECRYPT, KEY_LENGTH)); SecretKeySpec decryptKey = new SecretKeySpec(tmp2.getEncoded(), ALGORITHM); Cipher aesCipherDecrypt = Cipher.getInstance(CIPHER); aesCipherDecrypt.init(Cipher.DECRYPT_MODE, decryptKey); byte[] e64bytes = StringUtils.getBytesUtf8(encodedEncrypted); byte[] eBytes = Base64.decodeBase64(e64bytes); byte[] cipherDecode = aesCipherDecrypt.doFinal(eBytes); decoded = StringUtils.newStringUtf8(cipherDecode); } catch (Exception e) { LOGGER.error("Error while decoding the trial key", e); } return decoded; }