JAVA下wordpress加密

这个加密找了很久,都只有PHP版本的,同事给了我一个远古的C#版本的,写的太晦涩难懂,而且还有错的,不过最后好歹弄出来的。上代码

//wordpress 加密
	public String WordpressEncrypt(String str,String salt){
		MessageDigest md;
		try {
			md = MessageDigest.getInstance("MD5");
			byte[] hash = md.digest((salt + str).getBytes());
			byte[] palin = str.getBytes();
			for(int i = 0;i < 8192;i++){
				byte[] newplain = new byte[hash.length + palin.length];
				System.arraycopy(hash, 0, newplain, 0, hash.length);
				System.arraycopy(palin, 0, newplain, hash.length, palin.length);
				//MD5加密
				MessageDigest md5 = MessageDigest.getInstance("MD5");
				hash = md5.digest(newplain);
			}
			int[] x = new int[hash.length];
			for(int i = 0;i < hash.length;i++){
				x[i] = hash[i] & 0xff;
			}
		//	System.out.println(re);
		//	return re;
			return "$P$B" + salt + encode64(x, 16);
		//	return String.valueOf(hash.length);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "fail";	
		}
}
	
	
	private String encode64(int[] input, int number){
        String hash = "";
        int output = 0;
        int[] input_2 = new int[number];
        for (int i = 0; i < number; i++)
        {
            input_2[i] = input[i];
            //text_2.Text += "'" + input_2[i] + "'" ; 
        }
         String itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
         int output_2 = 0;
         int len_2 = 0;
         int value_2 = 0;
         for (int i = 0; i  0)
                 {
                     output = (output >> 6);//右移6位
                 }
                 else
                 {
                     output = 0;
                 }
                 value = output;
                 hash += itoa64.substring((value % 64 + 64)%64, (value % 64 + 64)%64 + 1);
             }
             else
             {
                 break;
             }

             if (i + 1 < number)
             {
                 value = input_2[++i];
                 output_2 = (value << 16);//左移16位
                 output = value_2 + output_2;
                 value_2 = output;
                 len_2 = Integer.toBinaryString(output).length();
                 output_2 = output;
                 output = (output >> 12);//右移12位
                 value = output;//
                 hash += itoa64.substring((value % 64 + 64)%64, (value % 64 + 64)%64 + 1);
             }
             else
             {
                 break;
             }
             if (i+1< number)
             {
                 len_2 = Integer.toBinaryString(output_2).length();
                 output = (output_2 >> 18);//右移18位
                 value = output;//
                 hash += itoa64.substring((value % 64 + 64)%64, (value % 64 + 64)%64 + 1);
             }
         }
        return hash; //*/	
    }



你可能感兴趣的:(JAVA)