|
|
package com.monitor.other.dns;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
import org.apache.commons.codec.binary.Hex;
|
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
import sun.misc.BASE64Decoder;
|
|
|
|
|
|
import javax.crypto.BadPaddingException;
|
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
|
import javax.crypto.NoSuchPaddingException;
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
import java.io.IOException;
|
|
|
import java.security.*;
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/7/29.
|
|
|
*/
|
|
|
public class AESTest {
|
|
|
|
|
|
public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, IOException, InvalidAlgorithmParameterException, NoSuchProviderException {
|
|
|
String password = "yoho9646YOHO9646";
|
|
|
byte[] digest = password.getBytes("UTF-8");
|
|
|
// 初始化
|
|
|
Security.addProvider(new BouncyCastleProvider());
|
|
|
Key aesKey = new SecretKeySpec(digest, "AES");
|
|
|
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
|
|
|
IvParameterSpec iv = new IvParameterSpec("YOHO9646yoho9646".getBytes("UTF-8"));
|
|
|
//
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, aesKey, iv);
|
|
|
byte[] bytes2 = cipher.doFinal("uid=1000&domain=api.yoho.cn".getBytes("utf-8"));
|
|
|
|
|
|
System.out.println(Base64.encodeBase64String(bytes2));
|
|
|
|
|
|
BASE64Decoder decoder = new BASE64Decoder();
|
|
|
|
|
|
byte[] bytes3 = decoder.decodeBuffer("n5+xSdOrP5MyhX37SqOxmzyif3+/GbmuQMCrsKIZwRJ+OED/aYjRA//Cz0/IyxnuUCcD8lxb/iFiReJHldMrxbo3bwzTs/tclhEBSuubqv8qKIrpNTh2W9+ac27djDHeLjifmVL7WgI4VNWvHw6vhQ5Bpli4GohmuIeFx/7pTMT9AKEnDnC8Yg/AJmiHzzhKy9u8t1N5d/vHT+1WhURkbsyQDQ/Due8Ygar4+aCC32xyinrY3nbJYHjfouj6bw0kE4r9ebHkyjyeAmqUFIjycqdaMk6sHJrO7R3TDw4r8yE=");
|
|
|
byte[] bytes = decoder.decodeBuffer(Base64.encodeBase64String(bytes3));
|
|
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, aesKey, iv);
|
|
|
byte[] bytes1 = cipher.doFinal(bytes);
|
|
|
String decryptMsg = new String(bytes1, "UTF-8");
|
|
|
System.out.println(decryptMsg);
|
|
|
|
|
|
IP ip = JSON.parseObject(decryptMsg, IP.class);
|
|
|
|
|
|
MessageDigest mdInst = MessageDigest.getInstance("MD5");
|
|
|
byte[] digest1 = mdInst.digest(ip.getContent().getBytes("UTF-8"));
|
|
|
|
|
|
String s = Hex.encodeHexString(digest1);
|
|
|
System.out.println(s);
|
|
|
}
|
|
|
|
|
|
|
|
|
private static class IP {
|
|
|
private String md5;
|
|
|
|
|
|
private String content;
|
|
|
|
|
|
public String getMd5() {
|
|
|
return md5;
|
|
|
}
|
|
|
|
|
|
public void setMd5(String md5) {
|
|
|
this.md5 = md5;
|
|
|
}
|
|
|
|
|
|
public String getContent() {
|
|
|
return content;
|
|
|
}
|
|
|
|
|
|
public void setContent(String content) {
|
|
|
this.content = content;
|
|
|
}
|
|
|
}
|
|
|
} |