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.fengduo.bee.commons.security.EncryptBuilder.java

@SuppressWarnings("restriction")
private byte[] doCrypt(String source, String secretKey, ICrypt iCrypt)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] cryptedData = iCrypt.getCryptedData(source);
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    SecureRandom sr = new SecureRandom();
    byte[] rawKeyData = (new String(secretKey)).getBytes();

    DESKeySpec dks = new DESKeySpec(rawKeyData);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey key = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES");
    iCrypt.initCipher(key, sr, cipher);//from w  ww. j ava2s .  c  o  m

    return cipher.doFinal(cryptedData);
}

From source file:org.xdi.util.security.StringEncrypter.java

/**
 * Constructor specifying scheme and key
 * /*from   ww w .j  a v  a2  s.  c om*/
 * @param encryptionScheme
 *            Encryption scheme to use
 * @param encryptionKey
 *            Encryption key to use
 * @throws EncryptionException
 */
public StringEncrypter(final String encryptionScheme, final String encryptionKey) throws EncryptionException {
    if (encryptionKey == null) {
        throw new IllegalArgumentException("encryption key was null");
    }
    if (encryptionKey.trim().length() < 24) {
        throw new IllegalArgumentException("encryption key was less than 24 characters");
    }

    try {
        final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);

        if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
            keySpec = new DESedeKeySpec(keyAsBytes);
        } else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
            keySpec = new DESKeySpec(keyAsBytes);
        } else {
            throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
        }

        keyFactory = SecretKeyFactory.getInstance(encryptionScheme);
        cipher = Cipher.getInstance(encryptionScheme);
    } catch (final InvalidKeyException e) {
        throw new EncryptionException(e);
    } catch (final UnsupportedEncodingException e) {
        throw new EncryptionException(e);
    } catch (final NoSuchAlgorithmException e) {
        throw new EncryptionException(e);
    } catch (final NoSuchPaddingException e) {
        throw new EncryptionException(e);
    }
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

/**
 * DES bytKey8?/* ww  w . java 2  s  .  c o  m*/
 * 
 * @param bytP
 * @param bytKey
 * @return
 * @throws Exception
 */
public static byte[] encryptByDES(byte[] bytP, byte[] bytKey) throws Exception {
    DESKeySpec dks = new DESKeySpec(bytKey);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey sk = skf.generateSecret(dks);
    Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM);
    cip.init(Cipher.ENCRYPT_MODE, sk);
    return cip.doFinal(bytP);
}

From source file:org.jamwiki.utils.Encryption.java

/**
 * Create the encryption key value.//from  w  ww .  j a va 2 s  .  c om
 * 
 * @return An encryption key value implementing the DES encryption algorithm.
 */
private static SecretKey createKey() throws GeneralSecurityException, UnsupportedEncodingException {
    byte[] bytes = ENCRYPTION_KEY.getBytes("UTF8");
    DESKeySpec spec = new DESKeySpec(bytes);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
    return keyFactory.generateSecret(spec);
}

From source file:jef.tools.security.EncrypterUtil.java

/**
 * ??DESKey//  w w  w .j  a  va2  s.  c o  m
 * 
 * @param password
 * @return
 */
public static final SecretKey toDESKey(String password) {
    byte[] bb = password.getBytes();
    Assert.isTrue(bb.length > 7, "the secretKey for DES must be 8 bytes at least.");
    try {
        KeySpec keySpec = new DESKeySpec(bb);
        SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
        return key;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

/**
 * DES bytKey8?/*from  w  w w .  ja v  a2s.c  o  m*/
 * 
 * @param bytE
 * @param bytKey
 * @return
 * @throws Exception
 */
public static byte[] decryptByDES(byte[] bytE, byte[] bytKey) throws Exception {
    DESKeySpec desKS = new DESKeySpec(bytKey);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey sk = skf.generateSecret(desKS);
    Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM);
    cip.init(Cipher.DECRYPT_MODE, sk);
    return cip.doFinal(bytE);
}

From source file:eap.util.EDcodeUtil.java

private static byte[] des(byte[] data, byte[] key, int opMode) {
    try {//from  www.  j a v  a  2 s.  c  o m
        DESKeySpec desKey = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES", provider);
        SecretKey secureKey = keyFactory.generateSecret(desKey);

        Cipher cipher = Cipher.getInstance("DES", provider);
        //         SecureRandom secureRandom = new SecureRandom();
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); // provider
        cipher.init(opMode, secureKey, secureRandom);

        return cipher.doFinal(data);
    } catch (Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}

From source file:org.kuali.rice.core.impl.encryption.DemonstrationGradeEncryptionServiceImpl.java

private SecretKey unwrapEncodedKey(String key) throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    SecretKey desKey = keygen.generateKey();

    // Create the cipher
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init((Cipher.UNWRAP_MODE), desKey);

    byte[] bytes = Base64.decodeBase64(key.getBytes());

    SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES");

    DESKeySpec keyspec = new DESKeySpec(bytes);
    SecretKey k = desFactory.generateSecret(keyspec);

    return k;/*w  w w. j  a v a 2s  .  c  om*/

}

From source file:com.mastercard.mcbp.utils.crypto.CryptoServiceImpl.java

/**
 * {@inheritDoc}/*w  w  w  .j av  a2 s  .  c om*/
 */
@Override
public final byte[] desCbc(byte[] data, byte[] bKey, Mode mode) throws McbpCryptoException {
    try {
        SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(bKey));
        final Cipher cipher = Cipher.getInstance("DES/CBC/noPadding");

        final IvParameterSpec ips = new IvParameterSpec(new byte[8]);
        if (mode == Mode.ENCRYPT) {
            cipher.init(Cipher.ENCRYPT_MODE, key, ips);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, key, ips);
        }
        return cipher.doFinal(data);
    } catch (InvalidKeySpecException | NoSuchAlgorithmException | InvalidKeyException
            | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException
            | NoSuchPaddingException e) {
        throw new McbpCryptoException(e.toString());
    }
}

From source file:com.yaxin.utils.StringUtils.java

/**
 * /*from  ww w  . j  a  v  a2s.  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);
}