java des cbc示例_java 实现DES 加密解密的示例

package com.cn.peitest;

import javax.crypto.Cipher;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

import javax.crypto.spec.IvParameterSpec;

/**

* @功能说明:

* @创建日期:2016年9月21日

* @变更记录:

* 1、2016年9月21日 LeoLu 更新

*/

public class DESUtil {

/**用于建立大写的十六进制字符的输出*/

private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

/**DES向量*/

private static final byte[] iv = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef};

//private static final Logger log = LoggerFactory.getLogger(DESUtil.class);

/**

* @函数名称:encodeHex

* @创建日期:2016年9月22日

* @功能说明: 将字节数组转换为十六进制字节数组

* @参数说明:data byte[] 字节数组

* @参数说明:toDigits char[] 向量

* @返回说明:十六进制char[]

*/

private static char[] encodeHex(byte[] data, char[] toDigits) {

int l = data.length;

char[] out = new char[l << 1];

for (int i = 0, j = 0; i < l; i++) {

out[j++] = toDigits[(0xF0 & data[i]) >>> 4];

out[j++] = toDigits[0x0F & data[i]];

}

return out;

}

/**

* @函数名称:encodeHexStr

* @创建日期:2016年9月22日

* @功能说明:将16进制字节数组转换为十六进制字符串

* @参数说明:data byte[] 16进制字节数组

* @参数说明:toDigits char[] 向量

* @返回说明:String 返回16进制字符串

*/

private static String encodeHexStr(byte[] data, char[] toDigits) {

return new String(encodeHex(data, toDigits));

}

/**

* @函数名称:hexStringToString

* @创建日期:2016年9月21日

* @功能说明:将16进制字符串转换为10进制字符串

* @参数说明:str String 16进制字符串

* @返回说明:String

*/

private static String hexStringToString(String str) {

if (str == null || str.equals("")) {

return null;

}

str = str.replace(" ", "");

byte[] baKeyword = new byte[str.length() / 2];

for (int i = 0; i < baKeyword.length; i++) {

try {

baKeyword[i] = (byte) (0xff & Integer.parseInt(

str.substring(i * 2, i * 2 + 2), 16));

} catch (Exception e) {

e.printStackTrace();

}

}

try {

str = new String(baKeyword, "UTF-8");

new String();

} catch (Exception e1) {

e1.printStackTrace();

}

return str;

}

/**

* @函数名称:encrypt

* @创建日期:2016年9月22日

* @功能说明:加密字节数组

* @参数说明:arrB byte[] 需要加密的字节数组

* @参数说明:key String 秘钥

* @返回说明:byte[]

*/

private static byte[] encrypt(byte[] arrB, String key) throws Exception {

return converCrypt(arrB, key, true);

}

/**

* @函数名称:encrypt

* @创建日期:2016年9月22日

* @功能说明:加密字符串

* @参数说明:xml String 加密字符串

* @参数说明:key String 秘钥

* @返回说明:String 返回加密后的16进制字符串

*/

public static String encrypt(String xml, String key) {

try {

return encodeHexStr(encrypt(xml.getBytes("UTF-8"), key), DIGITS_UPPER);

} catch (Exception e) {

System.out.println(e);

return "";

}

}

/**

* @函数名称:decrypt

* @创建日期:2016年9月22日

* @功能说明: 将16进制字节数组进行解密

* @参数说明: arrB byte[] 解密字节数组

* @参数说明:key String 秘钥

* @返回说明:byte[] 返回解密后 的16位字节数组

*/

private static byte[] decrypt(byte[] arrB, String key) throws Exception {

return converCrypt(arrB, key, false);

}

/**

* @函数名称:converCrypt

* @创建日期:2016年9月22日

* @功能说明:将16位的字节数据进行加密或解密

* @参数说明: arrB byte[] 需要加密的字节数组

* @参数说明: key String 秘钥

* @参数说明:encrypt boolean是否加密,true加密,false解密

* @返回说明:byte[] 返回16进制字节数组

*/

