Example usage for javax.crypto.spec DESKeySpec DESKeySpec

List of usage examples for javax.crypto.spec DESKeySpec DESKeySpec

Introduction

In this page you can find the example usage for javax.crypto.spec DESKeySpec DESKeySpec.

Prototype

public DESKeySpec(byte[] key) throws InvalidKeyException 

Source Link

Document

Creates a DESKeySpec object using the first 8 bytes in key as the key material for the DES key.

Usage

From source file:de.fhdo.helper.DES.java

public static String decrypt(String Text) {
    try {//from  w  ww  .ja  v  a 2 s .c  om
        DESKeySpec keySpec = new DESKeySpec("schluessel_stdrepository15".getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);

        byte[] encrypedPwdBytes = Base64.decodeBase64(Text);
        Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe
        cipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes));

        return new String(plainTextPwdBytes);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:com.intera.roostrap.util.EncryptionUtil.java

private static void encryptOrDecrypt(String encryptionKey, int mode, InputStream is, OutputStream os)
        throws InvalidKeyException, IOException {
    DESKeySpec keySpec = new DESKeySpec(toBytes(encryptionKey));

    SecretKey key = null;/*from   www .ja v  a  2 s . co  m*/
    Cipher cipher = null;
    try {
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        key = secretKeyFactory.generateSecret(keySpec);
        cipher = Cipher.getInstance("DES");

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    if (mode == Cipher.ENCRYPT_MODE) {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        doCopy(cis, os);
    } else if (mode == Cipher.DECRYPT_MODE) {
        cipher.init(Cipher.DECRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        doCopy(is, cos);
    }
}

From source file:com.ikon.util.SecureStore.java

/**
 * DES encoder//from w w w.ja va 2 s  .c om
 */
public static byte[] desEncode(String key, byte[] src)
        throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException,
        InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey sKey = keyFactory.generateSecret(keySpec);

    Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
    cipher.init(Cipher.ENCRYPT_MODE, sKey);
    byte[] dst = cipher.doFinal(src);

    return dst;
}

From source file:org.underworldlabs.util.DesEncrypter.java

public static SecretKey getSecretKey(String key) throws Exception {
    byte[] keyAsBytes = key.getBytes("UTF8");
    KeySpec keySpec = new DESKeySpec(keyAsBytes);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    return keyFactory.generateSecret(keySpec);
}

From source file:com.soctec.soctec.utils.Encryptor.java

/**
 * Constructs the encryptor.// w  ww.  j  av  a  2 s.c  o  m
 * Creates the needed keys and cipher for encryption.
 *
 */
public Encryptor() {

    try {
        keySpec = new DESKeySpec(keyString.getBytes("UTF8"));
        keyFactory = SecretKeyFactory.getInstance("DES");
        key = keyFactory.generateSecret(keySpec);

        ecipher = Cipher.getInstance("DES");
        decipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        decipher.init(Cipher.DECRYPT_MODE, key);

    } catch (InvalidKeyException | UnsupportedEncodingException | NoSuchAlgorithmException
            | InvalidKeySpecException | NoSuchPaddingException e) {
        e.printStackTrace();
    }
}

From source file:com.creditcloud.common.security.impl.DESTextCipher.java

@Override
public void init(String salt) {
    try {//from w w  w  .  ja  v a2s  .  c  o m
        SecretKey sk = keyFactory.generateSecret(new DESKeySpec(salt.getBytes()));
        encryptCipher.init(Cipher.ENCRYPT_MODE, sk);
        decryptCipher.init(Cipher.DECRYPT_MODE, sk);
    } catch (InvalidKeyException | InvalidKeySpecException ex) {
        logger.error("Can't init Cipher.[salt={}]", salt, ex);
    }
}

From source file:net.sf.hajdbc.codec.crypto.CipherCodecTest.java

@Before
public void before() throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
    Key key = factory.generateSecret(new DESKeySpec(Base64.decodeBase64(KEY.getBytes())));

    this.codec = new CipherCodec(key);
}

From source file:org.duracloud.common.util.EncryptionUtil.java

public EncryptionUtil(String key) throws DuraCloudRuntimeException {
    if (key == null) {
        throw new IllegalArgumentException("'key' parameter must be non-null");
    }/*from   w  w  w. jav  a 2s .c o m*/

    int keySize = DEFAULT_KEY.length();
    if (key.length() > keySize) {
        key = key.substring(0, keySize);
    }

    key = StringUtils.leftPad(key, keySize);

    this.keyBytes = key.getBytes();
    try {
        // Create cipher
        this.cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

        // Create Key
        DESKeySpec deskey = new DESKeySpec(this.keyBytes);
        this.key = new SecretKeySpec(deskey.getKey(), "DES");
    } catch (Exception e) {
        throw new DuraCloudRuntimeException(e);
    }
}

From source file:Crypt.java

private Crypt() {
    DESKeySpec dk;/*from   w ww.j  av a  2s  .  com*/
    try {
        dk = new DESKeySpec(new Long(serialVersionUID).toString().getBytes());
        SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
        secretKey = kf.generateSecret(dk);
    } catch (Exception e) {
        log.error("", e);
    }
}

From source file:com.liusoft.dlog4j.upgrade.StringUtils.java

/**
 * //from w  ww  .j  a  va 2s.c o  m
 * @param src ??
 * @param key 8?
 * @return     ??
 * @throws Exception
 */
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
    //      DES????
    SecureRandom sr = new SecureRandom();
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??
    // SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
    // ??
    // ??
    return cipher.doFinal(src);
}