1. 首页
  2. IT资讯

Java 开发中常用的 4 种加密方法

“u003Cpu003EJava后端,选择u003Cu002Fpu003Eu003Cpu003E技术博文,及时送达u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRelVn4tG7zucng” img_width=”535″ img_height=”10″ alt=”Java 开发中常用的 4 种加密方法” inline=”0″u003Eu003Cpu003E来源 | my.oschina.netu002Fuu002F4u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-4″u003E139u003Cu002Fiu003E951u002Fblogu002F3077236u003Cu002Fpu003Eu003Cpu003E作者 | 爱编程的浪子u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h3″u003E一、工具类u003Cu002Fh1u003Eu003Cpu003E1. md5加密工具类u003Cu002Fpu003Eu003Cpu003E2. baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003E加密工具类u003Cu002Fpu003Eu003Cpu003E3. Bcrypt工具类u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h3″u003E二、加密测试u003Cu002Fh1u003Eu003Cpu003E1. MD5加密测试u003Cu002Fpu003Eu003Cpu003E2. baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003E加密测试u003Cu002Fpu003Eu003Cpu003E3. SHA加密测试u003Cu002Fpu003Eu003Cpu003E4. BCrypt加密测试u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h3″u003E一、工具类u003Cu002Fh1u003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrong toutiao-origin=”span”u003E1. md5加密工具类u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epublic class MD5Utils {u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E private static final String hexDigIts = {“0″,”1″,”2″,”3″,”4″,”5″,”6″,”7″,”8″,”9″,”a”,”b”,”c”,”d”,”e”,”f”};u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E u002F**u003Cu002Fcodeu003Eu003Ccodeu003E * MD5加密u003Cu002Fcodeu003Eu003Ccodeu003E * @param origin 字符u003Cu002Fcodeu003Eu003Ccodeu003E * @param charsetname 编码u003Cu002Fcodeu003Eu003Ccodeu003E * @returnu003Cu002Fcodeu003Eu003Ccodeu003E *u002Fu003Cu002Fcodeu003Eu003Ccodeu003E public static String MD5Encode(String origin, String charsetname){u003Cu002Fcodeu003Eu003Ccodeu003E String resultString = ;u003Cu002Fcodeu003Eu003Ccodeu003E try{u003Cu002Fcodeu003Eu003Ccodeu003E resultString = new String(origin);u003Cu002Fcodeu003Eu003Ccodeu003E MessageDigest md = MessageDigest.getInstance(“MD5”);u003Cu002Fcodeu003Eu003Ccodeu003E if( == charsetname || “”.equals(charsetname)){u003Cu002Fcodeu003Eu003Ccodeu003E resultString = byteArrayToHexString(md.digest(resultString.getBytes));u003Cu002Fcodeu003Eu003Ccodeu003E }else{u003Cu002Fcodeu003Eu003Ccodeu003E resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E }catch (Exception e){u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E return resultString;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cbru003Eu003Cbru003Eu003Ccodeu003E public static String byteArrayToHexString(byte b[]){u003Cu002Fcodeu003Eu003Ccodeu003E StringBuffer resultSb = new StringBuffer;u003Cu002Fcodeu003Eu003Ccodeu003E for(int i = 0; i < b.length; i++){u003Cu002Fcodeu003Eu003Ccodeu003E resultSb.append(byteToHexString(b[i]));u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E return resultSb.toString;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E public static String byteToHexString(byte b){u003Cu002Fcodeu003Eu003Ccodeu003E int n = b;u003Cu002Fcodeu003Eu003Ccodeu003E if(n < 0){u003Cu002Fcodeu003Eu003Ccodeu003E n += 256;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E int d1 = n u002F 16;u003Cu002Fcodeu003Eu003Ccodeu003E int d2 = n % 16;u003Cu002Fcodeu003Eu003Ccodeu003E return hexDigIts[d1] + hexDigIts[d2];u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrong toutiao-origin=”span”u003E2. baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003E加密工具类u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epublic class Baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003EUtil {u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E u002Fu002F 字符串编码u003Cu002Fcodeu003Eu003Ccodeu003E private static final String UTF_8 = “UTF-8”;u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E u002F**u003Cu002Fcodeu003Eu003Ccodeu003E * 加密字符串u003Cu002Fcodeu003Eu003Ccodeu003E * @param inputDatau003Cu002Fcodeu003Eu003Ccodeu003E * @returnu003Cu002Fcodeu003Eu003Ccodeu003E *u002Fu003Cu002Fcodeu003Eu003Ccodeu003E public static String decodeData(String inputData) {u003Cu002Fcodeu003Eu003Ccodeu003E try {u003Cu002Fcodeu003Eu003Ccodeu003E if ( == inputData) {u003Cu002Fcodeu003Eu003Ccodeu003E return ;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E return new String(Baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003E.decodeBaseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003E(inputData.getBytes(UTF_8)), UTF_8);u003Cu002Fcodeu003Eu003Ccodeu003E } catch (UnsupportedEncodingException e) {u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E return ;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E u002F**u003Cu002Fcodeu003Eu003Ccodeu003E * 解密加密后的字符串u003Cu002Fcodeu003Eu003Ccodeu003E * @param inputDatau003Cu002Fcodeu003Eu003Ccodeu003E * @returnu003Cu002Fcodeu003Eu003Ccodeu003E *u002Fu003Cu002Fcodeu003Eu003Ccodeu003E public static String encodeData(String inputData) {u003Cu002Fcodeu003Eu003Ccodeu003E try {u003Cu002Fcodeu003Eu003Ccodeu003E if ( == inputData) {u003Cu002Fcodeu003Eu003Ccodeu003E return ;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E return new String(Baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003E.encodeBaseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003E(inputData.getBytes(UTF_8)), UTF_8);u003Cu002Fcodeu003Eu003Ccodeu003E } catch (UnsupportedEncodingException e) {u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E return ;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E public static void main(String[] args) {u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(Baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003EUtil.encodeData(“我是中文”));u003Cu002Fcodeu003Eu003Ccodeu003E String enStr = Baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003EUtil.encodeData(“我是中文”);u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(Baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003EUtil.decodeData(enStr));u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrong toutiao-origin=”span”u003E3. Bcrypt工具类u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epublic class BcryptCipher {u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F generate salt seedu003Cu002Fcodeu003Eu003Ccodeu003E private static final int SALT_SEED = 12;u003Cu002Fcodeu003Eu003Ccodeu003E u002Fu002F the head fo saltu003Cu002Fcodeu003Eu003Ccodeu003E private static final String SALT_STARTSWITH = “$2a$12”;u003Cu002Fcodeu003Eu003Ccodeu003E public static final String SALT_KEY = “salt”;u003Cu002Fcodeu003Eu003Ccodeu003E public static final String CIPHER_KEY = “cipher”;u003Cu002Fcodeu003Eu003Ccodeu003E u002F**u003Cu002Fcodeu003Eu003Ccodeu003E * Bcrypt encryption algorithm methodu003Cu002Fcodeu003Eu003Ccodeu003E * @param encryptSourceu003Cu002Fcodeu003Eu003Ccodeu003E * need to encrypt the stringu003Cu002Fcodeu003Eu003Ccodeu003E * @return Map , two values in Map , salt and cipheru003Cu002Fcodeu003Eu003Ccodeu003E *u002Fu003Cu002Fcodeu003Eu003Ccodeu003E public static Map<String, String> Bcrypt(final String encryptSource) {u003Cu002Fcodeu003Eu003Ccodeu003E String salt = BCrypt.gensalt(SALT_SEED);u003Cu002Fcodeu003Eu003Ccodeu003E Map<String, String> bcryptResult = Bcrypt(salt, encryptSource);u003Cu002Fcodeu003Eu003Ccodeu003E return bcryptResult;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E u002F**u003Cu002Fcodeu003Eu003Ccodeu003E *u003Cu002Fcodeu003Eu003Ccodeu003E * @param salt encrypt salt, Must conform to the rulesu003Cu002Fcodeu003Eu003Ccodeu003E * @param encryptSourceu003Cu002Fcodeu003Eu003Ccodeu003E * @returnu003Cu002Fcodeu003Eu003Ccodeu003E *u002Fu003Cu002Fcodeu003Eu003Ccodeu003E public static Map<String, String> Bcrypt(final String salt, final String encryptSource) {u003Cu002Fcodeu003Eu003Ccodeu003E if (StringUtils.isBlank(encryptSource)) {u003Cu002Fcodeu003Eu003Ccodeu003E throw new RuntimeException(“Bcrypt encrypt input params can not be empty”);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E if (StringUtils.isBlank(salt) || salt.length != 29) {u003Cu002Fcodeu003Eu003Ccodeu003E throw new RuntimeException(“Salt can’t be empty and length must be to 29”);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E if (!salt.startsWith(SALT_STARTSWITH)) {u003Cu002Fcodeu003Eu003Ccodeu003E throw new RuntimeException(“Invalid salt version, salt version is $2a$12”);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E String cipher = BCrypt.hashpw(encryptSource, salt);u003Cu002Fcodeu003Eu003Ccodeu003E Map<String, String> bcryptResult = new HashMap<String, String>;u003Cu002Fcodeu003Eu003Ccodeu003E bcryptResult.put(SALT_KEY, salt);u003Cu002Fcodeu003Eu003Ccodeu003E bcryptResult.put(CIPHER_KEY, cipher);u003Cu002Fcodeu003Eu003Ccodeu003E return bcryptResult;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h3″u003E二、加密测试u003Cu002Fh1u003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrong toutiao-origin=”span”u003E1. MD5加密测试u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Eu002F**u003Cu002Fcodeu003Eu003Ccodeu003E * MD5加密u003Cu002Fcodeu003Eu003Ccodeu003E *u002Fu003Cu002Fcodeu003Eu003Ccodeu003Epublic class MD5Test {u003Cu002Fcodeu003Eu003Ccodeu003E public static void main(String[] args) {u003Cu002Fcodeu003Eu003Ccodeu003E String string = “我是一句话”;u003Cu002Fcodeu003Eu003Ccodeu003E String byteArrayToHexString = MD5Utils.byteArrayToHexString(string.getBytes);u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(byteArrayToHexString);u002Fu002Fe68891e698afe4b880e58fa5e8af9du003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrong toutiao-origin=”span”u003E2. baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003E加密测试u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Eu002F**u003Cu002Fcodeu003Eu003Ccodeu003E * baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003E加密u003Cu002Fcodeu003Eu003Ccodeu003E *u002Fu003Cu002Fcodeu003Eu003Ccodeu003Epublic class Bastu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003ETester {u003Cu002Fcodeu003Eu003Ccodeu003E public static void main(String[] args) {u003Cu002Fcodeu003Eu003Ccodeu003E String string = “我是一个字符串”;u003Cu002Fcodeu003Eu003Ccodeu003E String encodeData = Baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003EUtil.encodeData(string); u002Fu002F加密u003Cu002Fcodeu003Eu003Ccodeu003E String decodeData = Baseu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E64u003Cu002Fiu003EUtil.decodeData(encodeData); u002Fu002F解密u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(encodeData);u002Fu002F5oiR5piv5LiA5Liq5a2X56ym5Liyu003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(decodeData);u002Fu002F我是一个字符串u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrong toutiao-origin=”span”u003E3. SHA加密测试u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Eu002F**u003Cu002Fcodeu003Eu003Ccodeu003E * SHA加密u003Cu002Fcodeu003Eu003Ccodeu003E *u002Fu003Cu002Fcodeu003Eu003Ccodeu003Epublic class ShaTest {u003Cu002Fcodeu003Eu003Ccodeu003E public static void main(String[] args) {u003Cu002Fcodeu003Eu003Ccodeu003E String string = “我是一句话”;u003Cu002Fcodeu003Eu003Ccodeu003E String sha256Crypt = Sha2Crypt.sha256Crypt(string.getBytes);u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(sha256Crypt);u002Fu002F$5$AFoQTeyt$TiqmobvcQXjXaAQMYosAAO4KI8LfigZMGHzq.Dlp4NCu003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrong toutiao-origin=”span”u003E4. BCrypt加密测试u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Eu002F**u003Cu002Fcodeu003Eu003Ccodeu003E * BCrypt加密u003Cu002Fcodeu003Eu003Ccodeu003E *u002Fu003Cu002Fcodeu003Eu003Ccodeu003Epublic class BCryptTest {u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E public static void main(String[] args) {u003Cu002Fcodeu003Eu003Ccodeu003E String string = “我是一句话”;u003Cu002Fcodeu003Eu003Ccodeu003E Map<String, String> bcrypt = BcryptCipher.Bcrypt(string);u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(bcrypt.keySet); u002Fu002F[cipher, salt]u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(bcrypt.get(“cipher”)); u002Fu002F$2a$12$ylb92Z84gqlrSfzIztlCV.dK0xNbw.pOv3Uu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003EwXu003Cu002Fiu003EXA76llOsNRTJsEu002F.u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(bcrypt.get(“salt”)); u002Fu002F$2a$12$ylb92Z84gqlrSfzIztlCV.u003Cu002Fcodeu003Eu003Ccodeu003E Map<String, String> bcrypt2 = BcryptCipher.Bcrypt(bcrypt.get(“salt”),string);u003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(bcrypt2.get(“SALT_KEY”)); u002Fu002Fu003Cu002Fcodeu003Eu003Ccodeu003E System.out.println(bcrypt2.get(“CIPHER_KEY”)); u002Fu002Fu003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E-END-u003Cu002Fpu003Eu003Cpu003E如果看到这里,说明你喜欢这篇文章,请u003Cstrong toutiao-origin=”span” class=”highlight-text”u003E转发u003Cu002Fstrongu003Eu003Cstrong toutiao-origin=”span” class=”highlight-text”u003E、点赞u003Cu002Fstrongu003E。u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-5″u003E微信u003Cu002Fiu003E搜索「web_resource」,u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003E关注u003Cu002Fiu003E后回复「进群」或者扫描下方二维码即可进入无广告交流群。u003Cu002Fpu003Eu003Cpu003Eu003Cstrongu003E↓u003Cu002Fstrongu003E扫描二维码进群↓u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002FRclVELq4D7W5d4″ img_width=”499″ img_height=”642″ alt=”Java 开发中常用的 4 种加密方法” inline=”0″u003E”

原文始发于:Java 开发中常用的 4 种加密方法

主题测试文章,只做测试使用。发布者:℅傍ㄖ免沦陷dε鬼,转转请注明出处:http://www.cxybcw.com/17942.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code