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:com.wms.studio.security.utils.Digests.java

public static byte[] desEncrypt(String pwd, byte[] key) throws Exception {
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??//  ww w. ja  v  a2s .co m
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
    return cipher.doFinal(pwd.getBytes());
}

From source file:com.greenline.hrs.admin.util.encrypt.DESUtil.java

/**
 * Description ?/*  w  w w  .  j ava2 s .  com*/
 *
 * @param data
 * @param key  byte
 * @return
 * @throws Exception
 */
private static byte[] encrypt(byte[] data, byte[] key) throws GeneralSecurityException {
    // ????
    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(data);

}

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

/**
 * /*from  w w w  .ja va 2s . co  m*/
 * @param src   ??
 * @param key   8?
 * @return      ??
 * @throws Exception
 */
public static byte[] decrypt(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.DECRYPT_MODE, securekey, sr);
    // ??
    // ??
    return cipher.doFinal(src);
}

From source file:com.example.license.DESUtil.java

public static String encryptBase64(String data, String key) throws Exception {

    DESKeySpec desKey = new DESKeySpec(key.getBytes());
    // ?DESKeySpec??
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey securekey = keyFactory.generateSecret(desKey);

    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, securekey);

    byte[] results = cipher.doFinal(data.getBytes("UTF-8"));
    // Encrypt//from  w w  w.  j  a v  a2s. c o  m
    // byte[] unencryptedByteArray = data.getBytes("UTF8");
    // byte[] encryptedBytes = encryptCipher.doFinal(unencryptedByteArray);

    // Encode bytes to base64 to get a string
    byte[] encodedBytes = Base64.encodeBase64(results);
    return new String(encodedBytes);
}

From source file:club.jmint.crossing.specs.Security.java

public static String desDecrypt(String data, String key) throws CrossException {
    String ret = null;//from  w  ww  .j  a v a  2  s .  c  o m
    try {
        DESKeySpec desKey = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);

        Cipher cipher = Cipher.getInstance(CIPHER_DES_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, securekey);

        ret = new String(cipher.doFinal(Base64.decodeBase64(data)));
    } catch (Exception e) {
        CrossLog.printStackTrace(e);
        throw new CrossException(ErrorCode.COMMON_ERR_DECRYPTION.getCode(),
                ErrorCode.COMMON_ERR_DECRYPTION.getInfo());
    }
    return ret;
}

From source file:egovframework.com.ext.jfile.security.service.CipherServiceImpl.java

/**
 *   ?.//from   w ww  . j  a v  a2 s . com
 * @param keyAlgorithm  .
 * @param algorithm .
 * @param keyData  ??.
 * @return Key  .
 * @throws NoSuchAlgorithmException   ?  ?  .
 * @throws InvalidKeyException  ? key ?  
 * @throws InvalidKeySpecException  ? keySpec ?  
 */
private static Key generateKey(String keyAlgorithm, String algorithm, byte[] keyData)
        throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    if (keyAlgorithm == null || "".equals(keyAlgorithm))
        throw new NoSuchAlgorithmException("algorithm is nessary");
    String upper = keyAlgorithm.toUpperCase();
    if ("DES".equals(upper)) {
        KeySpec keySpec = new DESKeySpec(keyData);
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(keyAlgorithm);

        SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
        return secretKey;

    } else if (upper.indexOf("DESEDE") > -1 || upper.indexOf("TRIPLEDES") > -1) {
        KeySpec keySpec = new DESedeKeySpec(keyData);

        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(keyAlgorithm);
        SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
        return secretKey;
    } else {
        SecretKeySpec keySpec = new SecretKeySpec(keyData, keyAlgorithm);
        return keySpec;
    }
}

From source file:com.glaf.core.security.SecurityUtils.java

/**
 * DES/*from w w w .ja  v a2  s. c  om*/
 * 
 * @param data
 *            
 * @param key
 *            ???8?
 * @return ?
 */
public static String encode(String key, String data) {
    if (data == null) {
        return null;
    }
    try {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // key??8?
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
        AlgorithmParameterSpec paramSpec = iv;
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        byte[] bytes = cipher.doFinal(data.getBytes());
        return byte2hex(bytes);
    } catch (Exception ex) {
        throw new SecurityException(ex);
    }
}

From source file:it.scoppelletti.programmerpower.security.spi.DESKeyFactory.java

@Override
protected KeySpec getKeySpec(String alg, KeyRep.Type keyType, Properties props, String prefix) {
    String name, value;/*w ww  . jav  a 2 s  .c  o m*/
    byte[] key;
    KeySpec keySpec;

    name = Strings.concat(prefix, DESKeyFactory.PROP_KEY);
    value = props.getProperty(name);
    if (Strings.isNullOrEmpty(value)) {
        throw new ArgumentNullException(name);
    }

    try {
        key = Hex.decodeHex(value.toCharArray());
    } catch (DecoderException ex) {
        throw SecurityUtils.toSecurityException(ex);
    }

    try {
        keySpec = new DESKeySpec(key);
    } catch (InvalidKeyException ex) {
        throw SecurityUtils.toSecurityException(ex);
    }

    return keySpec;
}

From source file:com.aqnote.shared.cryptology.symmetric.DES.java

private static void generateCipher(String rawKey) {
    try {/*  w w w  . j  a va  2  s .c om*/
        DESKeySpec dks = new DESKeySpec(rawKey.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey deskey = keyFactory.generateSecret(dks);
        encodeCipher = Cipher.getInstance(ALGORITHM);
        encodeCipher.init(Cipher.ENCRYPT_MODE, deskey);
        decodeCipher = Cipher.getInstance(ALGORITHM);
        decodeCipher.init(Cipher.DECRYPT_MODE, deskey);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.wms.studio.security.utils.Digests.java

public static byte[] desDecrypt(byte[] pwd, byte[] key) throws Exception {
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??//from w w  w  .ja  v a2s .c  o  m
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.DECRYPT_MODE, securekey, random);
    return cipher.doFinal(pwd);
}