java中加密解密工具类
在工作中经常遇到需要加密、解密的场景。例如用户的手机号等信息,在保存到数据库的过程中,需要对数据进行加密。取出时进行解密。
public class DEStool {
private String sKey;
public DEStool() {
//默认构造函数提供默认密钥
sKey = "des@#$12";
}
public DEStool(String securityKey) {
if (securityKey.length() < ) {
throw new IllegalArgumentException("密钥长度至少8位");
}
this.sKey = securityKey;
}
private Cipher makeCipher() throws Exception{
return Cipher.getInstance("DES");
}
private SecretKey makeKeyFactory() throws Exception{
SecretKeyFactory des = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = des.generateSecret(new DESKeySpec(sKey.getBytes()));
return secretKey;
}
public String encrypt(String text) throws Exception{
Cipher cipher = makeCipher();
SecretKey secretKey = makeKeyFactory();
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return new String(Base64.encodeBase64(cipher.doFinal(text.getBytes())));
}
public String decrypt(String text) throws Exception{
Cipher cipher = makeCipher();
SecretKey secretKey = makeKeyFactory();
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.decodeBase64(text.getBytes())));
}
public static void main(String[] args) {
DEStool tool = new DEStool("nice1234");
String content = "中国";
System.out.println("原文内容:"+content);
String encrpt = null;
try {
encrpt = tool.encrypt(content);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("加密后:"+encrpt + ", 长度=" + encrpt.length());
String descript =null;
try {
descript = tool.decrypt(encrpt);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("解密后:" + descript);
}
}
java中加密解密工具类的更多相关文章
- Base64加密解密工具类
使用Apache commons codec类Base64进行加密解密 maven依赖 <dependency> <groupId>commons-codec</grou ...
- JAVA中封装JSONUtils工具类及使用
在JAVA中用json-lib-2.3-jdk15.jar包中提供了JSONObject和JSONArray基类,用于JSON的序列化和反序列化的操作.但是我们更习惯将其进一步封装,达到更好的重用. ...
- Java 中的并发工具类
Java 中的并发工具类 CountDownLatch public class JoinCountDownLatchTest { public static void main(String[] a ...
- Java AES加密解密工具 -- GUI 、在线传输文件
原理 对于任意长度的明文,AES首先对其进行分组,每组的长度为128位.分组之后将分别对每个128位的明文分组进行加密. 对于每个128位长度的明文分组的加密过程如下: (1)将128位AES ...
- java中常用的工具类(一)
我们java程序员在开发项目的是常常会用到一些工具类.今天我汇总了一下java中常用的工具方法.大家可以在项目中使用.可以收藏!加入IT江湖官方群:383126909 我们一起成长 一.String工 ...
- .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 ...
- java 加密解密工具类(实用!!!)
最近发现了一个加密解密的好例子,很方便使用,可以作为平时开发的工具集,记录一下. package com.sh.springboottdemo2.util; import com.sun.org.ap ...
随机推荐
- 【spring boot】spring boot中使用@RestController不起作用,不返回json,依旧去找访问接口的请求地址对应的页面
问题描述: spring boot中使用@RestController不起作用,不返回json,依旧去找访问接口的请求地址对应的页面 表现结果: 1>使用postman测试接口,表现为返回是40 ...
- 介紹 IIS 8 全新的 HttpPlatformHandler 模組與 ASP.NET 5 Beta8 重大變更
HttpPlatformHandler 是一個支援 IIS 8 與 IIS 8.5 的原生模組 (native module),主要使用於 Microsoft Azure Websites 網站服務中 ...
- boa cgi程序cgi_header: unable to find LFLF
ftp必须用二进制模式上传才可以 sqlite3 arm-linux-gcc hello.c -o hello.cgi -I /cgi/include -L /cgi/lib -static -lsq ...
- mac 查看cpu个数
mac 查看cpu个数 同是unix系统的mac,如何查看cpu个数及其相关信息呢?
- ssh免密登录方法不生效?Authentication refused: bad ownership or modes for directory
机器A通过ssh登录机器B,方法有两种: 1.密码方式 2.密钥方式: 两种方式同时开启的时候,优先使用密钥方式. 密钥登录方式的配置方法是,首先在登录机器B上创建文件authorized_keys( ...
- Tomcat中JVM参数设置
Tomcat本身不能直接在计算机上运行,需要依赖于硬件基础之上的操作系统和一个Java虚拟机.Tomcat的内存溢出本质就是JVM内存溢出,所以在本文开始时,应该先对JavaJVM有关内存方面的知识进 ...
- 解决Spring MVC报No converter found for return value of type:class java.util.ArrayList问题
一.背景 在搭建一套Spring+SpringMVC+Mybatis(SSM)的环境(搭建步骤会在以后博客中给出),结果运行 程序时,适用@ResponseBody注解进行返回List<对象&g ...
- 解决 PowerDesigner 错误 The generation has been cancelled because errors have been found by the check model.
在通过概念数据模型生成为物理数据模型时出现错误“The generation has been cancelled because errors have been found by the chec ...
- Mapper 与 Reducer 解析
1 . 旧版 API 的 Mapper/Reducer 解析 Mapper/Reducer 中封装了应用程序的数据处理逻辑.为了简化接口,MapReduce 要求所有存储在底层分布式文件系统上的数据均 ...
- 转: Mac 使用ADT的问题
http://blog.csdn.net/wwj_748/article/details/44806253