ÎļþÃû´Ó src/java/org/apereo/cas/web/landtool/utils/CoderUtils.java ÐÞ¸Ä |
| | |
| | | import javax.crypto.SecretKey; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | |
| | | |
| | | //import sun.misc.BASE64Decoder; |
| | | //import sun.misc.BASE64Encoder; |
| | | |
| | | /** |
| | | * åºç¡å å¯ç»ä»¶ |
| | | * @author TanBin |
| | | */ |
| | | public abstract class CoderUtils { |
| | | public abstract class AbstractCoderUtils { |
| | | public static final String KEY_SHA = "SHA"; |
| | | public static final String KEY_MD5 = "MD5"; |
| | | |
| | |
| | | * @param key |
| | | * @return |
| | | * @throws Exception |
| | | * |
| | | * æ³1ï¼ä½¿ç¨sun.miscå¥ä»¶ã1.6ä¹åï¼ æçä¸å¥½ï¼æ°JDK䏿¯æ |
| | | * return (new BASE64Encoder()).encodeBuffer(data); |
| | | * |
| | | * æ³2ï¼ä½¿ç¨Apache Commons Codecã |
| | | * return (new Base64()).encodeToString(data); |
| | | * |
| | | * æ³3ï¼ä½¿ç¨Java 8çjava.utilå¥ä»¶ã æçæ¯1ç11åï¼æ¯2ç3å以ä¸ã |
| | | */ |
| | | public static String encryptBASE64(byte[] data) { |
| | | public static String encryptBase64(byte[] data) { |
| | | //text.getBytes("UTF-8"); new String(decoder.decode(encodedText), "UTF-8") |
| | | //æ³1ï¼ä½¿ç¨sun.miscå¥ä»¶ã1.6ä¹åï¼ æçä¸å¥½ï¼æ°JDK䏿¯æ |
| | | // return (new BASE64Encoder()).encodeBuffer(data); |
| | | |
| | | //æ³2ï¼ä½¿ç¨Apache Commons Codecã |
| | | // return (new Base64()).encodeToString(data); |
| | | |
| | | //æ³3ï¼ä½¿ç¨Java 8çjava.utilå¥ä»¶ã æçæ¯1ç11åï¼æ¯2ç3å以ä¸ã |
| | | return Base64.getEncoder().encodeToString(data); |
| | | } |
| | | |
| | |
| | | * @param key |
| | | * @return |
| | | * @throws Exception |
| | | * |
| | | * æ³1ï¼ä½¿ç¨sun.miscå¥ä»¶ã æçä¸å¥½ï¼æ°JDK䏿¯æ |
| | | * return (new BASE64Decoder()).decodeBuffer(key); |
| | | * |
| | | * æ³2ï¼ä½¿ç¨Apache Commons Codecã |
| | | * return (new Base64()).decode(key); |
| | | * |
| | | * æ³3ï¼ä½¿ç¨Java 8çjava.utilå¥ä»¶ã æçæ¯1ç11åï¼æ¯2ç3å以ä¸ã |
| | | */ |
| | | public static byte[] decryptBASE64(String key) { |
| | | //æ³1ï¼ä½¿ç¨sun.miscå¥ä»¶ã æçä¸å¥½ï¼æ°JDK䏿¯æ |
| | | // return (new BASE64Decoder()).decodeBuffer(key); |
| | | |
| | | //æ³2ï¼ä½¿ç¨Apache Commons Codecã |
| | | // return (new Base64()).decode(key); |
| | | |
| | | //æ³3ï¼ä½¿ç¨Java 8çjava.utilå¥ä»¶ã æçæ¯1ç11åï¼æ¯2ç3å以ä¸ã |
| | | public static byte[] decryptBase64(String key) { |
| | | return Base64.getDecoder().decode(key); |
| | | } |
| | | |
| | |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static byte[] encryptMD5(byte[] data) throws Exception{ |
| | | public static byte[] encryptMd5(byte[] data) throws Exception{ |
| | | MessageDigest md5 = MessageDigest.getInstance(KEY_MD5); |
| | | md5.update(data); |
| | | return md5.digest(); |
| | |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static String lantuEncryptMD5(String text) throws Exception{ |
| | | byte[] b = encryptMD5(text.getBytes("UTF-8")); |
| | | public static String lantuEncryptMd5(String text) throws Exception{ |
| | | byte[] b = encryptMd5(text.getBytes("UTF-8")); |
| | | String key = convertToHexString(b); |
| | | //转为大å |
| | | key = key.toUpperCase(); |
| | |
| | | * @param data |
| | | * @return |
| | | */ |
| | | private static String convertToHexString(byte data[]) { |
| | | private static String convertToHexString(byte[] data) { |
| | | StringBuffer strBuffer = new StringBuffer(); |
| | | for (int i = 0; i < data.length; i++) { |
| | | //strBuffer.append(Integer.toHexString(0xff & data[i])); //30bit |
| | |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static byte[] encryptSHA(byte[] data) throws Exception{ |
| | | public static byte[] encryptSha(byte[] data) throws Exception{ |
| | | MessageDigest sha = MessageDigest.getInstance(KEY_SHA); |
| | | sha.update(data); |
| | | return sha.digest(); |
| | |
| | | public static String initMacKey() throws Exception{ |
| | | KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC); |
| | | SecretKey secretKey = keyGenerator.generateKey(); |
| | | return encryptBASE64(secretKey.getEncoded()); |
| | | return encryptBase64(secretKey.getEncoded()); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static byte[] encryptHMAC(byte[] data, String key) throws Exception{ |
| | | SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC); |
| | | public static byte[] encryptHmac(byte[] data, String key) throws Exception{ |
| | | SecretKey secretKey = new SecretKeySpec(decryptBase64(key), KEY_MAC); |
| | | Mac mac = Mac.getInstance(secretKey.getAlgorithm()); |
| | | mac.init(secretKey); |
| | | return mac.doFinal(data); |
| | |
| | | String inputStr = "123456"; |
| | | System.err.println("åæ:" + inputStr); |
| | | try { |
| | | String codeBASE64 = encryptBASE64(inputStr.getBytes("UTF-8")); |
| | | String codeMD5 = lantuEncryptMD5(inputStr); |
| | | byte[] codeSHA = encryptSHA(inputStr.getBytes("UTF-8")); |
| | | String codeBase64 = encryptBase64(inputStr.getBytes("UTF-8")); |
| | | String codeMd5 = lantuEncryptMd5(inputStr); |
| | | byte[] codeSha = encryptSha(inputStr.getBytes("UTF-8")); |
| | | |
| | | String key = initMacKey(); |
| | | System.err.println("Macå¯é¥:" + key); |
| | | byte[] codeHMAC = encryptHMAC(inputStr.getBytes("UTF-8"), key); |
| | | byte[] codeHmac = encryptHmac(inputStr.getBytes("UTF-8"), key); |
| | | |
| | | System.err.println("\nBASE64:" + codeBASE64); |
| | | System.err.println("BASE64è§£å¯:" + new String(decryptBASE64(codeBASE64),"UTF-8")); |
| | | System.err.println("MD5:" + codeMD5); |
| | | System.err.println("SHA:" + codeSHA); |
| | | System.err.println("HMAC:" + codeHMAC); |
| | | System.err.println("\nBASE64:" + codeBase64); |
| | | System.err.println("BASE64è§£å¯:" + new String(decryptBase64(codeBase64),"UTF-8")); |
| | | System.err.println("MD5:" + codeMd5); |
| | | System.err.println("SHA:" + codeSha); |
| | | System.err.println("HMAC:" + codeHmac); |
| | | |
| | | } catch (Exception e) { |
| | | // TODO Auto-generated catch block |