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:org.yes.cart.web.support.util.cookie.impl.CookieTuplizerImpl.java

/**
 * Default Constructor.//w w w . java 2s  .  co  m
 *
 * @param keyRingPassword      key ring password to use.
 * @param chunkSize            Base64 chunk size.
 * @param secretKeyFactoryName Secret Key Factory Name.
 * @param cipherName           Cipher name.
 */
public CookieTuplizerImpl(final String keyRingPassword, final int chunkSize, final String secretKeyFactoryName,
        final String cipherName) {

    this.chunkSize = chunkSize;

    try {
        final DESKeySpec desKeySpec = new DESKeySpec(keyRingPassword.getBytes());

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretKeyFactoryName);
        secretKey = keyFactory.generateSecret(desKeySpec);

        // Create Cipher
        desCipher = Cipher.getInstance(cipherName);
        desCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        // create uncipher
        desUnCipher = Cipher.getInstance(cipherName);

        desUnCipher.init(Cipher.DECRYPT_MODE, secretKey);
    } catch (Exception ike) {
        ShopCodeContext.getLog(this).error(ike.getMessage(), ike);
        throw new RuntimeException("Unable to load Cipher for CookieTuplizer", ike);
    }

}

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

/**
 * Description ?// ww w . ja va 2  s. c o  m
 *
 * @param data
 * @param key  byte
 * @return
 * @throws Exception
 */
private static byte[] decrypt(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.DECRYPT_MODE, securekey, sr);

    return cipher.doFinal(data);
}

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

/**
 * ?/*from w w w  .  ja v a 2 s . co m*/
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String decryptBase64(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.DECRYPT_MODE, securekey);
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data.getBytes())));
}

From source file:com.titilink.common.app.EncryptDecryptUtil.java

public void testDES() throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException,
        NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {
    ///*from ww w  .  j a  v a2 s .com*/
    DESKeySpec desKeySpec = new DESKeySpec("SECURITY".getBytes(Charset.forName("UTF-8")));
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);

    //??
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom());
    byte[] cipherData = cipher
            .doFinal("this is a security text from server".getBytes(Charset.forName("UTF-8")));

    //?
    Cipher cipher1 = Cipher.getInstance("DES");
    cipher1.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom());
    byte[] plainData = cipher1.doFinal(cipherData);
    System.out.println(new String(plainData, Charset.forName("UTF-8")));
}

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

/**
 * deskey//from  w  w  w  .j  av  a  2s . co m
 * 
 * @param content
 * @param key
 * @return
 * @throws Exception
 */
public static byte[] encryptByDES(String content, String key) {
    try {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        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(content.getBytes());
    } catch (Exception e) {
        LOGGER.error("{}", e);
        return null;
    }

}

From source file:com.ubipass.middleware.web.action.LicenceMgtAction.java

private String getDate(String userName, String licenceKey) throws Exception {

    // DES????//from w  w w  . j  a va  2 s .  com
    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)
        return tmpStr[1];

    throw new InvalidLicenseException();

}

From source file:com.shenit.commons.codec.DesUtils.java

/**
 * @param rawData//from   w w  w . j  a va 2 s . co m
 * @param rawKey
 * @param mode
 */
private static byte[] crypt(byte[] rawData, byte[] rawKey, int mode) {
    try {
        return crypt(rawData, new DESKeySpec(rawKey), mode);
    } catch (InvalidKeyException e) {
        if (LOG.isWarnEnabled())
            LOG.warn("[crypt] could not generate key spec for des", e);
    }
    return null;
}

From source file:org.suren.autotest.web.framework.util.Encryptor.java

/** 
 * ?/*from w  w w . j  a  v a 2 s.c o  m*/
 *  
 * @param desKey 
 * @param algorithm ??? {@link #DES}? {@link #DESEDE}?{@link #AES}?
 *            {@link #BLOWFISH}?{@link #RC2}? {@link #RC4}
 * @return 
 */
public static SecretKey getSecretKey(byte[] desKey, String algorithm) {
    if (ALG_DES.equalsIgnoreCase(algorithm)) {
        try {
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
            DESKeySpec dks = new DESKeySpec(desKey);
            return keyFactory.generateSecret(dks);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getCause());
        } catch (Exception e) {
            throw new IllegalArgumentException("?", e);
        }
    }

    return new SecretKeySpec(desKey, algorithm);
}

From source file:com.mmj.app.common.security.EncryptBuilder.java

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 w  w  . j av  a 2s  .c  om*/

    return cipher.doFinal(cryptedData);
}

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

/**
 * des//from   www .ja va2  s.co  m
 * 
 * @param content
 * @param key
 * @return
 * @throws Exception
 */
public static String decryptByDES(byte[] content, String key) {
    try {
        DESKeySpec desKS = new DESKeySpec(key.getBytes());
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        SecretKey sk = skf.generateSecret(desKS);
        Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM);
        cip.init(Cipher.DECRYPT_MODE, sk);
        byte[] result = cip.doFinal(content);
        return new String(result);
    } catch (Exception e) {
        LOGGER.error("{}", e);
        return null;
    }
}