Java 8 DESKeySpec - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 DESKeySpec class.

This code example shows how to use the following methods:
public class NullKey { 
 
    public static void main(String[] args) throws Exception {
 
        try { 
            DESKeySpec desSpec = new DESKeySpec(null);
            throw new Exception("expected NullPointerException");
        } catch (NullPointerException npe) {}
 
        try { 
            DESKeySpec desSpec = new DESKeySpec(null, 0);
            throw new Exception("expected NullPointerException");
        } catch (NullPointerException npe) {}
 
        try { 
            boolean parityAdjusted = DESKeySpec.isParityAdjusted(null, 0);
            throw new Exception("expected InvalidKeyException");
        } catch (InvalidKeyException ike) {}
 
        try { 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
    public static byte[] desEncode(final String key, final byte[] src)
            throws InvalidKeyException, UnsupportedEncodingException, 
            NoSuchAlgorithmException, InvalidKeySpecException, 
            NoSuchPaddingException, IllegalBlockSizeException, 
            BadPaddingException { 
        final DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        final SecretKey sKey = keyFactory.generateSecret(keySpec);
 
        final Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
        cipher.init(Cipher.ENCRYPT_MODE, sKey);
        final byte[] dst = cipher.doFinal(src);
 
        return dst;
    } 
 
    /** 
     * DES decoder 
     */ 
    public static byte[] desDecode(final String key, final byte[] src)
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
	 * @throws Exception 
	 */ 
	private static Key toKey(byte[] key) throws Exception {
 
		// 实例化DES密钥材料 
		DESKeySpec dks = new DESKeySpec(key);
 
		// 实例化秘密密钥工厂 
		SecretKeyFactory keyFactory = SecretKeyFactory
				.getInstance(KEY_ALGORITHM);
 
		// 生成秘密密钥 
		SecretKey secretKey = keyFactory.generateSecret(dks);
 
		return secretKey;
	} 
 
	/** 
	 * 解密 
	 *  
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
    private Key getKey(String serialNumber) {
        Key secretKey = null;
        try { 
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(serialNumber.getBytes());
            DESKeySpec dks = new DESKeySpec(md5.digest());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            secretKey = keyFactory.generateSecret(dks);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } 
 
        return secretKey;
    } 
 
    /** 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
	 * @throws CryptException 
	 *             异常 
	 */ 
	public static String encode(String key, byte[] data) throws Exception {
		try { 
			DESKeySpec dks = new DESKeySpec(key.getBytes());
 
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			// key的长度不能够小于8位字节 
			Key secretKey = keyFactory.generateSecret(dks);
			Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
			IvParameterSpec iv = new IvParameterSpec(key.getBytes());
			AlgorithmParameterSpec paramSpec = iv;
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
 
			byte[] bytes = cipher.doFinal(data);
			return Base64.encodeBase64String(bytes);
		} catch (Exception e) {
			throw new Exception(e);
		} 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
	} 
	 
	public static final Cipher getEncriptyCipher() {
		try { 
			if (null == encripty.get()) {
				DESKeySpec key = new DESKeySpec(PASSWORD.getBytes());
				SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
				SecretKey genKey = keyFactory.generateSecret(key);
				Cipher enCipher = Cipher.getInstance("DES");
				enCipher.init(Cipher.ENCRYPT_MODE, genKey);
				encripty.set(enCipher);
			} 
		} catch (Exception e) {
			logger.error("Init DES Cipher Failed!"); 
			e.printStackTrace();
		} 
		 
		return encripty.get();
	} 
} 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
	 * @param key 
	 * @return 
	 * @throws Exception 
	 */ 
	private static Key toKey(byte[] key) throws Exception {
		DESKeySpec dks = new DESKeySpec(key);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
		SecretKey secretKey = keyFactory.generateSecret(dks);
 
		return secretKey;
	} 
 
	/** 
	 * 解密 
	 *  
	 * @param data 
	 * @param key 
	 * @return 
	 * @throws Exception 
	 */ 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
			throw new EntityException(e);
		} 
	} 
	 
	private Cipher createCipher(boolean isEncrypt) throws Exception{
		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(encoding));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
		IvParameterSpec ips = new IvParameterSpec(iv.getBytes(encoding));
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); 
        cipher.init(isEncrypt?Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE, secretKey, ips);  
        return cipher;
	} 
	 
 
	public static void main(String[] args) {
		DES des=new DES();
		String e=des.encrypt("jxvaframework");
		System.out.println(e);
		System.out.println(des.decrypt(e));
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
			throw new EntityException(e);
		} 
	} 
	 
	private Cipher createCipher(boolean isEncrypt) throws Exception{
		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(encoding));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
		IvParameterSpec ips = new IvParameterSpec(iv.getBytes(encoding));
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); 
        cipher.init(isEncrypt?Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE, secretKey, ips);  
        return cipher;
	} 
	 
 
	public static void main(String[] args) {
		DES des=new DES();
		String e=des.encrypt("jxvaframework");
		System.out.println(e);
		System.out.println(des.decrypt(e));
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Java 8 DESKeySpec Questions & Answers:

See Code Examples for Java 8 DESKeySpec Methods: