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:LicenseGenerator.java

/**
 * //  w  w w .  j  av  a2 s. c o 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.securityfilter.authenticator.persistent.DefaultPersistentLoginManager.java

/**
 * Set the Encryptin Key used to create a secret key, the secret key is passed
 * to the Cipher object to be used during encryption and decryption of cookie
 * values./*from w  w  w  .j av  a  2 s .com*/
 * <p>
 * <i>NOTE: This entry in the config file must NOT appear before any of the other
 * encryption config entries</i>
 *
 * @param encryptionkey          A String containing the encryption key as
 *                               defined in config file. This is a required
 *                               config entry if protection is set to ALL or ENCRYPTION.
 */
public void setEncryptionKey(String encryptionkey) {
    this.encryptionKey = encryptionkey;
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptionAlgorithm);
        byte[] desKeyData = encryptionkey.getBytes();
        DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
        secretKey = keyFactory.generateSecret(desKeySpec);
    } catch (Exception e) {
        System.out.println("Error: " + e);
        e.printStackTrace();
    }
}

From source file:org.bremersee.common.security.crypto.password.PasswordEncoderImpl.java

/**
 * <p>/*from   ww w.  j a  v a 2 s.co  m*/
 * Computes an odd DES key from 56 bits represented as a 7-bytes array.
 * </p>
 * <p>
 * Keeps elements from index {@code offset} to index
 * {@code offset + 7} of supplied array.
 * </p>
 *
 * @param keyData a byte array containing the 56 bits used to compute the DES
 *                key
 * @param offset  the offset of the first element of the 56-bits key data
 * @return the odd DES key generated
 * @throws InvalidKeyException      when key is invalid
 * @throws NoSuchAlgorithmException when algorithm is not available
 * @throws InvalidKeySpecException  when key spec is invalid
 */
private static Key computeDESKey(final byte[] keyData, final int offset)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {

    byte[] desKeyData = new byte[8];
    int[] k = new int[7];

    for (int i = 0; i < 7; i++)
        k[i] = unsignedByteToInt(keyData[offset + i]);

    desKeyData[0] = (byte) (k[0] >>> 1);
    desKeyData[1] = (byte) (((k[0] & 0x01) << 6) | (k[1] >>> 2));
    desKeyData[2] = (byte) (((k[1] & 0x03) << 5) | (k[2] >>> 3));
    desKeyData[3] = (byte) (((k[2] & 0x07) << 4) | (k[3] >>> 4));
    desKeyData[4] = (byte) (((k[3] & 0x0F) << 3) | (k[4] >>> 5));
    desKeyData[5] = (byte) (((k[4] & 0x1F) << 2) | (k[5] >>> 6));
    desKeyData[6] = (byte) (((k[5] & 0x3F) << 1) | (k[6] >>> 7));
    desKeyData[7] = (byte) (k[6] & 0x7F);

    for (int i = 0; i < 8; i++)
        desKeyData[i] = (byte) (unsignedByteToInt(desKeyData[i]) << 1);

    KeySpec desKeySpec = new DESKeySpec(desKeyData);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    return keyFactory.generateSecret(desKeySpec);
}

From source file:com.pdftron.pdf.utils.Utils.java

public static String decryptIt(Context context, String value) {
    String cryptoPass = context.getString(context.getApplicationInfo().labelRes);
    try {//from ww  w .java 2 s .  c o m
        DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);

        byte[] encrypedPwdBytes = Base64.decode(value, Base64.DEFAULT);
        // cipher is not thread safe
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));

        String decrypedValue = new String(decrypedValueBytes);
        Log.d("MiscUtils", "Decrypted: " + value + " -> " + decrypedValue);
        return decrypedValue;

    } catch (Exception e) {
        Log.e(e.getClass().getName(), e.getMessage());
    }
    return value;
}

From source file:com.pdftron.pdf.utils.Utils.java

public static String encryptIt(Context context, String value) {
    String cryptoPass = context.getString(context.getApplicationInfo().labelRes);
    try {//from   w ww  .ja  v a  2  s  .  c om
        DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);

        byte[] clearText = value.getBytes("UTF8");
        // Cipher is not thread safe
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        String encrypedValue = Base64.encodeToString(cipher.doFinal(clearText), Base64.DEFAULT);
        Log.d("MiscUtils", "Encrypted: " + value + " -> " + encrypedValue);
        return encrypedValue;

    } catch (Exception e) {
        Log.d(e.getClass().getName(), e.getMessage());
    }
    return value;
}

From source file:dev.ukanth.ufirewall.Api.java

/**
 * Encrypt the password/*from   w  w  w .jav a2  s. co m*/
 * @param key
 * @param data
 * @return
 */
public static String hideCrypt(String key, String data) {
    if (key == null || data == null)
        return null;
    String encodeStr = null;
    try {
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(charsetName));
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        byte[] dataBytes = data.getBytes(charsetName);
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        encodeStr = Base64.encodeToString(cipher.doFinal(dataBytes), base64Mode);

    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage());
    }
    return encodeStr;
}

From source file:dev.ukanth.ufirewall.Api.java

/**
 * Decrypt the password//from   ww w. j a  va2  s . co  m
 * @param key
 * @param data
 * @return
 */
public static String unhideCrypt(String key, String data) {
    if (key == null || data == null)
        return null;

    String decryptStr = null;
    try {
        byte[] dataBytes = Base64.decode(data, base64Mode);
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(charsetName));
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] dataBytesDecrypted = (cipher.doFinal(dataBytes));
        decryptStr = new String(dataBytesDecrypted);
    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage());
    }
    return decryptStr;
}

From source file:com.flexoodb.common.FlexUtils.java

static public String encryptdes(String passkey, String texttoencrypt) throws Exception {
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    byte key[] = passkey.trim().getBytes();
    DESKeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey desKey = keyFactory.generateSecret(desKeySpec);

    Cipher desCipher;/*from ww  w . ja va  2  s .  c o m*/

    // Create the cipher
    desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

    // Initialize the cipher for encryption
    desCipher.init(Cipher.ENCRYPT_MODE, desKey);

    // initialize base 64 encoding
    //org.apache.axis.encoding.Base64 base64 = new org.apache.axis.encoding.Base64();
    byte[] cleartext = texttoencrypt.getBytes();

    // Encrypt the cleartext
    byte[] ciphertext = desCipher.doFinal(cleartext);

    // encode to base64
    ciphertext = org.apache.axis.encoding.Base64.encode(ciphertext).getBytes();

    return new String(ciphertext);
}

From source file:com.flexoodb.common.FlexUtils.java

static public String decryptdes(String passkey, String texttoencrypt) throws Exception {
    Security.addProvider(new com.sun.crypto.provider.SunJCE());

    byte key[] = passkey.trim().getBytes();
    DESKeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey desKey = keyFactory.generateSecret(desKeySpec);

    Cipher desCipher;//  www.  j ava 2 s  .  co  m

    // Create the cipher
    desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

    // initialize base64 handler
    //org.apache.axis.encoding.Base64 base64 = new org.apache.axis.encoding.Base64();

    // decode from base64 first
    byte[] ciphertext = org.apache.axis.encoding.Base64.decode(new String(texttoencrypt.getBytes()));

    // Initialize the same cipher for decryption
    desCipher.init(Cipher.DECRYPT_MODE, desKey);

    // Decrypt the ciphertext
    byte[] cleartext1 = desCipher.doFinal(ciphertext);

    return new String(cleartext1);

}