sha1与md5的加密

import java.security.MessageDigest;

public class SHA1Util {
	//sha1加密
		public final static String getSha1(String str){
	        if(str==null||str.length()==0){
	            return null;
	        }
	        char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
	                'a','b','c','d','e','f'};
	        try {
	            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
	            mdTemp.update(str.getBytes("UTF-8"));

	            byte[] md = mdTemp.digest();
	            int j = md.length;
	            char buf[] = new char[j*2];
	            int k = 0;
	            for (int i = 0; i < j; i++) {
	                byte byte0 = md[i];
	                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
	                buf[k++] = hexDigits[byte0 & 0xf];      
	            }
	            return new String(buf);
	        } catch (Exception e) {
	            return null;
	        }
	    }
}
import java.security.MessageDigest;

public class MD5Util {
	  public final static String MD5Encrypted(String sender) {  
	        //用于加密的字符  
	        char md5String[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',  
	                'a', 'b', 'c', 'd', 'e', 'f' };  
	        try {  
	            //使用平台的默认字符集将此 String 编码为 byte序列,并将结果存储到一个新的 byte数组中  
	            byte[] btInput = sender.getBytes();  
	               
	            //信息摘要是安全的单向哈希函数,它接收任意大小的数据,并输出固定长度的哈希值。  
	            MessageDigest mdInst = MessageDigest.getInstance("MD5");  
	               
	            //MessageDigest对象通过使用 update方法处理数据, 使用指定的byte数组更新摘要  
	            mdInst.update(btInput);  
	               
	            // 摘要更新之后,通过调用digest()执行哈希计算,获得密文  
	            byte[] md = mdInst.digest();  
	               
	            // 把密文转换成十六进制的字符串形式  
	            int j = md.length;  
	            char str[] = new char[j * 2];  
	            int k = 0;  
	            for (int i = 0; i < j; i++) {   //  i = 0  
	                byte byte0 = md[i];  //95  
	                str[k++] = md5String[byte0 >>> 4 & 0xf];    //    5   
	                str[k++] = md5String[byte0 & 0xf];   //   F  
	            }  
	               
	            //返回经过加密后的字符串  
	            return new String(str);  
	               
	        } catch (Exception e) {  
	            return null;  
	        }  
	    }
	  
}
@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper,User> implements UserService{
	@Override
	public User insert(User entity) {
		User user = new User();
		user.setUserCode(entity.getUserCode());
		List<User> list = mapper.select(user);
		if(list.size() > 0){
			throw new CntenException(entity, "-1", "登陆名已存在");
		}
		String pass = encryption("123456");
		entity.setUserPass(pass);
		return super.insert(entity);
	}


	//加密
	public String encryption(String pass){
		return SHA1Util.getSha1(MD5Util.MD5Encrypted(MD5Util.MD5Encrypted(pass)));
	}
}

sha1与md5的加密_字符串

super为BaseServiceImpl<UserMapper,User>

public class BaseServiceImpl<M extends Mapper<T>, T> implements BaseService<T> {

	public T insert(T entity) {
		mapper.insert(entity);
		return entity;
	}
}

sha1与md5的加密_sha1与md5的加密_02

解密

public class AssembleJSON implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private Integer code;
	private String msg;
	private Object data;
}		


//获取用户信息
        String userpass = request.getParameter("userpass");
		AssembleJSON userByCode = frameService.findUserByCode(usercode);
		Map<String,Object> map = (Map<String, Object>) userByCode.getData();
		String password = "";
		if (map != null) {
			password = (String) map.get("userPass");
			userpass = MD5Util.MD5Encrypted(MD5Util.MD5Encrypted(userpass));
			userpass  = SHA1Util.getSha1(userpass);
		}
		if (password.equals(userpass)) {
			//设置缓存信息
}

sha1与md5的加密_sha1与md5的加密_03

sha1与md5的加密_git_04