passworddecoder.DesEncrypter.java Source code

Java tutorial

Introduction

Here is the source code for passworddecoder.DesEncrypter.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package passworddecoder;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.apache.commons.codec.binary.Base64;

/**
 *
 * @author planktonette
 */
public class DesEncrypter {
    Cipher dcipher;
    byte[] salt = { -87, -101, -56, 50, 86, 53, -29, 3 };

    int iterationCount = 19;

    DesEncrypter(String passPhrase) {
        KeySpec keySpec;
        try {
            keySpec = new PBEKeySpec(passPhrase.toCharArray(), this.salt, this.iterationCount);
            SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
            this.dcipher = Cipher.getInstance(key.getAlgorithm());

            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(this.salt, this.iterationCount);

            this.dcipher.init(2, key, paramSpec);
        } catch (InvalidAlgorithmParameterException e) {
        } catch (InvalidKeySpecException e) {
        } catch (NoSuchPaddingException e) {
        } catch (NoSuchAlgorithmException e) {
        } catch (InvalidKeyException e) {
        }
    }

    public String decrypt(String str) {
        try {
            byte[] dec = Base64.decodeBase64(str);

            byte[] utf8 = this.dcipher.doFinal(dec);

            dec = null;
            return new String(utf8, "UTF8");
        } catch (Exception e) {
        }
        return null;
    }
}