密码加密

EncoderUtil.java

package com.huawei.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* Created by Administrator on 2015/3/8.
* 编码工具
*/
public class EncoderUtil {
private static final String charset = "UTF-8";

/**
* MD5加密
*/
public static class MD5 {
private static final char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static String encode(String raw) {
if (raw == null || raw.length() < 0) {
return "";
}
StringBuffer result = new StringBuffer("");
try {
MessageDigest MD5 = MessageDigest.getInstance("MD5");
byte[] bytes = MD5.digest(raw.getBytes(charset));
for (byte _byte : bytes) {
result.append(hex[_byte >>> 4 & 0xf]);
result.append(hex[_byte & 0xf]);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result.toString();
}
}

/**
* SHA1加密
*/
public static class SHA1 {
public static String encode(String raw) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(raw.getBytes(charset));
byte[] bytes = digest.digest();
StringBuffer buffer = new StringBuffer();
for (byte b : bytes) {
String shaHex = Integer.toHexString(b & 0xFF);
if (shaHex.length() < 2) {
buffer.append(0);
}
buffer.append(shaHex);
}
return buffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
}

/**
* Base64加密与解密
*/
public static class BASE64 {
private final static char[] chars = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'};
public static String encode(String raw){
if (raw == null)
return "";
byte []raws = raw.getBytes(Charset.forName("UTF-8"));
int mod = raws.length % 3;
int group = (raws.length + 2) / 3;
byte []bytes = new byte[group * 3];
System.arraycopy(raws,0,bytes,0,raws.length);
StringBuilder sb = new StringBuilder();
for(int i = 0;i<group;i++){
sb.append(chars[(byte)((bytes[i*3]&0xff) >> 2)]);
sb.append(chars[(byte)((((bytes[i*3]&0xff)&0x03) << 4) | ((bytes[i*3+1]&0xff) >> 4))]);
sb.append(chars[(byte)((((bytes[i*3+1]&0xff)&0x0f) << 2) | ((bytes[i*3+2]&0xff) >> 6))]);
sb.append(chars[(byte)((bytes[i*3+2]&0xff)&0x3f)]);
}
if(mod!=0)
sb.replace(sb.length()-(2/mod),sb.length(),"==".substring(0,2/mod));
return sb.toString();
}

public static String decode(String raw){
if (raw.length() % 4 != 0)
throw new IllegalArgumentException("参数错误!");
String decode = String.valueOf(chars);
byte []raws = raw.getBytes(Charset.forName("UTF-8"));
int numExtraBytes = raw.endsWith("==") ? 2 : (raw.endsWith("=") ? 1 : 0);
int group = raws.length / 4;
byte []bytes = new byte[group * 3];
for (int i=0;i<group;i++){
bytes[i*3] = (byte)((decode.indexOf(raws[i*4]) << 2) | ((decode.indexOf(raws[i*4+1]) & 0x30) >>> 4));
bytes[i*3+1] = (byte)(((decode.indexOf(raws[i*4+1]) & 0x0f) << 4) | ((decode.indexOf(raws[i*4+2]) & 0x3c) >>> 2));
bytes[i*3+2] = (byte)(((decode.indexOf(raws[i*4+2]) & 0x03) << 6) | ((decode.indexOf(raws[i*4+3]) & 0x3f)));
}
return new String(bytes,0,bytes.length-numExtraBytes,Charset.forName("UTF-8"));
}
}
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
System.out.println(BASE64.encode("hsjhfliawehfk"));
System.out.println(BASE64.decode(BASE64.encode("就是巍峨长撒啊哈akshfah阿莱克斯京东方")));
System.out.println(SHA1.encode("HAHJKjhfliawehfk"));
System.out.println(SHA1.encode("HAHJKjhfliawehfk"));
System.out.println(SHA1.encode("hsjhfliawehfkad"));
System.out.println(SHA1.encode("hsjhfliawehfk策划活动"));
System.out.println(MD5.encode(MD5.encode(MD5.encode("wangwutest7"))));
System.out.println(MD5.encode("admin"));
}
}

原文地址:https://www.cnblogs.com/hwgok/p/5890303.html