加密算法是指通过md5工具将密码转换成16进制的字符串,一共32位长度
接下来的代码主要涉及四种加密方式:
1 自定义加密解密
2 普通md5加密
3,md5 加盐加密
4 文件加密,文件加密还是比较靠谱的,不容易被密码撞库破解
package com.daybreak.util;
import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5技术加密解密
*/
public class Md5 {
/***
* MD5加码 生成32位md5码
*/
/*public static String string2MD5(String inStr){
MessageDigest md5 = null;
try{
md5 = MessageDigest.getInstance("MD5");
}catch (Exception e){
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++){
//将数组里的数 按位 与 ff 转换成16进制
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}*/
/**
* 自定义实现的加密算法
* 加密解密算法 执行一次加密,两次解密
*/
public static String convertMD5(String inStr){
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++){
a[i] = (char) (a[i] ^ 'x');
}
String s = new String(a);
return s;
}
//加盐以后
/**
* 加密字符串普通加盐
* @param password 待加密数据
* @param salt 盐
* @return
*/
public static String textEncryptMD5(String password,String salt) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
md5.update(salt.getBytes());
byte[] bs = md5.digest(password.getBytes());
StringBuffer hexValue = new StringBuffer();
//循环将字节数组转换成16进制字符串
for (int i = 0; i < bs.length; i++) {
int val = ((int) bs[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
/*
* 还可以通过加密文件来进行加密
*
* *//**
* 加密文件,不加密
* 1.获取文件的读取流
* 2.不停的读取流中的"内容"放入字符串,放一部分就"更新"一部分.直到全部完毕
* 3.然后调用md5.digest();就会得到有内容的字节数组了.
** @param--file
* @return
* @throws Exception
*/
public static String fileEncryptMD5(File file) throws Exception {
String MD5 = "";
MessageDigest md5 = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream(file);
byte[] bytes = new byte[1024 * 5];
int len = -1;
while ((len=fis.read(bytes))!=-1) {
md5.update(bytes, 0, len);
}
fis.close();
byte[] digest = md5.digest();
for (int i = 0; i <digest.length; i++) {
int n = digest[i] & 0x000000ff;
String s = Integer.toHexString(n);
MD5 += s;
}
return MD5;
}
// 测试主函数
public static void main(String args[]) throws Exception {
String s = new String("1233");
System.out.println("原始:" + s);
//System.out.println("MD5后:" + string2MD5(s));
//加盐后 普通的字符串还是能通过被撞库破解,但是特殊意义的盐就不容易被破解 普通加盐
System.out.println("加盐后的MD5");
System.out.println(textEncryptMD5(s,"lybb"));
System.out.println("加密文件进行的加密");
// 加密文件加密
File file =new File("C:\\Users\\ly\\Desktop\\salt.txt");
System.out.println(fileEncryptMD5(file));
file =new File("C:\\Users\\ly\\Desktop\\salt2.txt");
System.out.println(fileEncryptMD5(file));
//经过测试加密文件加密的比较安全性高一点
//在线加密解密网站 https://www.cmd5.com/
System.out.println("-----自定义的加密解密");
System.out.println("加密的:" + convertMD5(s));
System.out.println("解密的:" + convertMD5(convertMD5(s)));
}
}