package me.zhengjie.core.utils;

import org.springframework.util.DigestUtils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec; /**
* 加密
* @author jie
* @date 2018-11-23
*/
public class EncryptUtils { private static String strKey = "Passw0rd", strParam = "Passw0rd"; /**
* 对称加密
* @param source
* @return
* @throws Exception
*/
public static String desEncrypt(String source) throws Exception {
if (source == null || source.length() == 0){
return null;
}
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(strParam.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return byte2hex(
cipher.doFinal(source.getBytes("UTF-8"))).toUpperCase();
} public static String byte2hex(byte[] inStr) {
String stmp;
StringBuffer out = new StringBuffer(inStr.length * 2);
for (int n = 0; n < inStr.length; n++) {
stmp = Integer.toHexString(inStr[n] & 0xFF);
if (stmp.length() == 1) {
// 如果是0至F的单位字符串,则添加0
out.append("0" + stmp);
} else {
out.append(stmp);
}
}
return out.toString();
} public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0){
throw new IllegalArgumentException("长度不是偶数");
}
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
} /**
* 对称解密
* @param source
* @return
* @throws Exception
*/
public static String desDecrypt(String source) throws Exception {
if (source == null || source.length() == 0){
return null;
}
byte[] src = hex2byte(source.getBytes());
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(strParam.getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(src);
return new String(retByte);
} /**
* 密码加密
* @param password
* @return
*/
public static String encryptPassword(String password){
return DigestUtils.md5DigestAsHex(password.getBytes());
} public static void main(String[] args) {
System.out.println(encryptPassword("e10adc3949ba59abbe56e057f20f883e"));
}
}
package me.zhengjie.core.utils;

import org.springframework.util.DigestUtils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec; /**
* 加密
* @author jie
* @date 2018-11-23
*/
public class EncryptUtils { private static String strKey = "Passw0rd", strParam = "Passw0rd"; /**
* 对称加密
* @param source
* @return
* @throws Exception
*/
public static String desEncrypt(String source) throws Exception {
if (source == null || source.length() == 0){
return null;
}
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(strParam.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return byte2hex(
cipher.doFinal(source.getBytes("UTF-8"))).toUpperCase();
} public static String byte2hex(byte[] inStr) {
String stmp;
StringBuffer out = new StringBuffer(inStr.length * 2);
for (int n = 0; n < inStr.length; n++) {
stmp = Integer.toHexString(inStr[n] & 0xFF);
if (stmp.length() == 1) {
// 如果是0至F的单位字符串,则添加0
out.append("0" + stmp);
} else {
out.append(stmp);
}
}
return out.toString();
} public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0){
throw new IllegalArgumentException("长度不是偶数");
}
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
} /**
* 对称解密
* @param source
* @return
* @throws Exception
*/
public static String desDecrypt(String source) throws Exception {
if (source == null || source.length() == 0){
return null;
}
byte[] src = hex2byte(source.getBytes());
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(strParam.getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(src);
return new String(retByte);
} /**
* 密码加密
* @param password
* @return
*/
public static String encryptPassword(String password){
return DigestUtils.md5DigestAsHex(password.getBytes());
} public static void main(String[] args) {
System.out.println(encryptPassword("e10adc3949ba59abbe56e057f20f883e"));
}
}

