base加密解密工具类
public class MLDUtil {
public static Key DEFAULT_KEY = null;
public static final String DEFAULT_SECRET_KEY1 = "?:P)(OL><KI*&UJMNHY^%TGBVFR$#EDCXSW@!QAZ";
public static final String DEFAULT_SECRET_KEY2 = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik,9ol.0p;/";
public static final String DEFAULT_SECRET_KEY3 = "!QAZ@WSX#EDC$RFV%TGB^YHN&UJM*IK<(OL>)P:?";
public static final String DEFAULT_SECRET_KEY4 = "1qaz@WSX3edc$RFV5tgb^YHN7ujm*IK<9ol.)P:?";
public static final String DEFAULT_SECRET_KEY5 = "!QAZ2wsx#EDC4rfv%TGB6yhn&UJM8ik,(OL>0p;/";
public static final String DEFAULT_SECRET_KEY6 = "1qaz2wsx3edc4rfv5tgb^YHN&UJM*IK<(OL>)P:?";
public static final String DEFAULT_SECRET_KEY = DEFAULT_SECRET_KEY1;
public static final String DES = "DES";
public static final Base32 base32 = new Base32();
static {
DEFAULT_KEY = obtainKey(DEFAULT_SECRET_KEY);
}
/**
* 获得key
**/
public static Key obtainKey(String key) {
if (key == null) {
return DEFAULT_KEY;
}
KeyGenerator generator = null;
try {
generator = KeyGenerator.getInstance(DES);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
generator.init(new SecureRandom(key.getBytes()));
Key key1 = generator.generateKey();
generator = null;
return key1;
}
/**
* 加密<br>
* String明文输入,String密文输出
*/
public static String encode(String str) {
return encode64(null, str);
}
/**
* 加密<br>
* String明文输入,String密文输出
*/
public static String encode64(String key, String str) {
return Base64.encodeBase64URLSafeString(obtainEncode(key, str.getBytes()));
}
/**
* 加密<br>
* String明文输入,String密文输出
*/
public static String encode32(String key, String str) {
return base32.encodeAsString(obtainEncode(key, str.getBytes())).replaceAll("=", "");
}
/**
* 加密<br>
* String明文输入,String密文输出
*/
public static String encode16(String key, String str) {
return Hex.encodeHexString(obtainEncode(key, str.getBytes()));
}
/**
* 解密<br>
* 以String密文输入,String明文输出
*/
public static String decode(String str) {
return decode64(null, str);
}
/**
* 解密<br>
* 以String密文输入,String明文输出
*/
public static String decode64(String key, String str) {
return new String(obtainDecode(key, Base64.decodeBase64(str)));
}
/**
* 解密<br>
* 以String密文输入,String明文输出
*/
public static String decode32(String key, String str) {
return new String(obtainDecode(key, base32.decode(str)));
}
/**
* 解密<br>
* 以String密文输入,String明文输出
*/
public static String decode16(String key, String str) {
try {
return new String(obtainDecode(key, Hex.decodeHex(str.toCharArray())));
} catch (DecoderException e) {
e.printStackTrace();
}
return null;
}
/**
* 加密<br>
* 以byte[]明文输入,byte[]密文输出
*/
private static byte[] obtainEncode(String key, byte[] str) {
byte[] byteFina = null;
Cipher cipher;
try {
Key key1 = obtainKey(key);
cipher = Cipher.getInstance(DES);
cipher.init(Cipher.ENCRYPT_MODE, key1);
byteFina = cipher.doFinal(str);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 解密<br>
* 以byte[]密文输入,以byte[]明文输出
*/
private static byte[] obtainDecode(String key, byte[] str) {
Cipher cipher;
byte[] byteFina = null;
try {
Key key1 = obtainKey(key);
cipher = Cipher.getInstance(DES);
cipher.init(Cipher.DECRYPT_MODE, key1);
byteFina = cipher.doFinal(str);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建一个DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
// 正式执行解密操作
return cipher.doFinal(src);
}
public final static String decrypt(String data, String key) {
try {
// 这里就没走
return new String(decrypt(String2byte(data.getBytes()), key.getBytes()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] String2byte(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;
}
public static String DataDecrypt(String str, byte[] key) {
String decrypt = null;
try {
byte[] ret = decrypt(Base64.decodeBase64(str), key);
decrypt = new String(ret, "UTF-8");
} catch (Exception e) {
System.out.print(e);
decrypt = str;
}
return decrypt;
}
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
// 正式执行加密操作
return cipher.doFinal(src);
}
public final static String encrypt(String password, String key) {
try {
return byte2String(encrypt(password.getBytes(), key.getBytes()));
} catch (Exception e) {
}
return null;
}
public static String byte2String(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
public static String DataEncrypt(String str, byte[] key) {
String encrypt = null;
try {
byte[] ret = encrypt(str.getBytes("UTF-8"), key);
encrypt = new String(Base64.decodeBase64(ret));
} catch (Exception e) {
System.out.print(e);
encrypt = str;
}
return encrypt;
}
public static void main(String[] args) throws InterruptedException {
String encrypt = encrypt("dxjkadmin", DEFAULT_SECRET_KEY2);
System.out.println(encrypt);
String desencryptString = decrypt("00E64BC6DB5530391EE1B6092E213772", DEFAULT_SECRET_KEY2);
System.out.println(desencryptString);
}
}
base加密解密工具类的更多相关文章
- Base64加密解密工具类
使用Apache commons codec类Base64进行加密解密 maven依赖 <dependency> <groupId>commons-codec</grou ...
- .Net(c#)加密解密工具类:
/// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelper { #region des实 ...
- 加密解密工具类(Java,DES)
一个Java版的DES加密工具类,能够用来进行网络传输数据加密,保存password的时候进行加密. import java.security.Key; import java.security.sp ...
- java加密解密工具类
package com.founder.mrp.util; import java.nio.charset.StandardCharsets; import java.security.Key; im ...
- AES加密解密工具类封装(AESUtil)
package club.codeapes.common.utils; import org.springframework.util.Base64Utils; import javax.crypto ...
- java 加密解密工具类(实用!!!)
最近发现了一个加密解密的好例子,很方便使用,可以作为平时开发的工具集,记录一下. package com.sh.springboottdemo2.util; import com.sun.org.ap ...
- des 加密解密工具类
最近在做des的双对称加密解密,特此记录一下. des对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码 ...
- 自写AES加密解密工具类
此类主要用于加密与解密,采用128位ECB模式,PKCS5Padding填充补位. 可使用方法为加密返回二进制encryptBin(content, key).加密返回十六进制encryptHex(c ...
- Java-DES算法加密解密工具类
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import ...
随机推荐
- XGBboost 特征评分的计算原理
xgboost是基于GBDT原理进行改进的算法,效率高,并且可以进行并行化运算,而且可以在训练的过程中给出各个特征的评分,从而表明每个特征对模型训练的重要性, 调用的源码就不准备详述,本文主要侧重的是 ...
- linux内核开发入门学习
1. 目录结构 内核源代码下载 https://www.kernel.org arch目录 arch是architecture的缩写. 内核所支持的每种CPU体系,在该目录下都有对应的子目录.每个CP ...
- Android AVD启动报错: This AVD's configuration is missing a kernel file! Please ensure the file "kernel-qemu" is in the same location as your system image.
启动Android SDK manager重新下载安装
- js中的 Date对象 在 IOS 手机中的兼容性问题
项目中有个时间相关的需求,很自然的用到了 js 中的 new Date() 获取时间,浏览器使用模拟手机模式访问没有问题,但是真机测试时发现,ios系统的手机无法显示时间. 定位问题发现是 new D ...
- smbclient匿名访问win7共享文件夹
windows: 首先需要开启Guest用户,设置密码为空. 然后需要在管理工具下的本地安全策略中检查本地策略\用户权限分配\拒绝从网络访问这台计算机如果有Guest或Guests则删掉. 然后正常共 ...
- L1-Day9
1.学习让我感觉很棒.(什么关系?动作 or 描述?主语部分是?) [我的翻译]Learning makes me that feel good. [标准答案]Lear ...
- ubuntu 下开机启动项修复(进不去windows系统)
1.终端输入: sudo gedit /etc/default/grub 2.更改: GRUB_DEFAULT=0 改为 GRUB_DEFAULT=4 GRUB_TIMEOUT=10 改为 ...
- Mac安装SecureCRT
8.3.1版本 链接:https://pan.baidu.com/s/1ohHunH_OVewF4QuRUzmChQ 密码:mc77 下载解压后直接是.app 直接打开会提示文件损害 打开终端,输入如 ...
- C++11 double转化为string
C++11转化double为string是一件很容易的事情. 方法: 1:使用C中的sprintf函数,这里就不说了. 2:使用std::ostringstream.这个与std::cout是一样的. ...
- 20175226 2018-2019-2《java程序设计》结对编程-四则运算(第二周-阶段总结)
需求分析(描述自己对需求的理解,以及后续扩展的可能性) 实现一个命令行程序,要求: 自动生成小学四则运算题目(加,减,乘,除) 支持整数 支持多运算符(比如生成包含100个运算符的题目) 支持真分数 ...