common_helper/src/main/java/com/xiongyingqi/util/MessageEncryptionHelper.java
public static byte[] desEncrypt(byte rawKeyData[], byte[] data)
            throws InvalidKeyException, NoSuchAlgorithmException,
            IllegalBlockSizeException, BadPaddingException,
            NoSuchPaddingException, InvalidKeySpecException {
        // DES算法要求有一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        // 从原始密匙数据创建一个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.ENCRYPT_MODE, key, sr);
        // 现在，获取数据并加密
//        byte data[] = str.getBytes();
        // 正式执行加密操作
        byte[] encryptedData = cipher.doFinal(data);
//        System.out.println("加密后===>" + encryptedData);
        return encryptedData;
    }
public static byte[] desEncrypt(byte rawKeyData[], byte[] data)
            throws InvalidKeyException, NoSuchAlgorithmException,
            IllegalBlockSizeException, BadPaddingException,
            NoSuchPaddingException, InvalidKeySpecException {
        // DES算法要求有一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        // 从原始密匙数据创建一个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.ENCRYPT_MODE, key, sr);
        // 现在，获取数据并加密
//        byte data[] = str.getBytes();
        // 正式执行加密操作
        byte[] encryptedData = cipher.doFinal(data);
//        System.out.println("加密后===>" + encryptedData);
        return encryptedData;
    }
加密方法
<pre>
// 1.1  首先要创建一个密匙
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 为我们选择的DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(sr);
// 生成密匙
SecretKey key = kg.generateKey();
// 获取密匙数据
byte rawKeyData[] = key.getEncoded();
//        byte rawKeyData[] = "sucretsa".getBytes();
System.out.println("密匙长度===" + rawKeyData.length);
System.out.println("密匙Base64===" + Base64.encodeBytes(rawKeyData));

String str = "hi.baidu.com/beijingalana"; // 待加密数据
// 2.1  调用加密方法
byte[] encryptedData = desEncrypt(rawKeyData, str.getBytes());
// 3.1  调用解密方法
byte[] decrypt = desDecrypt(rawKeyData, encryptedData);
System.out.println("解密===" + new String(decrypt));
</pre>

@param rawKeyData 密钥
@param data       要加密的数据
@throws java.security.InvalidKeyException
@throws NoSuchAlgorithmException
@throws javax.crypto.IllegalBlockSizeException
@throws javax.crypto.BadPaddingException
@throws javax.crypto.NoSuchPaddingException
@throws java.security.spec.InvalidKeySpecException