java des 16进制_DES加密后如何转换为16进制字符串

匿名用户

1级

2018-05-27 回答

可以通过下面的方法进行加密,key换成16位的密钥即可。

import java.io.IOException;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class DesUtil {

private final static String DES = "DES";

public static void main(String[] args) throws Exception {

String data = "123 456";

String key = "wang!@#$%";

System.err.println(encrypt(data, key));

System.err.println(decrypt(encrypt(data, key), key));

}

/**

* Description 根据键值进行加密

* @param data

* @param key 加密键byte数组

* @return

* @throws Exception

*/

public static String encrypt(String data, String key) throws Exception {

byte[] bt = encrypt(data.getBytes(), key.getBytes());

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());

return new String(bt);

}

/**

* Description 根据键值进行加密

* @param data

* @param key 加密键byte数组

* @return

* @throws Exception

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值