DES加解密工具类
来源:互联网 发布:机器视觉主流算法 编辑:程序博客网 时间:2024/06/06 00:12
Des算法加解密.注意,这里的key使用的是固定值+ip地址的八个字节的字节数组.在生成数组的时候,byte值为-128-127,所以,如果是192什么的,会超出byte的大小,可以直接对数字进行强转为byte型,也可以转为16进制数,还可以直接用他的负值来替换.这里的数组{0,1,0,8,192,168,1,103}不管怎么转换,最后其实都是{0,1,0,8,-64,-88,1,103}
package com.freetek.cc.utils;import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import java.io.IOException;import java.net.InetAddress;import java.security.SecureRandom;/** * DES加密 解密算法 * @author Young * @version 2017/5/5 * @email yangxiaoping@freetek.cc */public class DesUtil { private final static String DES = "DES"; private final static String ENCODE = "utf-8"; //默认key值 private final static String defaultKey = "test1234"; //运营系统提供的ticket private final static String sysTicket = "123d8ce011a5abf1edf62df84123c6b2"; private final static String localTicket = "freetekcc"; public static void main(String[] args) throws Exception { // 获取IP地址 byte[] address = InetAddress.getLocalHost().getAddress(); //直接数字转byte byte[] k = {(byte) 0,(byte) 1,(byte) 0,(byte) 8, (byte) 192,(byte) 168,(byte) 1,(byte) 103}; //转换为16进制转byte byte[] k1 = {(byte)0x0,(byte)0x1,(byte)0,(byte)8, (byte) 0xC0,(byte)0xA8,(byte)1,(byte)0x67}; //直接获取ip拼接 byte[] pre_k = {0,1,0,8}; byte[] key = addBytes(pre_k,address); /*String encrypt = encrypt(sysTicket, k); System.out.println("加密后的数据:" + encrypt); String decrypt = decrypt(encrypt, k); System.out.println("解密后的数据:" + decrypt);*/ String encrypt2 = encryptIV(sysTicket, key); System.out.println("带IV值的加密后的数据:" + encrypt2); String decrypt2 = decryptIV(encrypt2, key); System.out.println("带IV值的解密后的数据:" + decrypt2); } /** * 带iv值的解密 * 这里的iv值跟key值一样 * @param decryptString * @param decryptKey * @return * @throws Exception */ public static String decryptIV(String decryptString, byte[] decryptKey) throws Exception { IvParameterSpec iv = new IvParameterSpec(decryptKey); SecretKeySpec key = new SecretKeySpec(decryptKey, "DES"); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, iv); return new String(cipher.doFinal(Base64.decode(decryptString))); } /** * 带iv值的加密 * 这里的iv值跟key值一样 * @param encryptString * @param encryptKey * @return * @throws Exception */ public static String encryptIV(String encryptString, byte[] encryptKey) throws Exception { IvParameterSpec iv = new IvParameterSpec(encryptKey); DESKeySpec dks = new DESKeySpec(encryptKey); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, iv); return Base64.encode(cipher.doFinal(encryptString.getBytes())); } /** * 使用 默认key 加密 */ public static String encrypt(String data) throws Exception { byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE)); String strs = new BASE64Encoder().encode(bt); return strs; } /** * 使用 默认key 解密 */ public static String decrypt(String data) throws IOException, Exception { if (data == null) return null; BASE64Decoder decoder = new BASE64Decoder(); byte[] buf = decoder.decodeBuffer(data); byte[] bt = decrypt(buf, defaultKey.getBytes(ENCODE)); return new String(bt, ENCODE); } /** * Description 根据键值进行加密 * * @param data * @param key * 加密键byte数组 * @return * @throws Exception */ public static String encrypt(String data, String key) throws Exception { byte[] bt = encrypt(data.getBytes(ENCODE), key.getBytes(ENCODE)); String strs = new BASE64Encoder().encode(bt); return strs; } /** * Description 根据键值进行解密 * * @param data * @param key * 加密键byte数组 * @return * @throws IOException * @throws Exception */ public static String decrypt(String data, String key) throws IOException, Exception { if (data == null) return null; BASE64Decoder decoder = new BASE64Decoder(); byte[] buf = decoder.decodeBuffer(data); byte[] bt = decrypt(buf, key.getBytes(ENCODE)); return new String(bt, ENCODE); } /** * Description 根据键值进行加密 * * @param data * @param key * 加密键byte数组 * @return * @throws Exception */ public static String encrypt(String data, byte[] key) throws Exception { byte[] bt = encrypt(data.getBytes(ENCODE), key); String strs = new BASE64Encoder().encode(bt); return strs; } /** * Description 根据键值进行解密 * * @param data * @param key * 加密键byte数组 * @return * @throws IOException * @throws Exception */ public static String decrypt(String data, byte[] key) throws IOException, Exception { if (data == null) return null; BASE64Decoder decoder = new BASE64Decoder(); byte[] buf = decoder.decodeBuffer(data); byte[] bt = decrypt(buf, key); return new String(bt, ENCODE); } /** * Description 根据键值进行加密 * * @param data * @param key * 加密键byte数组 * @return * @throws Exception */ private static byte[] encrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 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.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); } /** * Description 根据键值进行解密 * * @param data * @param key * 加密键byte数组 * @return * @throws Exception */ private static byte[] decrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 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); } /** * * @param data1 * @param data2 * @return data1 与 data2拼接的结果 */ public static byte[] addBytes(byte[] data1, byte[] data2) { byte[] data3 = new byte[data1.length + data2.length]; System.arraycopy(data1, 0, data3, 0, data1.length); System.arraycopy(data2, 0, data3, data1.length, data2.length); return data3; }}
阅读全文
0 0
- DES加解密工具类
- DES加解密工具类
- DES加解密工具类
- DES加解密工具类
- 3DES加解密工具类
- C#DES文件加解密工具类
- JAVA工具类(16)---3des加解密工具类
- Java DES 加解密字符串、文件 工具类通用
- DES加解密及生成token工具类
- DES加密解密工具类
- DES 加密 解密 工具类
- des加密解密工具类
- 加解密工具类
- C# DES加密码解密类
- DES加解密算法实现类
- DES加解密
- Des加解密源代码
- DES加解密
- Intellij IDEA unresolved javascript function
- Spring整合hibernate配置
- makefile极好的极全面的自学博文
- Centos的yum源更换为国内的阿里云源
- 在Linux的Eclipse中编译使用动态链接库
- DES加解密工具类
- python 中trick 总结(一)
- Android_UI:Drawable
- Spring管理hibernate
- Unity Mecanim动画的实现(十):动画混合树、子状态机和状态行为
- Android中怎样防止内存泄漏?
- 收集各种API文档,提供下载.chm文件,HTML、java、w3school、servlet、js、spring、等等。
- 远程检测客户机上的共享设置情况
- 日志的生成