var get = function(a) {
var b = CryptoJS.MD5('contentWindowHig');
var c = CryptoJS.enc.Utf8.parse(b);
var d = CryptoJS.enc.Utf8.parse('contentDocuments');
var e = CryptoJS.AES.encrypt(a, c, {
iv: d,
mode: CryptoJS.mode.CBC
pad: CryptoJS.pad.ZeroPadding
});
return e.toString()
}
}
输入明文:8d3fj5T7lPMjMfMTyzdbl6Aq95zSUrmuChBFf / ex3lF23jHweq9ABJmEbd7 + 4z5DmjmhvTVyXWoeoFWCv6xCPFw7CAx7n9RZ9x9fOVrY3Cy + Nm1TAThsjgOjjK + M1S3XIim39NiRp55ai安培/分米2 + E + NzRGPN0wW1bc9Q
js输出数据:
一个:8d3fj5T7lPMjMfMTyzdbl6Aq95zSUrmuChBFf / ex3lF23jHweq9ABJmEbd7 + 4z5DmjmhvTVyXWoeoFWCv6xCPFw7CAx7n9RZ9x9fOVrY3Cy + Nm1TAThsjgOjjK + M1S3XIim39NiRp55ai安培/分米2 + E + NzRGPN0wW1bc9Q
b:a165f267f74aba5ded7563ebae2c5ac0
c:6131363566323637663734616261356465643735363365626165326335616330
d:636f6e74656e74446f63756d656e7473
E(输出):YzYv1oDPuHMyvtt8dgHUOdKaTev3rDtDXu9O%2BBlnCt4X118BroqK7nDjc%2Bo6cb6aoik6KAvIhZwENjkmrMvF26xhhWATh0TrlK0ZWPotNWI9mXWW1FgZtRrFkQGn6%2F4BWY8D%2BAfLxePup4rAw%2BekHjpkFsSZcY6rlL37uUKU%2F6OTdN4rrjG3FOEmN8yDZ9sMNAXu%2FdmAqgFRJ77gvWjgKZEXvhqYqaH1Ukb9eZYMRAE%3D
java输出数据:
b:a165f267f74aba5ded7563ebae2c5ac0
c:6131363566323637663734616261356465643735363365626165326335616330
d:636F6E74656E74446F63756D656E7473
enc_data:fPkNBugoCDHtBLO4tv9VcfGNCWxbqdWryrOWPXrsqN2I0IzNfTN + Smh5PQX5fKsGuHtGWFv7Lryh jEgOSUJNskH / JD1agfWfzDClSWA7eF4bpKmTg3ch8mrHHI7nwDm4t6jQEeCfDGcQ3Sa8s0UPipOR wcOTdGVl7RmgJ5gIERQ45m34fm8tveMYrXyTgvDKbvqO7toqshcg6IOxNgB5OCr / DH4xQibnYPnk 0Z4EBy8 =
encoded_str:fPkNBugoCDHtBLO4tv9VcfGNCWxbqdWryrOWPXrsqN2I0IzNfTN%2BSmh5PQX5fKsGuHtGWFv7Lryh%0D%0AjEgOSUJNskH%2FJD1agfWfzDClSWA7eF4bpKmTg3ch8mrHHI7nwDm4t6jQEeCfDGcQ3Sa8s0UPipOR%0D%0AwcOTdGVl7RmgJ5gIERQ45m34fm8tveMYrXyTgvDKbvqO7toqshcg6IOxNgB5OCr%2FDH4xQibnYPnk%0D%0A0Z4EBy8%3D
import com.pplive.common.util.LogUtil;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Encoder;
public static void main(String[] args) {
// key: 8d3fj5T7lPMjMfMTyzdbl6Aq95zSUrmuChBFf/ex3lF23jHweq9ABJmEbd7+4z5DmjmhvTVyXWoeoFWCv6xCPFw7CAx7n9RZ9x9fOVrY3Cy+Nm1TAThsjgOjjK+M1S3XIim39NiRp55ai/dm2+E+NzRGPN0wW1bc9Q
// b: a165f267f74aba5ded7563ebae2c5ac0
// c: 6131363566323637663734616261356465643735363365626165326335616330
// d: 636f6e74656e74446f63756d656e7473
//ss: YzYv1oDPuHMyvtt8dgHUOdKaTev3rDtDXu9O%2BBlnCt4X118BroqK7nDjc%2Bo6cb6aoik6KAvIhZwENjkmrMvF26xhhWATh0TrlK0ZWPotNWI9mXWW1FgZtRrFkQGn6%2F4BWY8D%2BAfLxePup4rAw%2BekHjpkFsSZcY6rlL37uUKU%2F6OTdN4rrjG3FOEmN8yDZ9sMNAXu%2FdmAqgFRJ77gvWjgKZEXvhqYqaH1Ukb9eZYMRAE%3D
//int end = html.indexOf("\")", start);
//String plaintext = html.substring(start + "encodeURIComponent(get(\"".length(), end);
String plaintext = "8d3fj5T7lPMjMfMTyzdbl6Aq95zSUrmuChBFf/ex3lF23jHweq9ABJmEbd7+4z5DmjmhvTVyXWoeoFWCv6xCPFw7CAx7n9RZ9x9fOVrY3Cy+Nm1TAThsjgOjjK+M1S3XIim39NiRp55ai/dm2+E+NzRGPN0wW1bc9Q";
System.out.println("plaintext: " + plaintext);
try {
String b = EncoderByMd5("contentWindowHig");
System.out.println("b: " + b);
String c = byteToHexString(b.getBytes("utf-8"));
System.out.println("c: " + c);
String d = byteToHexString("contentDocuments".getBytes("utf-8"));
System.out.println("d: " + d);
String enc_data = AES_CBC_Encrypt_nopadding(
plaintext.getBytes("utf-8"),
b.getBytes(),
"contentDocuments".getBytes());
System.out.println("enc_data: " + enc_data);
String encoded_str = URLEncoder.encode(enc_data, "utf-8");
System.out.println("encoded_str: " + encoded_str);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
public static String AES_CBC_Encrypt_nopadding(byte[] content, byte[] keyBytes, byte[] iv){
try{
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128, new SecureRandom(keyBytes));
SecretKey key = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
// process plain text
int plaintextLength = content.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(content, 0, plaintext, 0, content.length);
for (int i=content.length;i<plaintextLength;i++) {
plaintext[i] = (byte)0;
}
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
byte[] encrypted = cipher.doFinal(plaintext);
BASE64Encoder base64en = new BASE64Encoder();
return base64en.encode(encrypted);
}catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("exception:"+e.toString());
}
return null;
}
public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md5=MessageDigest.getInstance("MD5");
md5.update(str.getBytes());
return new BigInteger(1, md5.digest()).toString(16);
}
public static String AES_CBC_Decrypt(byte[] content, byte[] keyBytes, byte[] iv){
try{
KeyGenerator keyGenerator=KeyGenerator.getInstance("AES");
keyGenerator.init(128, new SecureRandom(keyBytes));//key长可设为128,192,256位,这里只能设为128
SecretKey key=keyGenerator.generateKey();
Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
byte[] result = cipher.doFinal(content);
BASE64Encoder base64en = new BASE64Encoder();
return base64en.encode(result);
}catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("exception:"+e.toString());
}
return null;
}
//Converting a string of hex character to bytes
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2){
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
public static String byteToHexString(byte[] bytes) {
StringBuffer sb = new StringBuffer(bytes.length);
String sTemp;
for (int i = 0; i < bytes.length; i++) {
sTemp = Integer.toHexString(0xFF & bytes[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
答案 0 :(得分:0)
太简单了。
js转换为Java
// var b = CryptoJS.MD5('contentWindowHig');
String b = org.apache.commons.codec.digest.DigestUtils.md5Hex("contentWindowHig");
// var c = CryptoJS.enc.Utf8.parse(b);
byte[] c = b.getByte("UTF-8");
// var d = CryptoJS.enc.Utf8.parse('contentDocuments');
byte[] d = "contentDocuments".getByte("UTF-8");
/* var e = CryptoJS.AES.encrypt(a, c, {
iv: d,
mode: CryptoJS.mode.CBC
pad: CryptoJS.pad.ZeroPadding
});
return e.toString()
*/
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(c, "AES"), new IvParameterSpec(d));
Base64.getEncoder().encodeToString(cipher.doFinal(a.getBytes("utf-8")));
a是加密str