java单des加密_Java开发经验分享之JAVA简单实现DES加密与实现DES解密

public classDesEncrypt {//DES加密密钥key

public static String key = "sdfDA12r3JHV214IJrwerDSO892BK2345nrekk35oewr4wrwrenlklknsdlemifzkw8iiiifegJG7649UJNDFJSvgsfdjFGDFGj435jUhjhjbkajb12kj987gsjh9834tbAXiudhf9B3PM4bt98dyf9Q2m97jjyf417aliD";//DES加密明文plaintext

@SuppressWarnings("static-access")public staticString encryptDES(String plaintext) {try{//首先,DES算法要求有一个可信任的随机数源,可以通过 SecureRandom类,内置两种随机数算法,NativePRNG和SHA1PRNG

SecureRandom random = newSecureRandom();//创建一个DESKeySpec对象

DESKeySpec desKey = newDESKeySpec(key.getBytes());//创建一个密匙工厂

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");//将DESKeySpec对象转换成SecretKey对象

SecretKey securekey =keyFactory.generateSecret(desKey);//Cipher对象实际完成加密操作

Cipher cipher = Cipher.getInstance("DES");//用密匙初始化Cipher对象

cipher.init(cipher.ENCRYPT_MODE, securekey, random);//加密生成密文byte数组

byte[] cipherBytes =cipher.doFinal(plaintext.getBytes());//将密文byte数组转化为16进制密文

String ciphertext =byteToHex(cipherBytes);returnciphertext;

}catch(Throwable e) {

e.printStackTrace();

}return null;

}//DES解密

@SuppressWarnings("static-access")public staticString decryptDES(String ciphertext) {try{//DES算法要求有一个可信任的随机数源,SecureRandom内置两种随机数算法,NativePRNG和SHA1PRNG,

// 通过new来初始化,默认来说会使用NativePRNG算法生成随机数,但是也可以配置-Djava.security参数来修改调用的算法,

// 如果是/dev/[u]random两者之一就是NativePRNG,否则就是SHA1PRNG。

SecureRandom random = newSecureRandom();//创建一个DESKeySpec对象

DESKeySpec desKey = newDESKeySpec(key.getBytes());//创建一个密匙工厂

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");//将DESKeySpec对象转换成SecretKey对象

SecretKey securekey =keyFactory.generateSecret(desKey);//Cipher对象实际完成解密操作

Cipher cipher = Cipher.getInstance("DES");//用密匙初始化Cipher对象

cipher.init(cipher.DECRYPT_MODE, securekey, random);//将16进制密文转化为密文byte数组

byte[] cipherBytes =hexToByte(ciphertext);//真正开始解密操作

String plaintext = newString(cipher.doFinal(cipherBytes));returnplaintext;

}catch(Throwable e) {

e.printStackTrace();

}return null;

}//将byte转化为16进制

public static String byteToHex(byte[] bs) {if (0 ==bs.length) {return "";

}else{

StringBuffer sb= newStringBuffer();for (int i = 0; i < bs.length; i++) {

String s= Integer.toHexString(bs[i] & 0xFF);if (1 ==s.length()) {

sb.append("0");

}

sb=sb.append(s.toUpperCase());

}returnsb.toString();

}

}//将16进制转化为byte

public static byte[] hexToByte(String ciphertext) {byte[] cipherBytes =ciphertext.getBytes();if ((cipherBytes.length % 2) != 0) {throw new IllegalArgumentException("长度不为偶数");

}else{byte[] result = new byte[cipherBytes.length / 2];for (int i = 0; i < cipherBytes.length; i += 2) {

String item= new String(cipherBytes, i, 2);

result[i/ 2] = (byte) Integer.parseInt(item, 16);

}returnresult;

}

}

// 进行测试public static voidmain(String[] args) {//当前时间戳

long timestamp =System.currentTimeMillis();

System.out.println(timestamp);//待加密内容

String str = "测试内容:Mr.JimYi" +timestamp;//密码,长度要是8的倍数

String ciphertext =encryptDES(str);

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

String plaintext=decryptDES(ciphertext);

System.out.println("解密后:" +plaintext);

String t= plaintext.substring(plaintext.length() - 13,

plaintext.length());

System.out.println(t);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值