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. 读写锁ReadWriteLock

    为了提高性能,Java提供了读写锁,在读的地方使用读锁,在写的地方使用写锁,灵活控制,如果没有写锁的情况下,读是无阻塞的,在一定程度上提高了程序的执行效率. Java中读写锁有个接口java.util ...

  2. Hadoop记录-hadoop2.x常用端口及定义方法

    Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以及HTTP访问.而随着Hadoop周边组件的增多,完全记不住哪个端口对应哪个应用,特收集记录如 ...

  3. linux unknown host 问题【转】

    如果某台Linux(CentOS)服务器ping域名, 如下提示: # ping www.sina.comping: unknown host www.sina.com 确认网络没问题的情况下, 可以 ...

  4. java.lang.String & java.lang.StringBuilder

    java.lang.String & java.lang.StringBuilder String 成员方法 作用 public charAr(int index) 返回给定位置的代码单元 p ...

  5. CRM项目之RBAC权限组件-day26

    写在前面 上课第26天,打卡: 世间安得双全法 不负如来不负卿 s17day26 CRM项目 项目概要:XX公司CRM - 权限管理,公共组件,app ***** - 熟悉增删改查,Low *** - ...

  6. C#控件绘图恢复最小化后不自动重绘问题

    最近在学习C#中的绘图,使用控件绘图时发现一个现象:即使将绘图代码写在了Paint方法中,将窗口最小化再恢复后依然不会重绘,而只有将鼠标移到控件上或者有其他改变窗口的行为时才会重绘. 一开始以为是自己 ...

  7. Java SSM框架之MyBatis3(十)MyBatis批量插入数据(MySql)

    插入成功后返回自增主键 <insert id="insertRole" parameterType="role" useGeneratedKeys=&qu ...

  8. spring注解第03课 按条件加载Bean @Conditional

    package com.atguigu.config; import org.springframework.context.annotation.Bean; import org.springfra ...

  9. Hyperic-Sigar简介——检测与监控

    http://blog.csdn.net/liyong199012/article/details/20302761 Hyperic-Sigar是一个收集系统各项底层信息的工具集.他有如下特点: 1. ...

  10. Bootstrap 使用

    bootstrap模板为使IE6.7.8版本(IE9以下版本)浏览器兼容html5新增的标签,引入下面代码文件即可. <script src="https://oss.maxcdn.c ...