package tech.fullink.eaglehorn.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec; /**
* DES CBC 加密、解密算法
*
* @author xiaoliang.chen
* @version $Id: DesCbcSecurity.java, v 0.1 2017年12月16日 下午12:55:29 xiaoliang.chen Exp $
*/
public class DesCbcSecurity { public static final String SECRET_DES = "DES";
public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding"; public static void main(String[] args) throws Exception {
String aaaString = "{\"customerUserId\": \"10\",\"customerUserType\": 1,\"name\": \"hello\",\"mobile\": \"13366666666\",\"idCardNo\": \"330106200009096666\",\"deviceId\": \"qwert\",\"token\": \"qwertyuiop12345\"}";
String key = "0uw5Wzwe";
System.out.println("加密前:" + aaaString);
String encrypedString = encrypt(aaaString, key); System.out.println("加密后: " + encrypedString);
String bString = decrypt(encrypedString, key);
System.out.println("解密后:" + bString);
}
/**
* 加密
* @author xiaoliang.chen
* 2017年12月16日 下午12:59:28
* @param content
* @param key
* @return
*/
public static String encrypt(String content, String key) {
return byteToHexString(encrypt(content.getBytes(), key.getBytes()));
} public static byte[] encrypt(byte[] content, byte[] keyBytes) {
try {
DESKeySpec keySpec = new DESKeySpec(keyBytes);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_DES);
SecretKey key = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(keySpec.getKey()));
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
* @author xiaoliang.chen
* 2017年12月16日 下午1:01:01
* @param content
* @param key
* @return
*/
public static String decrypt(String content, String key) {
byte[] contentBytes = hexStringToBytes(content);
return decrypt(contentBytes, key.getBytes());
} public static String decrypt(byte[] content, byte[] keyBytes) {
try {
DESKeySpec keySpec = new DESKeySpec(keyBytes);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_DES);
SecretKey key = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(keyBytes));
byte[] result = cipher.doFinal(content);
String contentString = new String(result);
return contentString;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public static String byteToHexString(byte[] bytes) {
StringBuffer sb = new StringBuffer(bytes.length);
String sTemp;
for (int i = 0; i < bytes.length; i++) {
sTemp = Integer.toHexString(0xFF & bytes[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
} public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); }
return d;
} private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}

  

Des加密解密算法java实现的更多相关文章

  1. java 实现 DES加密 解密算法

    DES算法的入口参数有三个:Key.Data.Mode.其中Key为8个字节共64位,是DES算法的工作密钥:Data也为8个字节64位,是要被加密或被解密的数据:Mode为DES的工作方式,有两种: ...

  2. DES加密解密算法C语言代码实现

    代码: #include<stdio.h> #include<string.h> #include<stdlib.h> /*-------------------- ...

  3. android -------- DES加密解密算法

    DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1977年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),并授权在非密级政府通信 ...

  4. AES加密解密算法---java

    package com.BFGJ.AES; import java.util.Random; import java.util.StringTokenizer; import javax.crypto ...

  5. C#和PHP加密结果一致的DES加密解密算法。php实现和c#一致的DES加密解密

    DES加密算法 des对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码),是一种对称加密 ...

  6. C#MD5加密和DES加密解密算法

    public partial class stringTest : System.Web.UI.Page     {         protected void Page_Load(object s ...

  7. DES加密解密算法C++实现

    DES加密算法并不难,是由一些简单的变换得来的,难的是要有足够的耐心.蒟蒻并不想说自己用了多久才把代码写好的. 代码: 我真的太难了QAQ #include<iostream> using ...

  8. DES对 json 、http参数加密解密算法

    网上众多大神们的众多方式实现加解密操作及保障数据安全性.今天无意中发现一篇以 DES加密解密算法.摘抄如下 工具类: import java.security.SecureRandom; import ...

  9. des加密解密——java加密,php解密

    最近在做项目中,遇到des加密解密的问题. 场景是安卓app端用des加密,php这边需要解密.之前没有接触过des这种加密解密算法,但想着肯定会有demo.因此百度,搜了代码来用.网上代码也是鱼龙混 ...

随机推荐

  1. 【Mac进销存管理软件】Daily Sales Pro Mac

        [简介] Daily Sales Mac版是Mac平台上的一款进销存软件,库存管理系统.Daily Sales Mac版是一款易于使用的进出库存管理软件,让您及时了解库存状况.销售收入.采购成 ...

  2. 2017-12-15python全栈9期第二天第五节之格式化输出补充之想要在格式化输出中表示单纯的%号就加%

    #!/user/bin/python# -*- coding:utf-8 -*-name = input('姓名:')age = input('年龄:')height = input('身高:')ms ...

  3. TF报错解决

    一.import tensorflow #h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubd ...

  4. centos系统中php7安装memcached 扩展

    #编译安装php-7.1.16 #wget http://cn2.php.net/distributions/php-7.1.16.tar.gz#tar -zxvf php-7.1.16.tar.gz ...

  5. 网络编程基础【day09】:通过socket实现简单ssh客户端(三)

    本节内容 1.概述 2.socket发送中文 3.重复发送和多次接收 4.模拟ssh客户端 一.概述 本篇博客讲一下,如果socket客户端断了,另外的客户端怎么接入服务端,还有模拟ssh的链接等. ...

  6. maven坑-Failure to transfer org.apache.maven:maven

    参考网址:http://www.mkyong.com/maven/how-to-convert-maven-java-project-to-support-eclipse-ide/ https://b ...

  7. python 管道 事件(Event) 信号量 进程池(map/同步/异步)回调函数

    ####################总结######################## 管道:是进程间通信的第二种方式,但是不推荐使用,因为管道会导致数据不安全的情况出现 事件:当我运行主进程的 ...

  8. python 正则表达式re模块

    #####################总结##############    优点:  灵活, 功能性强, 逻辑性强.               缺点:  上手难,旦上手, 会爱上这个东西    ...

  9. HTTP Method小结

    GET 从指定的url上获取内容 POST 提交body中的内容给服务器中指定的url中,属于非幂等的(non-idempotent)请求 HEAD 从指定的url上获取header内容(类似Get方 ...

  10. Sqlserver中的视图

    一.视图的基本知识 什么是视图:视图是从一个或多个表导出的虚拟的表,具有普通表的结构,物理上是不存在的.视图是动态的数据的集合,数据是随着基表的更新而更新. 视图的优点: ①在多表查询时,查询方便. ...