【JAVA - 基础】之数据加密和解密
1、Base64工具类(可逆):
import java.util.HashMap;
import java.util.Map; /**
* Base64加解密算法
* </p>
* Base64加密算法:<br/>
* 1、获取字符串中每个字符的ASCII码;<br/>
* 2、按照每3个8bit的字符为一组来分组,即每组24bit;<br/>
* 3、将这24bit划分成4个6bit的4个单位,每个单位前面添加2个0,得到4个8bit的单位;<br/>
* 4、将每个8bit的单位转换成十进制数字,对照Base64编码表找到对应的字符进行拼接,得到最终的加密后的字符串。<br/>
* </p>
* Base64解密算法:<br/>
* 1、读入4个字符,对照Base64编码表找到字符对应的索引,生成4个6为的值;<br/>
* 2、将这4个6为的值拼接起来,形成一个24为的值;<br/>
* 3、将这个24位的值按照8位一组截断成3个8位的值;<br/>
* 4、对照ASCII表找到这三个8位的值对应的字符,即解码后的字符。<br/>
* </p>
* 注意事项:<br/>
* 1、被编码的字符必须是8bit的,即必须在ASCII码范围内,中文不行;<br/>
* 2、如果被编码的字符长度不是3的倍数,则在最后添加1或2个0,对应的输出字符为“=”;
* 3、给定一个字符串,用Base64方法对其进行加密后解密,得到的结果就不是开始时候的字符串了。<br/>
*/
public class BASE64Util {
private static final Map<Integer, Character> base64CharMap = new HashMap<>();
private static final String base64CharString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; private static BASE64Util instance; private BASE64Util() {
for (int i = 0; i < base64CharString.length(); i++) {
char c = base64CharString.charAt(i);
base64CharMap.put(new Integer(i), new Character(c));
}
} public static BASE64Util getInstance() {
if (instance == null) {
synchronized (BASE64Util.class) {
if (instance == null) {
instance = new BASE64Util();
}
}
}
return instance;
} /**
* This method is used to encode a normal string to base64 string @param
* origin The String to be encoded @return The String after encoded.
*/
public String encode(String origin) {
if (origin == null) {
return null;
}
if (origin.length() == 0) {
return "";
}
int length = origin.length();
String binaryString = "";
// to binary String
for (int i = 0; i < length; i++) {
int ascii = origin.charAt(i);
String binaryCharString = Integer.toBinaryString(ascii);
while (binaryCharString.length() < 8) {
binaryCharString = "0" + binaryCharString;
}
binaryString += binaryCharString;
} // to base64 index
int beginIndex = 0;
int endIndex = beginIndex + 6;
String base64BinaryString = "";
String charString = "";
while ((base64BinaryString = binaryString.substring(beginIndex, endIndex)).length() > 0) {
// if length is less than 6, add "0".
while (base64BinaryString.length() < 6) {
base64BinaryString += "0";
}
int index = Integer.parseInt(base64BinaryString, 2);
char base64Char = base64CharMap.get(index);
charString = charString + base64Char;
beginIndex += 6;
endIndex += 6;
if (endIndex >= binaryString.length()) {
endIndex = binaryString.length();
}
if (endIndex < beginIndex) {
break;
}
}
if (length % 3 == 2) {
charString += "=";
}
if (length % 3 == 1) {
charString += "==";
}
return charString;
} public String decode(String encodedString) {
if (encodedString == null) {
return null;
}
if (encodedString.length() == 0) {
return "";
}
// get origin base64 String
String origin = encodedString.substring(0, encodedString.indexOf("="));
String equals = encodedString.substring(encodedString.indexOf("=")); String binaryString = "";
// convert base64 string to binary string
for (int i = 0; i < origin.length(); i++) {
char c = origin.charAt(i);
int ascii = base64CharString.indexOf(c);
String binaryCharString = Integer.toBinaryString(ascii);
while (binaryCharString.length() < 6) {
binaryCharString = "0" + binaryCharString;
}
binaryString += binaryCharString;
}
// the encoded string has 1 "=", means that the binary string has append
// 2 "0"
if (equals.length() == 1) {
binaryString = binaryString.substring(0, binaryString.length() - 2);
}
// the encoded string has 2 "=", means that the binary string has append
// 4 "0"
if (equals.length() == 2) {
binaryString = binaryString.substring(0, binaryString.length() - 4);
} // convert to String
String charString = "";
String resultString = "";
int beginIndex = 0;
int endIndex = beginIndex + 8;
while ((charString = binaryString.substring(beginIndex, endIndex)).length() == 8) {
int ascii = Integer.parseInt(charString, 2);
resultString += (char) ascii;
beginIndex += 8;
endIndex += 8;
if (endIndex > binaryString.length()) {
break;
}
}
return resultString;
}
}
2、MD5工具类(不可逆):
import java.security.MessageDigest; import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; public class MD5Util {
public static final String MD5 = "MD5";
public static final String HmacMD5 = "HmacMD5";
public static final String charset = null; // 编码格式;默认null为GBK private static MD5Util instance; private MD5Util() {
} // 单例
public static MD5Util getInstance() {
if (instance == null) {
synchronized (MD5Util.class) {
if (instance == null) {
instance = new MD5Util();
}
}
}
return instance;
} /**
* 使用 MD5 方法加密(无密码)
*/
public String encode(String res) {
try {
MessageDigest md = MessageDigest.getInstance(MD5);
byte[] resBytes = charset == null ? res.getBytes() : res.getBytes(charset);
return BASE64Util.getInstance().encode(md.digest(resBytes).toString());
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 使用 MD5 方法加密(可以设密码)
*/
public String encode(String res, String key) {
try {
SecretKey sk = null;
if (key == null) {
KeyGenerator kg = KeyGenerator.getInstance(HmacMD5);
sk = kg.generateKey();
} else {
byte[] keyBytes = charset == null ? key.getBytes() : key.getBytes(charset);
sk = new SecretKeySpec(keyBytes, HmacMD5);
}
Mac mac = Mac.getInstance(HmacMD5);
mac.init(sk);
byte[] result = mac.doFinal(res.getBytes());
return BASE64Util.getInstance().encode(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
3、SHA1工具类(不可逆):
import java.security.MessageDigest; import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; /**
* 使用 SHA1 方法进行加密<br/>
* SHA1方法加密是不可逆的,不能解密,要想解密就必须使用暴力解密<br/>
* <p/>
* 方法中的 res 参数:原始的数据<br/>
* 方法中的 key 参数:密钥,可以随便写<br/>
*/
public class SHA1Util {
public static final String SHA1 = "SHA1";
public static final String HmacSHA1 = "HmacSHA1";
public static final String charset = null; // 编码格式;默认null为GBK private static SHA1Util instance; private SHA1Util() {
} // 单例
public static SHA1Util getInstance() {
if (instance == null) {
synchronized (SHA1Util.class) {
if (instance == null) {
instance = new SHA1Util();
}
}
}
return instance;
} /**
* 使用 SHA1 方法加密(无密码)
*/
public String encode(String res) {
try {
MessageDigest md = MessageDigest.getInstance(SHA1);
byte[] resBytes = charset == null ? res.getBytes() : res.getBytes(charset);
return BASE64Util.getInstance().encode(md.digest(resBytes).toString());
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 使用 SHA1 方法加密(可以设密码)
*/
public String encode(String res, String key) {
try {
SecretKey sk = null;
if (key == null) {
KeyGenerator kg = KeyGenerator.getInstance(HmacSHA1);
sk = kg.generateKey();
} else {
byte[] keyBytes = charset == null ? key.getBytes() : key.getBytes(charset);
sk = new SecretKeySpec(keyBytes, HmacSHA1);
}
Mac mac = Mac.getInstance(HmacSHA1);
mac.init(sk);
byte[] result = mac.doFinal(res.getBytes());
return BASE64Util.getInstance().encode(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
4、AES工具类(可逆):
import java.security.SecureRandom; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; public class AESUtil {
public static final String AES = "AES";
public static final String charset = null; // 编码格式;默认null为GBK
public static final int keysizeAES = 128; private static AESUtil instance; private AESUtil() {
} // 单例
public static AESUtil getInstance() {
if (instance == null) {
synchronized (MD5Util.class) {
if (instance == null) {
instance = new AESUtil();
}
}
}
return instance;
} /**
* 使用 AES 进行加密
*/
public String encode(String res, String key) {
return keyGeneratorES(res, AES, key, keysizeAES, true);
} /**
* 使用 AES 进行解密
*/
public String decode(String res, String key) {
return keyGeneratorES(res, AES, key, keysizeAES, false);
} // 使用KeyGenerator双向加密,DES/AES,注意这里转化为字符串的时候是将2进制转为16进制格式的字符串,不是直接转,因为会出错
private String keyGeneratorES(String res, String algorithm, String key, int keysize, boolean isEncode) {
try {
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
if (keysize == 0) {
byte[] keyBytes = charset == null ? key.getBytes() : key.getBytes(charset);
kg.init(new SecureRandom(keyBytes));
} else if (key == null) {
kg.init(keysize);
} else {
byte[] keyBytes = charset == null ? key.getBytes() : key.getBytes(charset);
kg.init(keysize, new SecureRandom(keyBytes));
}
SecretKey sk = kg.generateKey();
SecretKeySpec sks = new SecretKeySpec(sk.getEncoded(), algorithm);
Cipher cipher = Cipher.getInstance(algorithm);
if (isEncode) {
cipher.init(Cipher.ENCRYPT_MODE, sks);
byte[] resBytes = charset == null ? res.getBytes() : res.getBytes(charset);
return parseByte2HexStr(cipher.doFinal(resBytes));
} else {
cipher.init(Cipher.DECRYPT_MODE, sks);
return new String(cipher.doFinal(parseHexStr2Byte(res)));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} // 将二进制转换成16进制
private String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} // 将16进制转换为二进制
private byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
5、DES工具类(可逆):
import java.security.SecureRandom; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; public class DESUtil {
public static final String DES = "DES";
public static final String charset = null; // 编码格式;默认null为GBK
public static final int keysizeDES = 0; private static DESUtil instance; private DESUtil() {
} // 单例
public static DESUtil getInstance() {
if (instance == null) {
synchronized (MD5Util.class) {
if (instance == null) {
instance = new DESUtil();
}
}
}
return instance;
} /**
* 使用 DES 进行加密
*/
public String encode(String res, String key) {
return keyGeneratorES(res, DES, key, keysizeDES, true);
} /**
* 使用 DES 进行解密
*/
public String decode(String res, String key) {
return keyGeneratorES(res, DES, key, keysizeDES, false);
} // 使用KeyGenerator双向加密,DES/AES,注意这里转化为字符串的时候是将2进制转为16进制格式的字符串,不是直接转,因为会出错
private String keyGeneratorES(String res, String algorithm, String key, int keysize, boolean isEncode) {
try {
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
if (keysize == 0) {
byte[] keyBytes = charset == null ? key.getBytes() : key.getBytes(charset);
kg.init(new SecureRandom(keyBytes));
} else if (key == null) {
kg.init(keysize);
} else {
byte[] keyBytes = charset == null ? key.getBytes() : key.getBytes(charset);
kg.init(keysize, new SecureRandom(keyBytes));
}
SecretKey sk = kg.generateKey();
SecretKeySpec sks = new SecretKeySpec(sk.getEncoded(), algorithm);
Cipher cipher = Cipher.getInstance(algorithm);
if (isEncode) {
cipher.init(Cipher.ENCRYPT_MODE, sks);
byte[] resBytes = charset == null ? res.getBytes() : res.getBytes(charset);
return parseByte2HexStr(cipher.doFinal(resBytes));
} else {
cipher.init(Cipher.DECRYPT_MODE, sks);
return new String(cipher.doFinal(parseHexStr2Byte(res)));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} // 将二进制转换成16进制
private String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} // 将16进制转换为二进制
private byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
6、XOR(异或加密)工具类(可逆):
public class XORUtil {
private static XORUtil instance; private XORUtil() {
} // 单例
public static XORUtil getInstance() {
if (instance == null) {
synchronized (XORUtil.class) {
if (instance == null) {
instance = new XORUtil();
}
}
}
return instance;
} /**
* 对一个数字进行异或加解密
*/
public int code(int res, String key) {
return res ^ key.hashCode();
} /**
* 异或加密
*/
public String encode(String res, String key) {
byte[] bs = res.getBytes();
for (int i = 0; i < bs.length; i++) {
bs[i] = (byte) ((bs[i]) ^ key.hashCode());
}
return parseByte2HexStr(bs);
} /**
* 异或解密
*/
public String decode(String res, String key) {
byte[] bs = parseHexStr2Byte(res);
for (int i = 0; i < bs.length; i++) {
bs[i] = (byte) ((bs[i]) ^ key.hashCode());
}
return new String(bs);
} // 将二进制转换成16进制
private String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} // 将16进制转换为二进制
private byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
7、测试:
public class Test {
public static void main(String[] args) {
int xorNum = 12345;
String res = "I am the text to be encoded and decoded.";
String key = "我是密钥key";
System.out.println("-------------------------BASE64--------------------------");
String base64_encodedStr = BASE64Util.getInstance().encode(res);
System.out.println("加密:" + base64_encodedStr);
System.out.println("解密:" + BASE64Util.getInstance().decode(base64_encodedStr));
System.out.println("-------------------------MD5--------------------------");
String md5_encodedStr = MD5Util.getInstance().encode(res);
System.out.println("无密码加密:" + md5_encodedStr);
System.out.println("有密码加密:" + MD5Util.getInstance().encode(md5_encodedStr, key));
System.out.println("-------------------------SHA1--------------------------");
String sha1_encodedStr = SHA1Util.getInstance().encode(res);
System.out.println("无密码加密:" + sha1_encodedStr);
System.out.println("有密码加密:" + SHA1Util.getInstance().encode(sha1_encodedStr, key));
System.out.println("-------------------------AES--------------------------");
String aes_encodedStr = AESUtil.getInstance().encode(res, key);
System.out.println("加密:" + aes_encodedStr);
System.out.println("解密:" + AESUtil.getInstance().decode(aes_encodedStr, key));
System.out.println("-------------------------DES--------------------------");
String des_encodedStr = DESUtil.getInstance().encode(res, key);
System.out.println("加密:" + des_encodedStr);
System.out.println("解密:" + DESUtil.getInstance().decode(des_encodedStr, key));
System.out.println("-------------------------XOR--------------------------");
String xor_encodedStr = XORUtil.getInstance().encode(res, key);
System.out.println("文本加密:" + xor_encodedStr);
System.out.println("文本解密:" + XORUtil.getInstance().decode(xor_encodedStr, key));
int xor_encodedNum = XORUtil.getInstance().code(xorNum, key);
System.out.println("数字加密:" + xor_encodedNum);
System.out.println("数字解密:" + XORUtil.getInstance().code(xor_encodedNum, key));
}
}
测试结果:
【JAVA - 基础】之数据加密和解密的更多相关文章
- 【推荐】JAVA基础◆浅谈3DES加密解密
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Java基础知识【上】(转载)
http://blog.csdn.net/silentbalanceyh/article/details/4608272 (最终还是决定重新写一份Java基础相关的内容,原来因为在写这一个章节的时候没 ...
- java中的数据加密
记录 一.java中的数据加密 Java提供的安全模型和API 加密基础知识 使用JAVA实现加密 二.Java提供的安全模型和API 2.1.Java语言本身的安全性 自动内存管理:对于生成的对象在 ...
- C# Java间进行RSA加密解密交互
原文:C# Java间进行RSA加密解密交互 这里,讲一下RSA算法加解密在C#和Java之间交互的问题,这两天纠结了很久,也看了很多其他人写的文章,颇受裨益,但没能解决我的实际问题,终于,还是被我捣 ...
- Python之数据加密与解密及相关操作(hashlib模块、hmac模块、random模块、base64模块、pycrypto模块)
本文内容 数据加密概述 Python中实现数据加密的模块简介 hashlib与hmac模块介绍 random与secrets模块介绍 base64模块介绍 pycrypto模块介绍 总结 参考文档 提 ...
- C# RSA加解密与验签,AES加解密,以及与JAVA平台的密文加解密
前言: RSA算法是利用公钥与密钥对数据进行加密验证的一种算法.一般是拿私钥对数据进行签名,公钥发给友商,将数据及签名一同发给友商,友商利用公钥对签名进行验证.也可以使用公钥对数据加密,然后用私钥对数 ...
- Java采用RSA加密及解密技术的有关Maven项目的配置流程:
第一步: 获得RSA公钥私钥(秘钥格式:PKCS#8 ,测试使用的是无私钥密码的) 公钥: -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4G ...
- Python之数据加密与解密及相关操作(hashlib、hmac、random、base64、pycrypto)
本文内容 数据加密概述 Python中实现数据加密的模块简介 hashlib与hmac模块介绍 random与secrets模块介绍 base64模块介绍 pycrypto模块介绍 总结 参考文档 提 ...
- java基础-day6
第06天 java基础语法 今日内容介绍 u Eclipse断点调试 u 基础语法的练习 第1章 Eclipse断点调试 1.1 Eclipse断点调试概述 Eclipse的断点调试可以 ...
随机推荐
- MySQL设置
在MySQL的使用中很容易出现乱码的情况. 实际上在MySQL中有个地方表明了系统中所用到的所有的字符集. 例如: 从中可以看出,对于server和database的默认字符集都是latin1,这样很 ...
- 彻底删除sql2008r2
一. SQL2008卸载. 1.从控制面板卸载 1)点击计算机右下角“开始”,点击“控制面板” 2)点击“卸载程序”. 3)在程序列表中找到“Microsoft SQL Server 2008” ...
- sql 建立数据库,表格,索引,主键
---- 数据库: `message_db`-- -- --------------------------------------------------------create database ...
- C#程序:如何创建xml文件以及xml文件的增、删、改、查
其实今天的这篇博文 ,是对请几天发表的博文的一个总结,只是想把xml文件的增删改查结合起来,这样更容易让初学的朋友理解,废话也不多说了,开始吧! 下面是我把我在vs环境下写的代码ctrl+V然后ctr ...
- php中foreach()函数与Array数组经典案例讲解
//php中foreach()函数与Array数组经典案例讲解 function getVal($v) { return $v; //可以加任意检查代码,列入要求$v必须是数字,或过滤非法字符串等.} ...
- 练习2 J题 - 多项式求和
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 多项式 ...
- URL encode 与 URL decode 的C语言实现
转载自:http://blog.csdn.net/langeldep/article/details/6264058 本文代码为从PHP代码中修改而来,只保留了2个函数. int php_url_de ...
- Spire.XLS
又一款Excel处理神器Spire.XLS,你值得拥有(二) 前言:上篇 C#组件系列——又一款Excel处理神器Spire.XLS,你值得拥有 介绍了下组件的两个功能,说不上特色,但确实能解决我 ...
- JSP面试题及答案
更新时间:2015-04-07 来源:网络 投诉删除 [看准网(Kanzhun.com)]JSP面试题频道小编搜集的范文“JSP面试题及答案”,供大家阅读参考,查看更多 ...
- 【产品体验】ONE一个
第二篇博客,加油加油~~本人产品新人,学习中,希望大家多多指教! 先来两张ONE的界面图镇楼—— ONE简介: “复杂世界里,一个就够了.”这是一款轻量级的文艺阅读应用,每日更新一张图 ...