1. 程式人生 > >sha1與md5的加密

sha1與md5的加密

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)));
	}
}

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;
	}
}

解密

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)) {
			//設定快取資訊
}