浅析Java 常用的 4 种加密方式(MD5+Base64+SHA+BCrypt)
一、工具类
md5加密工具类
publicclassMD5Utils{ privatestaticfinalStringhexDigIts[]={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; /** *MD5加密 *@paramorigin字符 *@paramcharsetname编码 *@return */ publicstaticStringMD5Encode(Stringorigin,Stringcharsetname){ StringresultString=null; try{ resultString=newString(origin); MessageDigestmd=MessageDigest.getInstance("MD5"); if(null==charsetname||"".equals(charsetname)){ resultString=byteArrayToHexString(md.digest(resultString.getBytes())); }else{ resultString=byteArrayToHexString(md.digest(resultString.getBytes(charsetname))); } }catch(Exceptione){ } returnresultString; } publicstaticStringbyteArrayToHexString(byteb[]){ StringBufferresultSb=newStringBuffer(); for(inti=0;ibase64加密工具类
publicclassBase64Util{ //字符串编码 privatestaticfinalStringUTF_8="UTF-8"; /** *加密字符串 *@paraminputData *@return */ publicstaticStringdecodeData(StringinputData){ try{ if(null==inputData){ returnnull; } returnnewString(Base64.decodeBase64(inputData.getBytes(UTF_8)),UTF_8); }catch(UnsupportedEncodingExceptione){ } returnnull; } /** *解密加密后的字符串 *@paraminputData *@return */ publicstaticStringencodeData(StringinputData){ try{ if(null==inputData){ returnnull; } returnnewString(Base64.encodeBase64(inputData.getBytes(UTF_8)),UTF_8); }catch(UnsupportedEncodingExceptione){ } returnnull; } publicstaticvoidmain(String[]args){ System.out.println(Base64Util.encodeData("我是中文")); StringenStr=Base64Util.encodeData("我是中文"); System.out.println(Base64Util.decodeData(enStr)); } }Bcrypt工具类
publicclassBcryptCipher{ //generatesaltseed privatestaticfinalintSALT_SEED=12; //theheadfosalt privatestaticfinalStringSALT_STARTSWITH="$2a$12"; publicstaticfinalStringSALT_KEY="salt"; publicstaticfinalStringCIPHER_KEY="cipher"; /** *Bcryptencryptionalgorithmmethod *@paramencryptSource *needtoencryptthestring *@returnMap,twovaluesinMap,saltandcipher */ publicstaticMapBcrypt(finalStringencryptSource){ Stringsalt=BCrypt.gensalt(SALT_SEED); Map bcryptResult=Bcrypt(salt,encryptSource); returnbcryptResult; } /** * *@paramsaltencryptsalt,Mustconformtotherules *@paramencryptSource *@return */ publicstaticMap Bcrypt(finalStringsalt,finalStringencryptSource){ if(StringUtils.isBlank(encryptSource)){ thrownewRuntimeException("Bcryptencryptinputparamscannotbeempty"); } if(StringUtils.isBlank(salt)||salt.length()!=29){ thrownewRuntimeException("Saltcan'tbeemptyandlengthmustbeto29"); } if(!salt.startsWith(SALT_STARTSWITH)){ thrownewRuntimeException("Invalidsaltversion,saltversionis$2a$12"); } Stringcipher=BCrypt.hashpw(encryptSource,salt); Map bcryptResult=newHashMap (); bcryptResult.put(SALT_KEY,salt); bcryptResult.put(CIPHER_KEY,cipher); returnbcryptResult; } } 二、加密测试
MD5加密测试
/** *MD5加密 */ publicclassMD5Test{ publicstaticvoidmain(String[]args){ Stringstring="你好世界"; StringbyteArrayToHexString=MD5Utils.byteArrayToHexString(string.getBytes()); System.out.println(byteArrayToHexString);//e68891e698afe4b880e58fa5e8af9d } }base64加密测试
/** *base64加密 */ publicclassBast64Tester{ publicstaticvoidmain(String[]args){ Stringstring="你好世界"; StringencodeData=Base64Util.encodeData(string);//加密 StringdecodeData=Base64Util.decodeData(encodeData);//解密 System.out.println(encodeData);//5oiR5piv5LiA5Liq5a2X56ym5Liy System.out.println(decodeData);//你好世界 } }SHA加密测试
/** *SHA加密 */ publicclassShaTest{ publicstaticvoidmain(String[]args){ Stringstring="你好世界"; Stringsha256Crypt=Sha2Crypt.sha256Crypt(string.getBytes()); System.out.println(sha256Crypt);//$5$AFoQTeyt$TiqmobvcQXjXaAQMYosAAO4KI8LfigZMGHzq.Dlp4NC } }BCrypt加密测试
/** *BCrypt加密 */ publicclassBCryptTest{ publicstaticvoidmain(String[]args){ Stringstring="你好世界"; Mapbcrypt=BcryptCipher.Bcrypt(string); System.out.println(bcrypt.keySet());//[cipher,salt] System.out.println(bcrypt.get("cipher"));//$2a$12$ylb92Z84gqlrSfzIztlCV.dK0xNbw.pOv3UwXXA76llOsNRTJsE/. System.out.println(bcrypt.get("salt"));//$2a$12$ylb92Z84gqlrSfzIztlCV. Map bcrypt2=BcryptCipher.Bcrypt(bcrypt.get("salt"),string); System.out.println(bcrypt2.get("SALT_KEY"));//null System.out.println(bcrypt2.get("CIPHER_KEY"));//null } } 总结
以上所述是小编给大家介绍的浅析Java常用的4种加密方式(MD5+Base64+SHA+BCrypt),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!