Java XML Data Type Converter decrypt(String encryptedText, String password)

Here you can find the source of decrypt(String encryptedText, String password)

Description

decrypt

License

Apache License

Declaration

public static String decrypt(String encryptedText, String password) 

Method Source Code

//package com.java2s;
/**//from w  w w .  j av  a 2 s . c om
 * Copyright 2013 Ralph Schaer <ralphschaer@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.xml.bind.DatatypeConverter;

public class Main {
    public static final String ENCODE = "ENC";

    public static String decrypt(String encryptedText, String password) {
        try {
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
            SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password.toCharArray()));
            Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");

            String enc = encryptedText.substring(ENCODE.length() + 12);
            byte[] salt = DatatypeConverter
                    .parseBase64Binary(encryptedText.substring(ENCODE.length(), ENCODE.length() + 12));

            pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, 20));
            return new String(pbeCipher.doFinal(DatatypeConverter.parseBase64Binary(enc)));
        } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException
                | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
            throw new RuntimeException(e);
        }

    }
}

Related

  1. decode(String source)
  2. decode(String string, boolean decode)
  3. decodeBasicAuth(String auth)
  4. decodeJavaOpts(String encodedJavaOpts)
  5. decodeToDoubleArray(String encoded)
  6. deCrypt(String property)
  7. deserializeFromString(String x)
  8. deserializeLogic(String logicString)
  9. encodeBasicHeader(final String username, final String password)