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:de.fhdo.helper.DES.java

public static String encrypt(String Text) {
    try {//from w  ww .j  ava  2  s . c  o  m
        DESKeySpec keySpec = new DESKeySpec("schluessel_stdrepository15".getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);

        // ENCODE plainTextPassword String
        byte[] cleartext = Text.getBytes("UTF8");
        Cipher cipher = Cipher.getInstance("DES");
        // cipher is not thread safe
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return Base64.encodeBase64URLSafeString(cipher.doFinal(cleartext));

    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:com.java.demo.DesDemo.java

public static void Init() {
    try {/*from w w w.  j  a va 2s  .  co  m*/
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(56);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] bytesKey = secretKey.getEncoded();

        DESKeySpec dESKeySpec = new DESKeySpec(bytesKey);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
        convertSecretKey = factory.generateSecret(dESKeySpec);

    } catch (Exception e) {
    }
}

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

/**
 * /*www  .  j  a  v  a  2s.c  om*/
 * ?key
 * 
 * @param KeyStr
 *            
 * @return 
 * @throws InvalidKeyException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws Exception
 */
private static SecretKey keyGenerator(String keyStr) throws Exception {
    byte input[] = HexString2Bytes(keyStr);
    DESKeySpec desKey = new DESKeySpec(input);
    // ?DESKeySpec??
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey securekey = keyFactory.generateSecret(desKey);
    return securekey;
}

From source file:Encrypt.java

private static String encrypt(String message) throws Exception {
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
    IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
    return toHexString(cipher.doFinal(message.getBytes("UTF-8")));
}

From source file:Main.java

public static Key getDESKey(byte[] key)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    DESKeySpec des = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_DES);
    return keyFactory.generateSecret(des);
}

From source file:org.talend.commons.utils.PasswordEncryptUtil.java

private static SecretKey getSecretKey() throws Exception {
    if (key == null) {

        byte rawKeyData[] = rawKey.getBytes();
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //$NON-NLS-1$
        key = keyFactory.generateSecret(dks);
    }//from   w w w. j a v a  2 s  .c  o m
    return key;
}

From source file:de.uzk.hki.da.passwordEncryptor.passwordEncryptor.java

private static String encryptPasswordForContentBroker(String password) {

    byte key[] = "394z57f4".getBytes();
    byte encryptedPassword[];

    try {/*from  w  w  w .  j  av  a2 s  .c  o  m*/
        SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key));

        Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
        encrypt.init(Cipher.ENCRYPT_MODE, secretKey,
                new IvParameterSpec(new byte[] { 0x01, 0x02, 0x04, 0x10, 0x01, 0x02, 0x04, 0x10 }));
        encryptedPassword = encrypt.doFinal(password.getBytes());
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Couldn't encrypt password " + password + e);
    }

    return new String(Base64.encodeBase64(encryptedPassword));
}

From source file:de.uzk.hki.da.utils.PasswordUtils.java

public static String decryptPassword(String password) {

    byte key[] = "394z57f4".getBytes();
    byte decryptedPassword[];

    try {/*from w  w w  .  jav a 2s . c o  m*/
        SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key));
        Cipher decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
        decrypt.init(Cipher.DECRYPT_MODE, secretKey,
                new IvParameterSpec(new byte[] { 0x01, 0x02, 0x04, 0x10, 0x01, 0x02, 0x04, 0x10 }));
        decryptedPassword = decrypt.doFinal(Base64.decodeBase64(password.getBytes()));

    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Couldn't decrypt password " + password + e);
    }

    return new String(decryptedPassword);
}

From source file:org.beangle.security.codec.DESEncrypt.java

public byte[] doEncrypt(byte plainText[]) throws Exception {
    SecureRandom sr = new SecureRandom();
    byte rawKeyData[] = desKey;
    DESKeySpec dks = new DESKeySpec(rawKeyData);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    javax.crypto.SecretKey key = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(1, key, sr);/*  ww w.  ja va2s . c  om*/
    byte data[] = plainText;
    byte encryptedData[] = cipher.doFinal(data);
    return encryptedData;
}

From source file:Main.java

private static byte[] doFinal(String key, int opmode, byte[] input)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException {
    key = checkNull(key) ? DEFAULT_KEY : key;
    if (checkNull(key)) {
        return null;
    }//  ww w.  j  a v a 2s.c o m
    SecureRandom sr = new SecureRandom();
    DESKeySpec dks = new DESKeySpec(key.getBytes());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance(MODE);
    cipher.init(opmode, securekey, sr);
    return cipher.doFinal(input);
}