List of usage examples for javax.crypto SecretKeyFactory generateSecret
public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException
From source file:com.ubipass.middleware.ems.EMSUtil.java
/** * set all licence parameters/*from w w w. j av a 2 s .c o m*/ * * @throws Exception */ private static void setParameters() throws Exception { File file = new File(path + LICENCE_FILE); FileInputStream is; try { is = new FileInputStream(file); } catch (FileNotFoundException e) { throw new InvalidLicenseException(); } Properties properties = new Properties(); properties.load(is); userName = properties.getProperty("userName"); licenceKey = properties.getProperty("licenceKey"); if (userName == null || licenceKey == null) { throw new InvalidLicenseException(); } // DES???? SecureRandom sr = new SecureRandom(); byte rawKeyData[] = (userName + "midware").getBytes(); // ?DESKeySpec DESKeySpec dks = new DESKeySpec(rawKeyData); // ?DESKeySpec?? // SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); // Cipher?? Cipher cipher = Cipher.getInstance("DES"); // ?Cipher cipher.init(Cipher.DECRYPT_MODE, key, sr); // ?? licenceKey = new String(cipher.doFinal(Base64.decodeBase64(licenceKey.getBytes()))); String tmpStr[] = licenceKey.split("-"); if (tmpStr.length != 2) throw new InvalidLicenseException(); licenceUser = tmpStr[0]; licenceExpDate = tmpStr[1]; }
From source file:org.apache.nifi.security.util.crypto.CipherUtility.java
/** * Initializes a {@link Cipher} object with the given PBE parameters. * * @param algorithm the algorithm/* w w w . j a v a 2s.co m*/ * @param provider the JCA provider * @param password the password * @param salt the salt * @param iterationCount the KDF iteration count * @param encryptMode true to encrypt; false to decrypt * @return the initialized Cipher * @throws IllegalArgumentException if any parameter is invalid */ public static Cipher initPBECipher(String algorithm, String provider, String password, byte[] salt, int iterationCount, boolean encryptMode) throws IllegalArgumentException { try { // Initialize secret key from password final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); final SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm, provider); SecretKey tempKey = factory.generateSecret(pbeKeySpec); final PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterationCount); Cipher cipher = Cipher.getInstance(algorithm, provider); cipher.init(encryptMode ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, tempKey, parameterSpec); return cipher; } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) { throw new IllegalArgumentException("One or more parameters to initialize the PBE cipher were invalid", e); } }
From source file:net.sf.jftp.config.Crypto.java
public static String Encrypt(String str) { // create cryptography object SecretKeyFactory factory; try {/*from ww w. j av a2 s .co m*/ factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); } catch (NoSuchAlgorithmException e) { // We could try another algorithm, but it is highly unlikely that this would be the case return ""; } // init key SecretKey key; try { key = factory.generateSecret(new PBEKeySpec(PASSWORD)); } catch (InvalidKeySpecException e) { // The password is hard coded - this exception can't be the case return ""; } // init cipher Cipher pbeCipher; try { pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); } catch (NoSuchPaddingException e) { // We could try another algorithm, but it is highly unlikely that this would be the case return ""; } catch (NoSuchAlgorithmException e) { // We could try another algorithm, but it is highly unlikely that this would be the case return ""; } try { pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); } catch (InvalidKeyException e) { return ""; } catch (InvalidAlgorithmParameterException e) { return ""; } // encode & return encoded string try { return base64Encode(pbeCipher.doFinal(str.getBytes())); } catch (IllegalBlockSizeException e) { return ""; } catch (BadPaddingException e) { return ""; } }
From source file:LicenseGenerator.java
/** * // w w w .j a v a2s . c o m * * * @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:LicenseGenerator.java
/** * //from ww w . j av a 2s. c o 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:net.sf.jftp.config.Crypto.java
public static String Decrypt(String str) { // create cryptography object SecretKeyFactory keyFactory; try {/*from w w w . java 2 s .c o m*/ keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); } catch (NoSuchAlgorithmException e) { // We could try another algorithm, but it is highly unlikely that this would be the case return ""; } // init key SecretKey key; try { key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); } catch (InvalidKeySpecException e) { return ""; } // init cipher Cipher pbeCipher; try { pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); } catch (NoSuchAlgorithmException e) { return ""; } catch (NoSuchPaddingException e) { return ""; } try { pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); } catch (InvalidKeyException e) { return ""; } catch (InvalidAlgorithmParameterException e) { return ""; } // decode & return decoded string String dec; try { dec = new String(pbeCipher.doFinal(base64Decode(str))); } catch (IllegalBlockSizeException e) { return ""; } catch (BadPaddingException e) { return ""; } catch (IOException e) { return ""; } return dec; }
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 {/* www . jav a 2s .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.jasig.cas.extension.clearpass.EncryptedMapDecorator.java
private static Key getSecretKey(final String secretKeyAlgorithm, final String secretKey, final String salt) throws Exception { SecretKeyFactory factory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_ALGORITHM); KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), char2byte(salt), 65536, 128); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), secretKeyAlgorithm); return secret; }
From source file:com.glaf.core.security.SecurityUtils.java
/** * ??/* w w w. j a v a 2 s . c o m*/ * * @param ctx * * @param envelope * ? * @param privateKey * ? * @return key */ public static Key openDigitalEnvelope(SecurityContext ctx, String envelope, Key privateKey) { try { Cipher cipher = Cipher.getInstance(ctx.getAsymmetryAlgorithm(), ctx.getJceProvider()); cipher.init(Cipher.DECRYPT_MODE, privateKey); envelope = StringTools.replaceIgnoreCase(envelope, " ", ""); byte[] key = cipher.doFinal(Base64.decodeBase64(envelope)); SecretKeyFactory skf = SecretKeyFactory.getInstance(ctx.getSymmetryKeyAlgorithm(), ctx.getJceProvider()); DESKeySpec keySpec = new DESKeySpec(key); Key symmetryKey = skf.generateSecret(keySpec); return symmetryKey; } catch (Exception ex) { throw new SecurityException(ex); } }
From source file:com.jrummyapps.busybox.signing.ZipSigner.java
/** * Decrypt an encrypted PKCS 8 format private key. * * Based on ghstark's post on Aug 6, 2006 at * http://forums.sun.com/thread.jspa?threadID=758133&messageID=4330949 * * @param encryptedPrivateKey//from w w w .ja v a2 s .c o m * The raw data of the private key */ private static KeySpec decryptPrivateKey(final byte[] encryptedPrivateKey) throws GeneralSecurityException { EncryptedPrivateKeyInfo epkInfo; try { epkInfo = new EncryptedPrivateKeyInfo(encryptedPrivateKey); } catch (final IOException ex) { // Probably not an encrypted key. return null; } final String pass = "android"; final char[] password = pass.toCharArray(); final SecretKeyFactory skFactory = SecretKeyFactory.getInstance(epkInfo.getAlgName()); final Key key = skFactory.generateSecret(new PBEKeySpec(password)); final Cipher cipher = Cipher.getInstance(epkInfo.getAlgName()); cipher.init(Cipher.DECRYPT_MODE, key, epkInfo.getAlgParameters()); try { return epkInfo.getKeySpec(cipher); } catch (final InvalidKeySpecException ex) { Log.e(TAG, "Password for keyFile may be bad.", ex); return null; } }