/** URL解码 (使用默认字符集) */ public final static String urlDecode(String url) { return urlDecode(url, null); }
/** AES 解密,并使用默认字符集生成解密后的字符串 */ public final static String aesDecrypt(String content, String password) { return aesDecrypt(content, null, password); }
/** 使用默认字符集对字符串编码后再进行 AES 加密 */ public final static String aesEncrypt(String content, String password) { return aesEncrypt(content, null, password); }
/** 使用指定字符集对字符串编码后再进行 SHA 加密 */ public final static String sha(String input, String charset) { return encode(getShaDigest(), input, charset); }
/** MD5 加密 */ public final static byte[] md5(byte[] input) { MessageDigest algorithm = getMd5Digest(); return encode(algorithm, input); }
/** 使用指定字符集对字符串编码后再进行 SHA-{X} 加密,字符串的编码由 charset 参数指定 */ public final static String encode(MessageDigest algorithm, String input, String charset) { try { byte[] bytes = input.getBytes(safeCharset(charset)); byte[] output = encode(algorithm, bytes); return bytes2HexStr(output); } catch(UnsupportedEncodingException e) { throw new RuntimeException(e); } }
/** * 加密字符串 * * @param method :加密方法(AES、DES) * @param keysize :密匙长度 * @param content :要加密的内容 * @param charset :加密内容的编码字符集 * @param password :密码 * @return :加解密结果 * @throws GeneralSecurityException 加密失败抛出异常 */ public final static String encrypt(String method, int keysize, String content, String charset, String password) { try { byte[] bytes = content.getBytes(safeCharset(charset)); byte[] output = crypt(method, ENCRYPT_MODE, keysize, bytes, password); return bytes2HexStr(output); } catch(UnsupportedEncodingException e) { throw new RuntimeException(e); } }
/** * 解密字符串 * * @param method :解密方法(AES、DES) * @param keysize :密匙长度 * @param content :要解密的内容 * @param charset :解密结果的编码字符集 * @param password :密码 * @return :加解密结果 * @throws GeneralSecurityException 解密失败抛出异常 */ public final static String decrypt(String method, int keysize, String content, String charset, String password) { try { byte[] bytes = hexStr2Bytes(content); byte[] output = crypt(method, DECRYPT_MODE, keysize, bytes, password); return new String(output, safeCharset(charset)); } catch(UnsupportedEncodingException e) { throw new RuntimeException(e); } }
/** AES 解密 */ public final static byte[] aesDecrypt(byte[] content, String password) { return crypt(AES, DECRYPT_MODE, AES_KEY_SIZE, content, password); }
/** AES 解密,并使用指定字符集生成解密后的字符串,字符串的编码由 charset 参数指定 */ public final static String aesDecrypt(String content, String charset, String password) { return decrypt(AES, AES_KEY_SIZE, content, charset, password); }
/** byte -> 十六进制双字符 (小写) */ public final static char[] byte2Hex(byte b) { return byte2Hex(b, false); }
/** 使用指定算法对字符串加密 */ public final static String encode(MessageDigest algorithm, String input) { return encode(algorithm, input, null); }
/** DES 解密,并使用默认字符集生成解密后的字符串 */ public final static String desDecrypt(String content, String password) { return desDecrypt(content, null, password); }
/** byte[] -> 十六进制字符串 (小写) */ public final static String bytes2HexStr(byte[] bytes) { return bytes2HexStr(bytes, false); }
/** 使用默认字符集对字符串编码后再进行 DES 加密 */ public final static String desEncrypt(String content, String password) { return desEncrypt(content, null, password); }
/** 使用指定字符集对字符串编码后再进行 SHA-{X} 加密,其中 {X} 由 version 参数指定 */ public final static String sha(String input, String charset, int version) { return encode(getShaDigest(version), input, charset); }
/** 使用指定字符集对字符串编码后再进行 MD5 加密 */ public final static String md5(String input, String charset) { return encode(getMd5Digest(), input, charset); }
/** DES 加密 */ public final static byte[] desEncrypt(byte[] content, String password) { return crypt(DES, ENCRYPT_MODE, DES_KEY_SIZE, content, password); }
/** DES 解密,并使用指定字符集生成解密后的字符串,字符串的编码由 charset 参数指定 */ public final static String desDecrypt(String content, String charset, String password) { return decrypt(DES, DES_KEY_SIZE, content, charset, password); }
/** byte[] -> 十六进制字符串 */ public final static String bytes2HexStr(byte[] bytes, boolean capital) { StringBuilder sb = new StringBuilder(); for(byte b : bytes) sb.append(byte2Hex(b, capital)); return sb.toString(); }