Two implementations of Java SHA 256 encryption are explained in detail

  • 2020-09-16 07:29:46
  • OfStack

This article illustrates two implementation methods of Java SHA-256 encryption. To share for your reference, the details are as follows:

Recently, I have been doing one function of registration, the password needs to be encrypted. At first, I wanted to use MD5 encryption, but I heard that it had been broken, so I wanted to play es6EN-256 encryption. After studying, two methods are summarized for future reference:

1. Use Apache's tool class to achieve encryption:

maven:


<dependency>
 <groupId>commons-codec</groupId>
 <artifactId>commons-codec</artifactId>
 <version>${common-codec.version}</version>
</dependency>

Implementation code:


/***
*  using Apache Tool class implementation SHA-256 encryption 
* @param str  Encrypted message 
* @return
*/
public static String getSHA256Str(String str){
 MessageDigest messageDigest;
 String encdeStr = "";
 try {
  messageDigest = MessageDigest.getInstance("SHA-256");
  byte[] hash = messageDigest.digest(str.getBytes("UTF-8"));
  encdeStr = Hex.encodeHexString(hash);
 } catch (NoSuchAlgorithmException e) {
  e.printStackTrace();
 } catch (UnsupportedEncodingException e) {
  e.printStackTrace();
 }
 return encdeStr;
}

2. Use the implementation of Java to encrypt:


/**
*  using java Native summary implementation SHA256 encryption 
* @param str  Encrypted message 
* @return
*/
public static String getSHA256StrJava(String str){
 MessageDigest messageDigest;
 String encodeStr = "";
 try {
  messageDigest = MessageDigest.getInstance("SHA-256");
  messageDigest.update(str.getBytes("UTF-8"));
  encodeStr = byte2Hex(messageDigest.digest());
 } catch (NoSuchAlgorithmException e) {
  e.printStackTrace();
 } catch (UnsupportedEncodingException e) {
  e.printStackTrace();
 }
 return encodeStr;
}
/**
*  will byte to 16 Into the system 
* @param bytes
* @return
*/
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 get 1 Complement of bits 0 operation 
  stringBuffer.append("0");
  }
  stringBuffer.append(temp);
 }
 return stringBuffer.toString();
}

PS: For those who are interested in encryption and decryption, please refer to our online tools:

Online SHA1 encryption tools:
http://tools.ofstack.com/password/sha1encode

Text online encryption and decryption tools (including AES, DES, RC4, etc.) :
http://tools.ofstack.com/password/txt_encode

Online hashing/hashing algorithm encryption tools:
http://tools.ofstack.com/password/hash_encrypt

MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 Encryption tools:
http://tools.ofstack.com/password/hash_md5_sha

Online sha1 sha224 / sha256 sha384 / sha512 encryption tools:
http://tools.ofstack.com/password/sha_encode

For more information about java, please refer to Java Mathematical Operation Skills summary, Java Data Structure and Algorithm Tutorial, Java Character and string Operation Skills Summary, Java Node Operation Skills Summary and Java Array operation Skills Summary.

I hope this article has been helpful in java programming.


Related articles: