List of usage examples for javax.crypto.spec PBEKeySpec PBEKeySpec
public PBEKeySpec(char[] password)
From source file:cherry.goods.crypto.RSACryptoTest.java
private RSACrypto create2(char[] password) throws Exception { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); keygen.initialize(2048);//from ww w . jav a 2 s . c om KeyPair key = keygen.generateKeyPair(); String pbeAlgName = "PBEWithMD5AndDES"; PBEKeySpec pbeKeySpec = new PBEKeySpec(password); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(RandomUtils.nextBytes(8), 20); SecretKey pbeKey = SecretKeyFactory.getInstance(pbeAlgName).generateSecret(pbeKeySpec); AlgorithmParameters pbeParam = AlgorithmParameters.getInstance(pbeAlgName); pbeParam.init(pbeParamSpec); Cipher cipher = Cipher.getInstance(pbeAlgName); cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParam); EncryptedPrivateKeyInfo encryptedKeyInfo = new EncryptedPrivateKeyInfo(pbeParam, cipher.doFinal(key.getPrivate().getEncoded())); RSACrypto impl = new RSACrypto(); impl.setAlgorithm("RSA/ECB/PKCS1Padding"); impl.setPublicKeyBytes(key.getPublic().getEncoded()); impl.setPrivateKeyBytes(encryptedKeyInfo.getEncoded(), password); return impl; }
From source file:be.tutul.naheulcraft.launcher.auth.Auth.java
private Cipher getCipher(int paramInt, String key) throws Exception { Random random = new Random(43287234L); byte[] salt = new byte[8]; random.nextBytes(salt);/*from w w w. j a va 2 s. c om*/ PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 5); SecretKey pbeKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES") .generateSecret(new PBEKeySpec(key.toCharArray())); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(paramInt, pbeKey, pbeParamSpec); return cipher; }
From source file:org.eclipse.che.ide.ext.datasource.server.EncryptTextService.java
public String decryptText(String textToDecrypt) throws Exception { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(getMasterPassword())); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return new String(pbeCipher.doFinal(Base64.decodeBase64(textToDecrypt)), "UTF-8"); }
From source file:org.datacleaner.util.convert.EncodedStringConverter.java
@Override public String toString(String password) { if (password == null) { return null; }/* www . j av a 2 s .co m*/ try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORHITM); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(_secret)); Cipher pbeCipher = Cipher.getInstance(ALGORHITM); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(_salt, 20)); byte[] bytes = pbeCipher.doFinal(password.getBytes()); bytes = Base64.encodeBase64(bytes, false); return new String(bytes, "UTF-8"); } catch (Exception e) { throw new IllegalStateException("Unable to encode password", e); } }
From source file:net.sf.jftp.config.Crypto.java
public static String Decrypt(String str) { // create cryptography object SecretKeyFactory keyFactory;//from ww w . ja va 2s .c o m try { 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.report.query.ReportQueryBL.java
private static String dcl(String encryptedText, char[] password) { byte[] clearText = { ' ' }; int count = 20; PBEKeySpec pbeKeySpec;//from w w w .j a v a 2 s .c om PBEParameterSpec pbeParamSpec; SecretKeyFactory keyFac; // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt, count); pbeKeySpec = new PBEKeySpec(password); try { 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.DECRYPT_MODE, pbeKey, pbeParamSpec); byte[] ciphertext = Base64.decodeBase64(encryptedText); // Decrypt the cleartext clearText = pbeCipher.doFinal(ciphertext); } catch (Exception e) { } return new String(clearText); }
From source file:com.aurel.track.admin.customize.category.filter.execute.ReportQueryBL.java
private static String dcl(String encryptedText, char[] password) { byte[] clearText = { ' ' }; int count = 20; PBEKeySpec pbeKeySpec;/*ww w.j a v a 2 s . c om*/ PBEParameterSpec pbeParamSpec; SecretKeyFactory keyFac; // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt, count); pbeKeySpec = new PBEKeySpec(password); try { 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.DECRYPT_MODE, pbeKey, pbeParamSpec); byte[] ciphertext = Base64.decodeBase64(encryptedText); //Decrypt the cleartext clearText = pbeCipher.doFinal(ciphertext); } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e), e); } return new String(clearText); }
From source file:org.apache.nifi.processors.standard.util.crypto.PasswordBasedEncryptor.java
public PasswordBasedEncryptor(final EncryptionMethod encryptionMethod, final char[] password, KeyDerivationFunction kdf) {/* w ww . ja v a 2 s .c om*/ super(); try { if (encryptionMethod == null) { throw new IllegalArgumentException( "Cannot initialize password-based encryptor with null encryption method"); } this.encryptionMethod = encryptionMethod; if (kdf == null || kdf.equals(KeyDerivationFunction.NONE)) { throw new IllegalArgumentException("Cannot initialize password-based encryptor with null KDF"); } this.kdf = kdf; if (password == null || password.length == 0) { throw new IllegalArgumentException( "Cannot initialize password-based encryptor with empty password"); } this.password = new PBEKeySpec(password); } catch (Exception e) { throw new ProcessException(e); } }
From source file:com.adeptj.modules.security.jwt.internal.JwtKeys.java
static PrivateKey createSigningKey(JwtConfig jwtConfig, SignatureAlgorithm signatureAlgo) { LOGGER.info("Creating RSA signing key!!"); String keyData = jwtConfig.privateKey(); Assert.isTrue(StringUtils.startsWithAny(keyData, PRIVATE_ENCRYPTED_KEY_HEADER, PRIVATE_KEY_HEADER), INVALID_PRIVATE_KEY_MSG);//from w w w. ja v a 2s . co m try { KeyFactory keyFactory = KeyFactory.getInstance(signatureAlgo.getFamilyName()); if (StringUtils.startsWith(keyData, PRIVATE_ENCRYPTED_KEY_HEADER)) { LOGGER.info("Creating PKCS8EncodedKeySpec from private [encrypted] key !!"); Assert.hasText(jwtConfig.privateKeyPassword(), KEYPASS_NULL_MSG); byte[] privateKeyData = decodePrivateKeyData(keyData, true); EncryptedPrivateKeyInfo privateKeyInfo = new EncryptedPrivateKeyInfo(privateKeyData); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(privateKeyInfo.getAlgName()); PBEKeySpec keySpec = new PBEKeySpec(jwtConfig.privateKeyPassword().toCharArray()); SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(privateKeyInfo.getAlgName()); cipher.init(DECRYPT_MODE, secretKey, privateKeyInfo.getAlgParameters()); return keyFactory.generatePrivate(privateKeyInfo.getKeySpec(cipher)); } LOGGER.info("Creating PKCS8EncodedKeySpec from private key !!"); return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(decodePrivateKeyData(keyData, false))); } catch (GeneralSecurityException | IOException ex) { LOGGER.error(ex.getMessage(), ex); throw new KeyInitializationException(ex.getMessage(), ex); } }
From source file:com.almende.util.EncryptionUtil.java
/** * Decrypt an encrypted string./* w ww . ja v a2 s. c om*/ * * @param encryptedText * the encrypted text * @return text * @throws InvalidKeyException * the invalid key exception * @throws InvalidAlgorithmParameterException * the invalid algorithm parameter exception * @throws NoSuchAlgorithmException * the no such algorithm exception * @throws InvalidKeySpecException * the invalid key spec exception * @throws NoSuchPaddingException * the no such padding exception * @throws IllegalBlockSizeException * the illegal block size exception * @throws BadPaddingException * the bad padding exception * @throws UnsupportedEncodingException * the unsupported encoding exception */ public static String decrypt(final String encryptedText) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { final PBEParameterSpec pbeParamSpec = new PBEParameterSpec(S, C); final PBEKeySpec pbeKeySpec = new PBEKeySpec(P); final SecretKeyFactory keyFac = SecretKeyFactory.getInstance(ENC); final SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); final Cipher pbeCipher = Cipher.getInstance(ENC); pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); final byte[] text = pbeCipher.doFinal(Base64.decodeBase64(encryptedText)); return new String(text, "UTF-8").intern(); }