SHA256---Using Java to implement encryption

Source: Internet
Author: User

Implementing encryption using Java's own: reference https://jingyan.baidu.com/article/2fb0ba40a2ef2b00f3ec5f74.html

/*** Implementation of SHA256 encryption with Java Native Digest *@paramSTR Encrypted message *@return     */     Public Staticstring 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 (); }        returnEncodestr; }    /*** Convert byte to 16 binary *@parambytes *@return     */    Private StaticString Byte2hex (byte[] bytes) {StringBuffer StringBuffer=NewStringBuffer (); String Temp=NULL;  for(inti=0;i<bytes.length;i++) {Temp= Integer.tohexstring (Bytes[i] & 0xFF); if(Temp.length () ==1){                //1 Get one bit to make a 0 operationStringbuffer.append ("0");        } stringbuffer.append (temp); }        returnstringbuffer.tostring (); }

SHA256---Using Java to implement encryption

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.