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

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

Description

to Plaintext

License

Apache License

Declaration

public static String toPlaintext(String encryptedText, String password) 

Method Source Code

//package com.java2s;
/**//from w  ww .j  a v a 2  s .  c o m
 * 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 OBFUSCATE = "OBF";
    public static final String ENCODE = "ENC";

    public static String toPlaintext(String encryptedText, String password) {
        if (encryptedText == null) {
            return null;
        }

        if (encryptedText.startsWith(OBFUSCATE)) {
            return deobfuscate(encryptedText);
        } else if (encryptedText.startsWith(ENCODE) && password != null) {
            return decrypt(encryptedText, password);
        }

        return encryptedText;
    }

    public static String deobfuscate(String obfuscatedText) {
        String s = obfuscatedText;
        if (s.startsWith(OBFUSCATE)) {
            s = s.substring(OBFUSCATE.length());
        }

        byte[] b = new byte[s.length() / 2];
        int l = 0;
        for (int i = 0; i < s.length(); i += 4) {
            String x = s.substring(i, i + 4);
            int i0 = Integer.parseInt(x, 36);
            int i1 = i0 / 256;
            int i2 = i0 % 256;
            b[l++] = (byte) ((i1 + i2 - 254) / 2);
        }

        return new String(b, 0, l);
    }

    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. serializeToString(Object object)
  2. serializeURLSafe(String string)
  3. stringToCalendar(String value)
  4. stringToData(String data)
  5. toBinaryKeyId(String keyId)
  6. toRoboCommand(int command)
  7. toString(byte[] edid)
  8. toString(final Serializable o)
  9. toString(Serializable o)