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.dc.tes.License.java

public static byte[] decrypt(byte[] src, byte[] key) throws Exception {

    // DES????/*from   w  ww .  ja v  a 2s .  c o m*/

    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:org.kuali.rice.krad.devtools.maintainablexml.EncryptionService.java

private SecretKey unwrapEncodedKeyOld(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());

    // If decoding was done with commons-codec 1.3 and the key not ended with '='
    bytes[6] = 1;/*from  w  ww.ja v a 2 s  . co m*/
    bytes[7] = 1;

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

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

    return k;

}

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

/**
 * /*from   ww  w .  j av a  2 s. 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:org.xdi.util.security.StringEncrypter.java

/**
 * Decrypt a string encrypted with this encrypter
 * //ww w  .j ava2 s  .  c  o m
 * @param encryptedString
 *            Encrypted string
 * @return Decrypted string
 * @throws EncryptionException
 */
public String decrypt(final String encryptedString, String encryptionKey) throws EncryptionException {
    lock.lock();
    try {
        final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
        String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
        KeySpec keySpec;
        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);
        }
        return decrypt(encryptedString, keySpec);
    } catch (final Exception e) {
        throw new EncryptionException(e);
    } finally {
        lock.unlock();
    }
}

From source file:org.kuali.rice.kew.documentoperation.web.DocumentContentOperationAction.java

private SecretKey getSecretKey(String encryptionKey) 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(encryptionKey.getBytes());

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

    DESKeySpec keyspec = new DESKeySpec(bytes);
    desKey = desFactory.generateSecret(keyspec);
    // Create the cipher
    cipher.init((Cipher.WRAP_MODE), desKey);
    return desKey;
}

From source file:com.ubipass.middleware.ems.EMSUtil.java

/**
 * set all licence parameters//from ww  w . ja  v a  2  s . c o  m
 * 
 * @throws Exception
 */
private static void setParameters() throws Exception {

    File file = new File(path + LICENCE_FILE);
    FileInputStream is;

    try {
        is = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        throw new InvalidLicenseException();
    }

    Properties properties = new Properties();
    properties.load(is);

    userName = properties.getProperty("userName");
    licenceKey = properties.getProperty("licenceKey");

    if (userName == null || licenceKey == null) {
        throw new InvalidLicenseException();
    }

    // DES????
    SecureRandom sr = new SecureRandom();

    byte rawKeyData[] = (userName + "midware").getBytes();

    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(rawKeyData);

    // ?DESKeySpec??
    // SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey key = keyFactory.generateSecret(dks);

    // Cipher??
    Cipher cipher = Cipher.getInstance("DES");

    // ?Cipher
    cipher.init(Cipher.DECRYPT_MODE, key, sr);

    // ??
    licenceKey = new String(cipher.doFinal(Base64.decodeBase64(licenceKey.getBytes())));

    String tmpStr[] = licenceKey.split("-");

    if (tmpStr.length != 2)
        throw new InvalidLicenseException();

    licenceUser = tmpStr[0];
    licenceExpDate = tmpStr[1];

}

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

/**
 * ??/*w w  w . j  a va2s  .c  o m*/
 * 
 * @param ctx
 *            
 * @param envelope
 *            ?
 * @param privateKey
 *            ?
 * @return key 
 */
public static Key openDigitalEnvelope(SecurityContext ctx, String envelope, Key privateKey) {
    try {
        Cipher cipher = Cipher.getInstance(ctx.getAsymmetryAlgorithm(), ctx.getJceProvider());
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        envelope = StringTools.replaceIgnoreCase(envelope, " ", "");
        byte[] key = cipher.doFinal(Base64.decodeBase64(envelope));

        SecretKeyFactory skf = SecretKeyFactory.getInstance(ctx.getSymmetryKeyAlgorithm(),
                ctx.getJceProvider());
        DESKeySpec keySpec = new DESKeySpec(key);
        Key symmetryKey = skf.generateSecret(keySpec);

        return symmetryKey;
    } catch (Exception ex) {
        throw new SecurityException(ex);
    }
}

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

/**
 * Encrypt a string//from   w w w.  j a v  a2s . c  om
 * 
 * @param unencryptedString
 *            String to encrypt
 * @return Encrypted string (using scheme and key specified at construction)
 * @throws EncryptionException
 */
public String encrypt(final String unencryptedString, String encryptionKey) throws EncryptionException {
    lock.lock();
    try {
        final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
        String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
        KeySpec keySpec;
        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);
        }

        return encrypt(unencryptedString, keySpec);
    } catch (final Exception e) {
        throw new EncryptionException(e);
    } finally {
        lock.unlock();
    }
}

From source file:com.grazerss.EntryManager.java

private static SecretKey getSecretKey() throws InvalidKeyException, UnsupportedEncodingException,
        NoSuchAlgorithmException, InvalidKeySpecException {
    DESKeySpec keySpec = new DESKeySpec("EntryManager.class".getBytes("UTF8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(keySpec);
    return secretKey;
}

From source file:LicenseGenerator.java

/**
 * //from  w  w w.ja v a  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);

}