import com.alibaba.fastjson.JSONObject; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; public class BlurayUtil { /** * * @param sSrc json字符串 * @param sKey 秘钥 * @return 返回加密后的字符串 * @throws Exception */ public static String Encrypt(String sSrc, String sKey) throws Exception { if (sKey == null) { return null; } // 判断Key是否为16位 if (sKey.length() != 16) { return null; } byte[] raw = sKey.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式" cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); return new org.apache.commons.codec.binary.Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。 }
}