Java XML Data Type Converter encrypt(String plainText, String password)

Here you can find the source of encrypt(String plainText, String password)

Description

encrypt

License

Apache License

Declaration

public static String encrypt(String plainText, String password) throws Exception 

Method Source Code

//package com.java2s;
/**//from  ww  w  .  j a v  a2 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.SecureRandom;

import javax.crypto.Cipher;

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 encrypt(String plainText, String password) throws Exception {

        byte[] salt = new byte[8];
        SecureRandom random = new SecureRandom();
        random.nextBytes(salt);

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password.toCharArray()));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, 20));
        return ENCODE + DatatypeConverter.printBase64Binary(salt)
                + DatatypeConverter.printBase64Binary(pbeCipher.doFinal(plainText.getBytes()));
    }
}

Related

  1. deserializeFromString(String x)
  2. deserializeLogic(String logicString)
  3. encodeBasicHeader(final String username, final String password)
  4. encodeDoubleArray(double[] noData)
  5. encodeJavaOpts(String javaOpts)
  6. encryptPassword(String unencrypted)
  7. fetchUrl(String url, String username, String password)
  8. formatFriendlyName(final byte[] addressBytes)
  9. fromString(String s)