SHA-256加密簡單例項(客戶端、服務端)

耗子他大哥發表於2018-06-26

該博文是參照其他博主內容經過親測的產物,也是因為網上關於這類博文比較多,大多還類似,再此就不註明原創了,如有侵權,望主動聯絡,謝謝!!!

  這次選用SHA-256的原因也很簡單,據說之前用的MD5被人爆破了,寧可信其有不可信其無。

  言歸正傳,總結三種方式,最終輸出結果一致,下面看服務端程式碼:

package com.zxx.until;

import org.apache.commons.codec.binary.Hex;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class ads {

private static final char[] HEX_DIGITS = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

    public static void main(String[] args) {
        toSHA001("123456123456");
        toSHA002("123456123456");
        toSHA003("123456123456");
    }

    private static void toSHA001(String text) {

        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(text.getBytes("UTF-8"));
            String output = Hex.encodeHexString(hash);
            System.out.println(output);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public static String toSHA002(String str){
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr = byte2Hex(messageDigest.digest());
            System.out.println(encodeStr);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }
    private static String byte2Hex(byte[] bytes){
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i=0;i<bytes.length;i++){
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length()==1){
                //1得到一位的進行補0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    private static void toSHA003(String text) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(text.getBytes());
            byte[] digest = messageDigest.digest();
            int length = digest.length;
            StringBuilder buf = new StringBuilder(length*2);
            //密文轉換成16進位制
            for(int j = 0 ; j < length ; j++){
                buf.append(HEX_DIGITS[(digest[j] >> 4) & 0x0f]);
                buf.append(HEX_DIGITS[digest[j] & 0x0f]);
            }
            System.out.println(buf.toString());

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}

用到一個JAR包:

<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<!-- 如果用的alibaba的資料來源請暫時註釋掉,或者呼叫 HEX 導jar包時看清楚 -->
<!-- import org.apache.commons.codec.binary.Hex; -->
<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.11</version>
</dependency>

客戶端也有專門的 js 可以做:
我是 JS ,點我呀

程式碼就一句話:

<html>
<head></head>
<body>
    <script type="text/javascript" src="sha256_2.js"></script>
    <script type="text/javascript">

      document.write(sha256_digest("hello world"));

    </script> 
</body>
</html>

  最後多說一句,因為SHA-256和之前的MD5都屬於雜湊演算法加密技術,也就是說是不可逆的,作為開發人員只要記住加密規則然後再去對比密文即可,對於解密這種事情就交給那些大神去做就好了!!!

相關文章