EncryptUtils的更多相关文章

  1. 自己写的AES和RSA加密解密工具

    package com.sdyy.common.utils; import java.security.Key; import java.security.KeyFactory; import jav ...

  2. app缓存设计-文件缓存

    采用缓存,可以进一步大大缓解数据交互的压力,又能提供一定的离线浏览.下边我简略列举一下缓存管理的适用环境: 1. 提供网络服务的应用 2. 数据更新不需要实时更新,哪怕是3-5分钟的延迟也是可以采用缓 ...

  3. c# 加密转载 备忘

    public sealed class EncryptUtils { #region Base64加密解密 /// <summary> /// Base64加密 /// </summ ...

  4. C# 加密解密(DES,3DES,MD5,Base64) 类

    public sealed class EncryptUtils     {         #region Base64加密解密         /// <summary>        ...

  5. 权限控制框架Spring Security 和Shiro 的总结

    关于权限控制,一开始感觉比较难,后来先是接触了Spring Security 学起来也比较吃力,再是学习了Shiro,感觉简单很多. 总体来说这些框架,主要做了两个事情 Authentication: ...

  6. PBOC圈存时用到3DES加密解密以及MAC计算方法

    最近在做PBOC圈存时用到了3DES的加密解密以及MAC计算问题,在网上了解一些知识,复制了一些demo学习,我这里没有深入研究,只是把我用到的和了解的做个总结,便于以后使用和学习. 3DES分双倍长 ...

  7. App安全(一) Android防止升级过程被劫持和换包

    文/ Tamic 地址/ http://blog.csdn.net/sk719887916/article/details/52233112 前言 APP 安全一直是开发者头痛的事情,越来越多的安全漏 ...

  8. 开发企业微信打卡API笔记

    获取企业微信打开API上面的数据 根据企业ID和打卡模块的secret获取access_token 打卡传参body为json格式的字符传 创建打卡对象把参数写入,useridlist为list格式. ...

  9. android:Android开发不得不收藏的Utils

    AndroidUtils AndroidUtils Android开发不得不收藏的Utils 之前写这篇文章主要是项目应用到的Utils,发现已经有一个更全面的开源库总结,所以还是非常震惊可以总结的这 ...

随机推荐

  1. 2014_csu选拔1_B

    Description Here is no naked girl nor naked runners, but a naked problem: you are to find the K-th s ...

  2. 201709-1 打酱油 Java

    思路: 先看能不能买5瓶,因为送的最多,然后看能不能买3瓶,最后一瓶一瓶地买 import java.util.Scanner; public class Main { public static v ...

  3. 吴裕雄--天生自然 JAVASCRIPT开发学习: 错误 - throw、try 和 catch

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. LCIS HDU - 3308 (线段树区间合并)

    LCIS HDU - 3308 Given n integers. You have two operations: U A B: replace the Ath number by B. (inde ...

  5. vi粘贴代码后格式混乱的问题

    最近在远程终端使用vi发现从其他地方复制代码的后,粘贴到vi里面出现格式变乱的问题. 主要是因为终端通常无法区分输入是来自用户输入还是来自粘贴,所以终端也不能通知 vim 输入来自何处.在 vim 里 ...

  6. 2020/1/29 PHP代码审计之进一步学习XSS【持续更新】

    0x00 上午学习了XSS漏洞,中午吃饭想了想,还是思考的太浅层了,这种老生常谈的东西对于现在的我意义不大.现在我需要的是思考.于是就有了这个随笔.在本文中,我会持续更新一些XSS的深入思考,payl ...

  7. GaussDB数据dump实现完全同步

    问题背景:搭建服务后端容灾集群,服务正常时容灾DB需要从业务DB完全同步数据,服务异常时,容灾DB停止抽取数据,自动从探针采集业务数据. 解决方案:常用的有两种思路,一是从服务后端定时每天拉取业务DB ...

  8. 最小二乘拟合(scipy实现)

    Scipy库在numpy库基础上增加了众多数学,科学及工程计算中常用库函数.如线性代数,常微分方程数值求解,信号处理,图像处理,稀疏矩阵等. 如下理解通过Scipy进行最小二乘法拟合运算 最小二乘拟合 ...

  9. [CF百场计划]#2 Codeforces Round #618 (Div. 2)

    A. Non-zero Description: Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \d ...

  10. linux 解压命令总结

    常用Linux 命令: [转自]https://www.jianshu.com/p/ca41f32420d6 解压缩 .tar 解包:tar xvf FileName.tar 打包:tar cvf F ...