<small id='bc5eyZ3'></small> <noframes id='kI2Y'>

  • <tfoot id='1xvk4Mnst'></tfoot>

      <legend id='c5dDxPqO1'><style id='on9DlJ0eY'><dir id='lhVdHiDgv'><q id='WEzvgJXdBx'></q></dir></style></legend>
      <i id='VEMLj'><tr id='4PQdeA'><dt id='C3nF6eM'><q id='LsB3rd'><span id='L8iljpK'><b id='1xjq2V'><form id='rIhPUD'><ins id='3qPEc1x6jh'></ins><ul id='Oxh7Fq'></ul><sub id='xIKt'></sub></form><legend id='WXQDk'></legend><bdo id='Cm9klHvD'><pre id='Ttpuxb5lPf'><center id='WQIYG'></center></pre></bdo></b><th id='f6yTBX9a'></th></span></q></dt></tr></i><div id='qzrpfb'><tfoot id='6Gcj2B'></tfoot><dl id='Q5FoXSCI'><fieldset id='UrCnYH9f'></fieldset></dl></div>

          <bdo id='dv2Z'></bdo><ul id='h0vC'></ul>

          1. <li id='Sgoa'></li>
            登陆

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

            admin 2019-11-23 261人围观 ,发现0个评论

            Java后端,挑选

            技能博文,及时送达

            来历 | my.oschina.net/u/4139951/blog/3077236

            作者 | 爱编程的浪子

            一、东西类

            1. md5加密东西类

            2. base64加密东西类

            3. Bcrypt东西类

            二、加密测验

            1. MD5加密测验

            2. base64加密测验

            3. SHA加密测验

            4. BCrypt加密测验

            一、东西类

            1. md5加密东西类

            public class MD5Utils {
            private static final String hexDigIts = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
            /** * MD5加密 * @param origin 字符 * @param charsetname 编码 * @return */ public static String MD5Encode(String origin, String charsetname){ String resultString = ; try{ resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); if( == charsetname || "".equals(charsetname)){ resultString = byteArrayToHexString(md.digest(resultString.getBytes)); }else{ resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname))); } }catch (Exception e){ } return resultString; }

            public static String byteArrayToHexString(byte b[]){ StringBuffer resultSb = new StringBuffer; for(int i = 0; i < b.length; i++){ resultSb.append(byteToHexString(b[i])); } return resultSb.toString; }
            public static String byteToHexString(byte b){ int n = b; if(n < 0){ n += 256; } int d1 = n / 16; int d2 = n % 16; return hexDigIts[d1] + hexDigIts[d2]; }
            }

            2. base64加密东西类

            public class Base64Util {
            // 字符串编码 private static final String UTF_8 = "UTF-8";
            /** * 加密字符串 * @param inputData * @return */ public static String Java 开发中常用的 4 种加密办法decodeData(String inputData) { try { if ( == inputData) { return ; } return new String(Base64.decodeBase64(inputData.getBytes(UTF_8)), UTF_8); } catch (UnsupportedEncodingException e) { } return ; }
            /** * 解密加密后的字符串 * @param inputData * @return */ public static String encodeData(String inputData) { try { if ( == inputData) { return ; } return new String(Base64.encodeBase64(inputData.getBytes(UTF_8)), UTF_8); } catch (UnsupportedEncodingException e) { } return ; }
            public static void main(String[] args) { System.out.println(Base64Util.encodeData("我是中文")); String enStr = Base64Util.encodeData("我是中文"); System.out.println(Base64Util.decodeData(enStr)); }}

            3. Bcrypt东西类

            public class BcryptCipher { // generate salt seed private static final int SALT_SEED = 12; // the head fo salt private static final String SALT_STARTSWITH = "$2a$12"; public static final String SALT_KEY = "salt"; public static final String CIPHE盗墓者训练营R_KEY = "cipher"; /** * Bcrypt encryption algorithm method * @param encryptSource * need to encrypt the string * @return Map , two values in Map , salt and cipher */ public static Map Bcrypt(final String encryptSource) { StJava 开发中常用的 4 种加密办法ring salt = BCrypt.gensalt(SALT_SEED); Map bcryptResult = Bcrypt(salt, encryptSource); return bcryptResult; } /** * * @param salt encrypt salt, Must conform to the rules * @param encryptSource * @return */ public static Map Bcrypt(final String salt, final String encryptSource) { if (StringUtils.isBlank(encryptSource)) { throw new RuntimeException("Bcrypt encrypt input params can not be empty"); } if (StringUtils.isBlank(salt) || salt.length != 29) { throw new RuntimeException("Salt can't be empty and length must be to 29"); } if (!salt.startsWith(SALT_STARTSWITH)) { throw new RuntimeException("Invalid salt version, salt version is $2a$12"); } String cipher = BCrypt.hashpw(encryptSource, salt); Map bcryptResult = new HashMap; bcryptResult.put(SALT_KEY, salt); bcryptResult.put(CIPHER_KEY, cipher); return bcryptResult; }}

            二、加密测验

            1. MD5加密测验

            /** * MD5加密 */public class MD5Test { public static void main(String[] args) { String string = "我是一句话"; String byteArrayToHexString = MD5Utils.byteArrayToHexString(string.getBytes); System.out.println(byteArrayToHexString);//e68891e698afe4b880e58fa5e8af9d
            }}

            2. base64加密测验

            /** * base64加密 */public class Bast64Tester { public static void main(String[] args) { String string = "我是一个字符串"; String encodeData = Base64Util.encodeData(string); //加密 String decodeData = Base64Util.decodeData(encodeData); //解密 System.out.println(encodeDJava 开发中常用的 4 种加密办法ata);//5oiR5piv5LiA5Liq5a2X56ym5Liy System.out.println(decodeData);//我是一个字符串
            }}

            3. SHA加密测验

            /** * SHA加密 */publJava 开发中常用的 4 种加密办法ic class ShaTest { public static void main(String[] args) { String string = "我是一句话"; String sha256Crypt = Sha2Crypt.sha256Crypt(string.getBytes); System.out.pJava 开发中常用的 4 种加密办法rintln(sha256Crypt);//$5$AFoQTeyt$TiqmobvcQXjXaAQMYosAAO4KI8LfigZMGHzq.Dlp4NC
            }}

            4. BCrypt加密测验

            /** * BCrypt加密 */public class BCryptTest {
            public static void maiJava 开发中常用的 4 种加密办法n(String[] args) { String string = "我是一句话"; Map bcrypt = 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")); // System.out.println(bcrypt2.get("CIPHER_KEY")); // }}

            -END-

            假如看到这儿,阐明你喜爱这篇文章,请转发、点赞微信查找「web_resource」,重视后回复「进群」或许扫描下方二维码即可进入无广告沟通群。

            扫描二维码进群↓

          2. 智能售货终端带来革命性改动
          3. 1号站客户端下载-丰田召回12637辆进口埃尔法:启停形式下发动机或无法发动
          4. 1号站客户端下载-丰厚协作深挖IP大生意,供应端开放创新力
          5. 请关注微信公众号
            微信二维码
            不容错过
            Powered By Z-BlogPHP