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. IDEA tomcat 部署WEB项目

    1. 2. 3.

  2. golang 使用os/exec配合context实现的超时机制

    在使用golang开发中,调用外部可执行程序通过exec包是我们常用的方式.如何控制超时请见如下样例: var ( Timeout = 3 * time.Second ) func Command(n ...

  3. mysql 遍历所有的库并根据表和sql语句备份

    建库.用户语句 create database test_hb; create user ' test_hb'@'%' identified by '123456'; grant all privil ...

  4. Dubbo优雅关机原理

    Dubbo是通过JDK的ShutdownHook来完成优雅停机的 所以如果用户使用 kill -9 PID 等强制关闭命令,是不会执行优雅停机的 只有通过 kill PID时,才会执行 原理: · 服 ...

  5. JWT认证

    1.什么是JWT Token JWT(Json Web Tokens) 是一个开放标准(RFC 7519),它定义了一种简洁,自包含,JSON 对象形式的安全传递信息的方法.JWT常用在 Web 应用 ...

  6. Ruby页面,循环赋值方法(类似java EL表达式赋值)

    ------------前台代码--------------- <% @form_hash.each_with_index do |f,index| %> <% item = f[: ...

  7. 搭建idea下的vue工程

    需要先安装好nodejs和npm 输入下面的命令查看是否成功安装 node -v npm -v 一.开始 工作目录:IdeaProjects使用idea新建Static Web项目:demo 在dem ...

  8. [Android] Android 使用 Greendao 操作 db sqlite(2)-- 封装DaoUtils类

    继续接上文: Android 使用 Greendao 操作 db sqlite(1)-- 直接在MainActivity中调用 布局文件同上文一致,这里就不贴了. 一.封装DaoUtils类 User ...

  9. git 提交解决冲突(转载)

    转载 git 提交解决冲突 http://www.cnblogs.com/qinbb/p/5972308.html   一:git命令在提交代码前,没有pull拉最新的代码,因此再次提交出现了冲突. ...

  10. 'DataVisualization' does not exist in the namespace 'System.Web.UI'一例解决办法

    之前项目是vs2010 aspx项目,用vs2017打开后,非运行状态下有一行错误:CS0234 C# The type or namespace name 'DataVisualization' d ...