java des使用详解_JAVA语言DES算法------

1、DES算法:

Java代码 收藏代码

/**

加解密算法

@param data 加解密数据

@param key 秘钥

@param mode 模式

@return 加解密结果

*/

public static byte[] desCryt(byte[] data, byte[] key, int mode){

byte[] result = null ;

try {

SecureRandom sr = new SecureRandom();

SecretKeyFactory keyFactory;

DESKeySpec dks = new DESKeySpec(key);

keyFactory = SecretKeyFactory.getInstance("DES");

SecretKey secretkey = keyFactory.generateSecret(dks);

//创建Cipher对象

Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");

//初始化Cipher对象

cipher.init(mode, secretkey, sr);

//加解密

result = cipher.doFinal(data);

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (InvalidKeySpecException e) {

e.printStackTrace();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

}

return result;

}

2、byte数组转换成16进制字符串

Java代码 收藏代码

/**

byte数组转换成16进制字符串

@param b

@return

*/

ublic static String bytes2HexString(byte[] b) {

String ret = "";

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

String hex = Integer.toHexString(b[i] & 0xFF);

if (hex.length() == 1) {

hex = '0' + hex;

}

ret += hex.toUpperCase();

}

return ret;

3、16进制字符串转成byte数组

Java代码 收藏代码

/**

16进制字符串转成byte数组

@param src

@return

/

ublic static byte[] hexString2Bytes(String src){

byte[] ret = new byte[8];

byte[] tmp = src.getBytes();

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

ret[i] = uniteBytes(tmp[i2], tmp[i*2+1]);

}

return ret;

}

Java代码 收藏代码

public static byte uniteBytes(byte src0, byte src1) {

byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();

_b0 = (byte)(_b0 << 4);

byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();

byte ret = (byte)(_b0 ^ _b1);

return ret;

}

执行

Java代码 收藏代码

public static void main(String[] args) {

//加解密模式

int mode = Cipher.ENCRYPT_MODE;

//被加解密byte数组16进制字符串

String dataHexString = "1234567887654321";

//秘钥byte数组16进制字符串

String keyHexString = "9AAB1D2EE004AAC3";

byte[] data = hexString2Bytes(dataHexString);

byte[] key = hexString2Bytes(keyHexString);

byte[] result = desCryt(data, key, mode);

//打印结果

System.out.println("结果:"+bytes2HexString(result));

}

结果:7D592BF239849E76

执行

Java代码 收藏代码

public static void main(String[] args) {

//加解密模式

int mode = Cipher.DECRYPT_MODE;

//被加解密byte数组16进制字符串

String dataHexString = "7D592BF239849E76";

//秘钥byte数组16进制字符串

String keyHexString = "9AAB1D2EE004AAC3";

byte[] data = hexString2Bytes(dataHexString);

byte[] key = hexString2Bytes(keyHexString);

byte[] result = desCryt(data, key, mode);

//打印结果

System.out.println("结果:"+bytes2HexString(result));

}

结果:1234567887654321

PS:

获取Cipher对象的时候一定要写成

Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");

不要写成

Cipher cipher = Cipher.getInstance("DES");

否则解密的时候会报错:

Given final block not properly padded

原因是Cipher cipher = Cipher.getInstance("DES");与Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");等同,填充方式错误,加密的时候会得到16长度的字节数组。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值