JAVA动态加密方法其一

package net.jeeshop.core.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 文本加密/解密 (可以把正常文本加密为类似"3E24D795C1E8BAC38986E4D1B4DDB2B4C081E485334532334532"的密文)
 * 
 */
public class StringEncryptionUtils {
	
	public static void main(String[] args){
		String data="这里是要加密的内容123456";
		String pswd="123456hijk";
		String Ret=Encrypt(data,pswd);
		System.out.println("第一次加密结果:"+Ret);
		Ret=Decrypt(Ret,pswd);
		System.out.println("第一次解密结果:"+Ret);
		
		Ret=Encrypt(data,pswd);
		System.out.println("第二次加密结果:"+Ret);
		Ret=Decrypt(Ret,pswd);
		System.out.println("第二次解密结果:"+Ret);
	}
	/**
	 * String 加密
	 * 
	 * @param (String 数据,String 密码)
	 * @return String 加密后的String
	 */
	public static String Encrypt(String Data,String Password) {
		Random rand = new Random();
		int ra=rand.nextInt();
		int rb=rand.nextInt();
		String mod= Integer.toHexString( ra^rb).toUpperCase();
		
		mod=mod+"00000";
		mod=mod.substring(0, 4);
		
		int aLen=Data.getBytes().length;
		int bLen=Password.getBytes().length;
		int clen=mod.getBytes().length;
		String result="";
		String temp="";

		for(int i=0,j=0,k=0;i<aLen;i++){
			int a=Data.getBytes()[i];
			int b=Password.codePointAt(j);
			int c=mod.codePointAt(k);
			temp = Integer.toHexString(a^b^c).toUpperCase();
			temp="00000"+temp;
			temp=temp.substring(temp.length()-2, temp.length());
			result=result+temp;
			j+=1;
			k+=1;
			if(j+1==bLen)j=0;
			if(k+1==clen)k=0;
		}

		return mod+result;
	}
	/**
	 * String 解密
	 * 
	 * @param (String 已加密的数据,String 密码)
	 * @return String 解密出来的String
	 */
	public static String Decrypt(String Data,String Password) {
		if(Data.length()<4)return Data;
		String resultString="";
		String mod="";
		
		mod=Data.substring(0, 4);
		Data=Data.substring(4);
		int aLen=Data.length();
		int bLen=Password.length();
		int cLen=mod.length();
		int j=0;
		int k=0;
		byte[] data=new byte[aLen/2];
		for(int i=0;i<aLen;i+=2){
		    data[i/2]=(byte) (HexToFirstInt( Data.substring(i, i+2))^Password.codePointAt(j)^mod.codePointAt(k));
		    
		    j = j + 1;
		    k = k + 1;
		    if (j == bLen-1)j = 0;
		    if (k == cLen-1)k = 0;
		}
		resultString = new String(data);
		return resultString;
	}
	/**
	 * Int 十六进制文本返回第一个Byte
	 * 
	 * @param (String 十六进制文本)
	 * @return Int 第一个Byte
	 */
	private static int HexToFirstInt(String HexStr){
		if(HexStr.length()==0){
			HexStr="0";
		}
		if(HexStr.length()%2==1){
			HexStr="0"+HexStr;
		}
		int tempa=0;
		int tempb=0;
		byte[] bytes=HexStr.getBytes();
		int i=0;
			if(bytes[i]<58){
				tempa=bytes[i]-48;
			}else{
				tempa=bytes[i]-55;
			}
			if(bytes[i+1]<58){
				tempb=bytes[i+1]-48;
			}else{
				tempb=bytes[i+1]-55;
			}
		return ((tempa*16)+tempb);
	}
	/**
	 * List<String> 把String解密成List<String>
	 * 
	 * @param (String 已加密数据)
	 * @return List<String> 数组
	 */
	public static List<String> Trans(String Data){
		String tempda=Decrypt(Data, GetDefaultPass());
		String[] tempS = tempda.split("\\|\\|");
		List<String> ret= new ArrayList<String>();
		for(int i=0;i<tempS.length;i++){
			ret.add(Decrypt(tempS[i], GetDefaultPass()) );
		}
		return ret;
	}
	/**
	 * String 把List<String>加密成String
	 * 
	 * @param (List<String> 数组)
	 * @return String 已加密数据
	 */
	public static String Trans(List<String> Data){
		if(Data.size()<=0){
			return "";
		}else{
			String ret = Encrypt(Data.get(0), GetDefaultPass());
			for(int i=1;i<Data.size();i++){
				ret=ret+"||"+Encrypt(Data.get(i), GetDefaultPass());
			}
			return Encrypt(ret, GetDefaultPass());
		}
	}
	public static String GetDefaultPass(){
		return "123456";
	}
}

测试参数:

        String data="这里是要加密的内容123456";
        String pswd="123456hijk";

测试结果:

第一次加密结果:3E24D795C1E8BAC38986E4D1B4DDB2B4C081E485334532334532
第一次解密结果:这里是要加密的内容123456
第二次加密结果:18B9D5E8B1EAC7B38BFB94D3C9ADB0C9B08399F5313842313842
第二次解密结果:这里是要加密的内容123456


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java提供了很多密码加密方法,下面我介绍一下常用的几种: 1. MD5加密 MD5是一种哈希算法,它可以将任意长度的数据通过一定的运算,变成一个128位的小数据块。Java中可以使用java.security.MessageDigest类来实现MD5加密。 示例代码: ``` public static String md5(String input) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] inputBytes = input.getBytes(); byte[] hashBytes = md.digest(inputBytes); return bytesToHex(hashBytes); } public static String bytesToHex(byte[] bytes) { StringBuilder hexString = new StringBuilder(); for (byte b : bytes) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { hexString.append('0'); } hexString.append(hex); } return hexString.toString(); } ``` 2. SHA加密 SHA是一种安全哈希算法,它可以将任意长度的数据通过一定的运算,变成一个160位的小数据块。Java中可以使用java.security.MessageDigest类来实现SHA加密。 示例代码: ``` public static String sha(String input) throws Exception { MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] inputBytes = input.getBytes(); byte[] hashBytes = md.digest(inputBytes); return bytesToHex(hashBytes); } ``` 3. BCrypt加密 BCrypt是一种密码哈希算法,它使用salt(盐)和cost(代价因子)对密码进行加密Java中可以使用BCrypt库实现BCrypt加密。 示例代码: ``` String password = "123456"; String hashed = BCrypt.hashpw(password, BCrypt.gensalt(12)); ``` 以上是常用的几种密码加密方法,可以根据实际需求选择使用。需要注意的是,密码加密只是增加了破解密码的难度,但并不能完全防止密码泄露。因此,在设计系统安全策略时,应该综合考虑多种安全措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值