private static byte[] converCrypt(byte[] arrB, String key, boolean encrypt) throws Exception{

String vikey = MD5.sign(key).substring(0, 8).toUpperCase();

DESKeySpec desKeySpec = new DESKeySpec(vikey.getBytes());

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

IvParameterSpec ivp = new IvParameterSpec(vikey.getBytes());

Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

/**加密*/

if (encrypt == true) {

cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivp);

} else{

/**解密*/

cipher.init(Cipher.DECRYPT_MODE, secretKey, ivp);

}

return cipher.doFinal(arrB);

}

/**

* @函数名称:decrypt

* @创建日期:2016年9月22日

* @功能说明:解密16进制字符串

* @参数说明: desStr String 需要解密的16进制字符串

* @参数说明: key String 秘钥

* @返回说明:String 返回解密后的10进制字符串

*/

public static String decrypt(String desStr, String key) {

try{

if (null == desStr || null == key) {

return "";

}

return hexStringToString(encodeHexStr(decrypt(hexStringToByte(new String(desStr.getBytes("UTF-8"))), key), DIGITS_UPPER));

} catch (Exception e) {

System.out.println();

return "";

}

}

/**

* @函数名称:hexStringToByte

* @创建日期:2016年9月22日

* @功能说明:将16进制字符串转换为16进制字节数组

* @参数说明:hex String需要转换的16进制字符串

* @返回说明:byte[] 返回转换后的16进制字节数组

*/

private static byte[] hexStringToByte(String hex) {

int len = (hex.length() / 2);

byte[] result = new byte[len];

char[] achar = hex.toCharArray();

for (int i = 0; i < len; i++) {

int pos = i * 2;

result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));

}

return result;

}

/**

* @函数名称:toByte

* @创建日期:2016年9月22日

* @功能说明: 将字符转换为字节

* @参数说明: c char 需要转换的字符

* @返回说明:int 返回字符对应的字节码

*/

private static int toByte(char c) {

byte b = (byte) "0123456789ABCDEF".indexOf(c);

return b;

}

/**

* @构造函数

*/

public DESUtil() {

// TODO Auto-generated constructor stub

}

/**

* @函数名称:main

* @创建日期:2016年9月21日

* @功能说明:

* @参数说明:

* @返回说明:void

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

try {

System.out.println("-----------------------------");

String bStr = "123";

String key = "SZAOA589";

String binSing = encrypt(bStr, key);

System.out.println("加密前:"+bStr);

System.out.println("加密后:"+binSing);

System.out.println("解密后:"+decrypt(binSing, key));

System.out.println("-------------------------");

} catch (Exception e) {

// TODO Auto-generated catch bloc

e.printStackTrace();

}

//3B976A2A2919A60B57DFF3518F65E1FF

//3B976A2A2919A60B57DFF3518F65E1FF

/*

* C4A737D04D0D05E2

BD2DD4FC5050EBD0

*/

}

}

//================================

package com.cn.peitest;

import java.security.MessageDigest;

public class MD5 {

// 生成MD5(截取16位长度)

public static String sign(String message) {

String md5 = "";

try {

MessageDigest md = MessageDigest.getInstance("MD5"); // 创建一个md5算法对象

byte[] messageByte = message.getBytes("UTF-8");

byte[] md5Byte = md.digest(messageByte); // 获得MD5字节数组,16*8=128位

md5 = bytesToHex(md5Byte).substring(0, 16); // 转换为16进制字符串

} catch (Exception e) {

e.printStackTrace();

}

return md5;

}

// 二进制转十六进制

public static String bytesToHex(byte[] bytes) {

StringBuffer hexStr = new StringBuffer();

int num;

for (int i = 0; i < bytes.length; i++) {

num = bytes[i];

if (num < 0) {

num += 256;

}

if (num < 16) {

hexStr.append("0");

}

hexStr.append(Integer.toHexString(num));

}

return hexStr.toString().toUpperCase();

}

}

以上就是java 实现DES 加密解密的示例代码的详细内容,更多关于java des加密解密的资料请关注聚米学院其它相关文章!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值